forked from msalom28/Larasocial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (66 loc) · 2.58 KB
/
server.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var server = require('http').createServer(),
io = require('socket.io')(server),
logger = require('winston'),
port = 3000;
// Logger config
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { colorize: true, timestamp: true });
logger.info('SocketIO > listening on port ' + port);
var clients = {};
io.on('connection', function (socket){
//Regiter a client based on userid
socket.on('register', function(data)
{
if (clients[data.userId] && clients[data.userId].sockets instanceof Array)
{
//if user is already registered
//with one or many socket clients,
//just push socket id to array of sockets
clients[data.userId].sockets.push(socket.id);
} else
{
//if it is the first socket client
//add an array and push socket id
clients[data.userId] = { sockets: []};
clients[data.userId].sockets.push(socket.id);
}
logger.info('SocketIO > New connection -> userId = ' + data.userId +' socketId = ' + socket.id);
});
socket.on('broadcast', function (data) {
//when LAMP server broadcast look for user in list of clients
if(clients[data.userId])
{
for(var soct in clients[data.userId].sockets){
//proxy event to all connected sockets
io.sockets.connected[clients[data.userId].sockets[soct]].emit(data.receiverId, data);
logger.info('New data was sent = ' + JSON.stringify(data));
}
}
else
{
logger.info('UserId is not open for comunication: ' + data.userId);
}
});
socket.on('disconnect', function () {
//when socket disconnects remove socket from list of sockets
for(var name in clients){
for (var soct in clients[name].sockets){
if(clients[name].sockets[soct] === socket.id)
{
//remove socket from array of sockets
clients[name].sockets.splice(soct, 1);
logger.info('socket removed');
//if no more sockets are connected
//remove user from list of clients
if (clients[name].sockets.length === 0)
{
delete clients[name];
logger.info('userId completely removed');
}
break;
}
}
}
});
});
server.listen(port);