-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
35 lines (29 loc) · 867 Bytes
/
Copy pathsocket.js
File metadata and controls
35 lines (29 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module.exports = (io, Users, rndString, Chats) =>{
io.on('connection', (socket)=>{
socket.on('message', (msg, room_token)=>{
io.broadcast.to(room_token).emit('message', msg);
})
.on('make', (token, chat_people)=>{
var new_Chat = new Chats({
chat_people: chat_people,
room_token: rndString.generate(),
});
new_Chat.save((err, result)=>{
if(err) io.emit('make', 'save err');
if(result){
io.emit('make', new_Chat.room_token);
io.room.join(new_Chat.room_token);
}
});
})
.on('join', (room_token, token)=>{
Chats.findOne({room_token: room_token}, (err, user)=> {
if(err) throw err;
if(users) socket.room.join(user.token);
});
})
.on('message', (msg)=>{
io.broadcast.emit('message', msg);
})
});
}