-
Notifications
You must be signed in to change notification settings - Fork 6
/
socketEvents.js
38 lines (38 loc) · 1.37 KB
/
socketEvents.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
module.exports = function(io, participants) {
io.on('connection', function(socket) {
var client = {};
socket.on('new participant', function(name) {
// push to participant list
participants.push(name);
//update new client, this is used to track the client while disconnecting
client.name = name;
client.socket = socket;
console.log(new Date() + ' ' + client.name + ' connected');
// broadcast new user notification
socket.broadcast.emit('new participant', name);
});
socket.on('chat message', function(data) {
socket.broadcast.emit('chat message', data);
});
socket.on('typing', function(name) {
socket.broadcast.emit('typing', name);
});
socket.on('stoppedTyping', function(name) {
socket.broadcast.emit('stoppedTyping', name);
});
socket.on('removeTyping', function(name) {
socket.broadcast.emit('removeTyping', name);
});
socket.on('disconnect', function() {
if (client.name == undefined) {
// do nothing, client disconnected before joining chat
} else {
console.log(new Date() + ' ' + client.name + ' disconnected');
// remove from participants list
participants.splice(participants.indexOf(client.name), 1);
// broadcast to other participants
socket.broadcast.emit('disconnected', client.name);
}
});
});
}