-
Notifications
You must be signed in to change notification settings - Fork 13
/
nodeServer.js
50 lines (44 loc) · 1.24 KB
/
nodeServer.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
// note, io.listen(<port>) will create a http server for you
var io = require('socket.io').listen(8080);
var nick = "";
var socketList = [];
var counter = 0;
var userList = [];
io.sockets.on('connection', function (socket) {
socket.on('signIn', function (name) {
for(var i=0;i<counter;i++) {
if(userList[i] == name) {
socket.emit('error', "Username already taken!");
return;
}
}
userList[counter] = name;
console.log("User Added : "+name);
socket.emit('ready', counter, userList);
for(var i=0;i<counter;i++)
socketList[i].emit('addPeer', i, userList);
socketList[counter] = socket;
counter++;
console.log("Users added!");
});
socket.on('sendMessage', function (from, to, content) {
console.log(content);
socketList[to].emit('receiveMessage',from, content);
console.log("Message Sent!");
});
socket.on('reject', function (myId, remoteId) {
socketList[remoteId].send(from, 'REJECT', userList[from]);
});
socket.on('disconnect', function () {
for(var i=0;i<socketList.length;i++)
if(socket==socketList[i]) {
userList[i]=-1;
console.log(i+"th User Deleted!");
for(var j=0;j<counter;j++) {
if(userList[j]!=-1)
socketList[j].emit('addPeer', j, userList);
}
break;
}
});
});