-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
executable file
·43 lines (36 loc) · 1.33 KB
/
socket.js
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
36
37
38
39
40
41
42
43
const socket = require('socket.io');
const config = require('./configs')
const users = [];
module.exports = function(app){
const io = socket(app.listen(config.socketPort));
io.on('connection', function(client){
var id = client.id;
client.on('new-user',function(username){
users.push({
id,
name:username
});
client.emit('users',users);
client.broadcast.emit('users',users);
})
console.log('client connected to socket server');
// client.emit('hi','hello and welcome to socket server');
// client.on('hello', function(data){
// console.log('hello event>>',data);
// })
//new-msg event on onSubmit on client
client.on('new-msg', function(data){
client.emit('reply-msg-own',data); //for own client
client.broadcast.to(data.receiverId).emit('reply-msg',data); //for each and every client connected to server
})
client.on('disconnect', function(){
users.forEach(function(user,i){
if(user.id === id){
users.splice(i, 1);
}
})
client.emit('users',users);
client.broadcast.emit('users',users);
})
})
}