-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
89 lines (82 loc) · 2.44 KB
/
server.js
File metadata and controls
89 lines (82 loc) · 2.44 KB
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
var io = require('socket.io').listen(8089);
io.configure(function(){
io.set('transports', ['websocket', 'flashsocket']);
});
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
var userList = new Array();
var User = function(){
this.id = "";
this.nickname = "";
this.toString = function(){
return "User: ID:" + this.id + " Nickname: " + this.nickname;
};
};
var findUser = function(userArray,id){
var user = null;
for(var i = 0; i < userArray.length; i++){
if(userArray[i].id == id){
user = userArray[i];
}
}
return user;
};
var sendToOthers = function(userArray,id,nickname,msg){
for(var i = 0; i < userArray.length; i++){
if(userArray[i].id != id){
console.log(id);
io.sockets.sockets[userArray[i].id].json.send({type:5,chatMsg:msg,sender:nickname});
}
}
};
var sendToAll = function(userArray,nickname,msg,typeValue){
for(var i = 0;i < userArray.length; i++){
io.sockets.sockets[userArray[i].id].json.send({type:typeValue,chatMsg:msg,sender:nickname});
}
};
var removeUser = function(userArray,user){
for(var i = 0;i < userArray.length; i++){
if(userArray[i].id === user.id){
userArray.remove(i);
}
}
};
io.sockets.on('connection', function(socket){
console.log("A user is connected");
socket.on('message',function(data){
var user = findUser(userList,socket.id);
if(user != null){
console.log("A message is received: " + data);
console.log("Message received from: " + user.toString());
sendToOthers(userList,user.id,user.nickname,data);
}
});
socket.on('setNickname',function(data){
console.log("Data is: " + data.nickname);
var newUser = new User();
newUser.id = socket.id;
newUser.nickname = data.nickname;
userList.push(newUser);
console.log(userList);
socket.json.send({type:10,msg:"Nickname setting is finished"});
sendToAll(userList,newUser.nickname,userList,11);
});
socket.on('sendChat',function(data){
var user = findUser(userList,socket.id);
if(user != null){
console.log("A message is received: " + data);
console.log("Message received from: " + user.toString());
sendToAll(userList,user.nickname,data.chatMsg,5);
}
});
socket.on('disconnect',function(){
console.log("User is disconnected");
//Disconnect from server
var user = findUser(userList,socket.id);
removeUser(userList,user);
sendToAll(userList,"",userList,11);
});
});