-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (30 loc) · 884 Bytes
/
app.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
var path = require('path');
var connect = require('connect');
var app = connect().use(connect.static(path.join(__dirname, '..', 'dist')));
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var users = [];
io.on('connection', function(socket){
console.log("connected");
var user = {};
socket.on('peerId', function(id) {
console.log("got peerId");
user.id = id;
if(users.length === 0) {
user.index = 0;
}else {
user.index = users[users.length - 1].index + 1;
}
users.push(user);
console.log("users:", users);
socket.emit('index', user.index);
io.emit('userList', users);
});
socket.on('disconnect', function() {
var index = users.indexOf(user);
users = users.slice(0, index).concat(users.slice(index + 1));
io.emit('userList', users);
console.log("after disco:", users);
});
});
server.listen(80);