-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_server.js
More file actions
32 lines (27 loc) · 848 Bytes
/
socket_server.js
File metadata and controls
32 lines (27 loc) · 848 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
var io = require('socket.io').listen(parseInt(process.env.PORT||5000));
var i = 0;
var usernames = {};
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
socket.on('add_user', function(user_name){
console.log('add user');
socket.name = user_name;
usernames[user_name] = user_name;
io.sockets.emit('updateusers', usernames);
});
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.name];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// socket.emit('news', { hello: 'world' });
// socket.on('my other event', function (data) {
// socket.emit('news', i);
// console.log(data);
// i++;
// });
});