2016-04-11 01:51:47 +00:00
|
|
|
(function(){
|
|
|
|
var WebSocketServer=require('ws').Server;
|
|
|
|
var wss=new WebSocketServer({port:8080});
|
|
|
|
|
2016-04-13 04:17:17 +00:00
|
|
|
var rooms=[{},{},{},{},{},{}];
|
2016-04-11 01:51:47 +00:00
|
|
|
var clients={};
|
|
|
|
var messages={
|
2016-04-17 06:00:26 +00:00
|
|
|
enter:function(index,nickname,avatar,config,mode){
|
2016-04-11 01:51:47 +00:00
|
|
|
this.nickname=nickname;
|
|
|
|
this.avatar=avatar;
|
|
|
|
var room=rooms[index];
|
|
|
|
if(!room){
|
|
|
|
index=0;
|
|
|
|
room=rooms[0];
|
|
|
|
}
|
|
|
|
this.room=room;
|
|
|
|
if(room.owner){
|
2016-04-17 06:00:26 +00:00
|
|
|
if(room.servermode&&!room.owner._onconfig&&config&&mode){
|
2016-04-18 03:13:31 +00:00
|
|
|
room.owner.sendl('createroom',index,config,mode);
|
2016-04-17 06:00:26 +00:00
|
|
|
room.owner._onconfig=this;
|
|
|
|
room.owner.nickname=nickname;
|
|
|
|
room.owner.avatar=avatar;
|
|
|
|
}
|
|
|
|
else if(!room.config){
|
2016-04-11 01:51:47 +00:00
|
|
|
this.sendl('enterroomfailed');
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.owner=room.owner;
|
2016-04-11 06:13:35 +00:00
|
|
|
this.owner.sendl('onconnection',this.wsid);
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
2016-04-11 06:13:35 +00:00
|
|
|
util.updaterooms();
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
room.owner=this;
|
2016-04-18 03:13:31 +00:00
|
|
|
this.sendl('createroom',index);
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
|
|
|
},
|
2016-04-17 09:54:13 +00:00
|
|
|
changeAvatar:function(nickname,avatar){
|
|
|
|
this.nickname=nickname;
|
|
|
|
this.avatar=avatar;
|
|
|
|
util.updaterooms();
|
|
|
|
},
|
2016-04-18 05:13:47 +00:00
|
|
|
server:function(cfg){
|
|
|
|
if(cfg){
|
|
|
|
this.servermode=true;
|
|
|
|
var room=rooms[cfg[0]];
|
|
|
|
if(!room||room.owner){
|
|
|
|
this.sendl('reloadroom',true);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
room.owner=this;
|
|
|
|
this.room=room;
|
|
|
|
this.nickname=cfg[1];
|
|
|
|
this.avatar=cfg[2];
|
|
|
|
this.sendl('createroom',cfg[0],{},'auto')
|
2016-04-17 06:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-18 05:13:47 +00:00
|
|
|
else{
|
|
|
|
for(var i=0;i<rooms.length;i++){
|
|
|
|
if(!rooms[i].owner){
|
|
|
|
rooms[i].owner=this;
|
|
|
|
rooms[i].servermode=true;
|
|
|
|
this.room=rooms[i];
|
|
|
|
this.servermode=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
util.updaterooms();
|
|
|
|
}
|
2016-04-17 06:00:26 +00:00
|
|
|
},
|
2016-04-11 01:51:47 +00:00
|
|
|
config:function(config){
|
|
|
|
var room=this.room;
|
|
|
|
if(room&&room.owner==this){
|
2016-04-17 06:00:26 +00:00
|
|
|
if(room.servermode){
|
|
|
|
room.servermode=false;
|
|
|
|
if(this._onconfig){
|
|
|
|
if(clients[this._onconfig.wsid]){
|
|
|
|
this._onconfig.owner=this;
|
|
|
|
this.sendl('onconnection',this._onconfig.wsid);
|
|
|
|
}
|
|
|
|
delete this._onconfig;
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 01:51:47 +00:00
|
|
|
room.config=config;
|
|
|
|
}
|
2016-04-11 06:13:35 +00:00
|
|
|
util.updaterooms();
|
2016-04-11 01:51:47 +00:00
|
|
|
},
|
2016-04-11 06:13:35 +00:00
|
|
|
send:function(id,message){
|
|
|
|
if(clients[id]&&clients[id].owner==this){
|
2016-04-11 08:49:00 +00:00
|
|
|
try{
|
|
|
|
clients[id].send(message);
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
clients[id].close();
|
|
|
|
}
|
2016-04-11 06:13:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
close:function(id){
|
|
|
|
if(clients[id]&&clients[id].owner==this){
|
|
|
|
clients[id].close();
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
2016-04-11 08:33:58 +00:00
|
|
|
},
|
2016-04-11 01:51:47 +00:00
|
|
|
};
|
|
|
|
var util={
|
|
|
|
sendl:function(){
|
2016-04-11 06:34:50 +00:00
|
|
|
var args=[];
|
|
|
|
for(var i=0;i<arguments.length;i++){
|
|
|
|
args.push(arguments[i]);
|
|
|
|
}
|
2016-04-11 08:49:00 +00:00
|
|
|
try{
|
|
|
|
this.send(JSON.stringify(args));
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
this.close();
|
|
|
|
}
|
2016-04-11 01:51:47 +00:00
|
|
|
},
|
|
|
|
getid:function(){
|
|
|
|
return (Math.floor(1000000000+9000000000*Math.random())).toString();
|
|
|
|
},
|
|
|
|
getroomlist:function(){
|
|
|
|
var roomlist=[];
|
2016-04-13 04:17:17 +00:00
|
|
|
for(var i=0;i<rooms.length;i++){
|
2016-04-11 06:13:35 +00:00
|
|
|
rooms[i]._num=0;
|
|
|
|
}
|
|
|
|
for(var i in clients){
|
2016-04-17 06:00:26 +00:00
|
|
|
if(clients[i].room&&!clients[i].servermode){
|
2016-04-11 06:13:35 +00:00
|
|
|
clients[i].room._num++;
|
|
|
|
}
|
|
|
|
}
|
2016-04-13 04:17:17 +00:00
|
|
|
for(var i=0;i<rooms.length;i++){
|
2016-04-17 06:00:26 +00:00
|
|
|
if(rooms[i].servermode){
|
|
|
|
roomlist[i]='server';
|
|
|
|
}
|
|
|
|
else if(rooms[i].owner&&rooms[i].config){
|
|
|
|
if(rooms[i]._num==0){
|
2016-04-18 03:13:31 +00:00
|
|
|
rooms[i].owner.sendl('reloadroom');
|
2016-04-17 06:00:26 +00:00
|
|
|
}
|
2016-04-11 06:13:35 +00:00
|
|
|
roomlist[i]=[rooms[i].owner.nickname,rooms[i].owner.avatar,
|
|
|
|
rooms[i].config,rooms[i]._num];
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
roomlist[i]=null;
|
|
|
|
}
|
2016-04-11 06:13:35 +00:00
|
|
|
delete rooms[i]._num;
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
|
|
|
return roomlist;
|
2016-04-11 06:13:35 +00:00
|
|
|
},
|
|
|
|
updaterooms:function(){
|
|
|
|
var roomlist=util.getroomlist();
|
|
|
|
for(var i in clients){
|
|
|
|
if(!clients[i].room){
|
|
|
|
clients[i].sendl('updaterooms',roomlist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-04-11 01:51:47 +00:00
|
|
|
};
|
|
|
|
wss.on('connection',function(ws){
|
|
|
|
ws.sendl=util.sendl;
|
|
|
|
ws.wsid=util.getid();
|
|
|
|
clients[ws.wsid]=ws;
|
|
|
|
ws.sendl('roomlist',util.getroomlist());
|
2016-04-11 08:33:58 +00:00
|
|
|
ws.heartbeat=setInterval(function(){
|
2016-04-11 08:49:00 +00:00
|
|
|
if(ws.beat){
|
2016-04-11 08:33:58 +00:00
|
|
|
ws.close();
|
|
|
|
clearInterval(ws.heartbeat);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
ws.beat=true;
|
2016-04-11 08:49:00 +00:00
|
|
|
try{
|
|
|
|
ws.send('heartbeat');
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
ws.close();
|
|
|
|
}
|
2016-04-11 08:33:58 +00:00
|
|
|
}
|
|
|
|
},60000);
|
2016-04-11 01:51:47 +00:00
|
|
|
ws.on('message',function(message){
|
2016-04-22 06:59:46 +00:00
|
|
|
if(!clients[this.wsid]) return;
|
2016-04-11 08:33:58 +00:00
|
|
|
if(message=='heartbeat'){
|
|
|
|
this.beat=false;
|
|
|
|
}
|
|
|
|
else if(this.owner){
|
2016-04-11 06:13:35 +00:00
|
|
|
this.owner.sendl('onmessage',this.wsid,message);
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
2016-04-11 06:13:35 +00:00
|
|
|
else{
|
|
|
|
var arr;
|
|
|
|
try{
|
|
|
|
arr=JSON.parse(message);
|
|
|
|
if(!Array.isArray(arr)){
|
|
|
|
throw('err');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e){
|
|
|
|
this.sendl('denied','banned');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(arr.shift()=='server'){
|
|
|
|
var type=arr.shift();
|
|
|
|
if(messages[type]){
|
|
|
|
messages[type].apply(this,arr);
|
|
|
|
}
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ws.on('close',function(){
|
2016-04-11 06:13:35 +00:00
|
|
|
if(!clients[this.wsid]) return;
|
2016-04-11 01:51:47 +00:00
|
|
|
if(this.owner){
|
2016-04-11 06:13:35 +00:00
|
|
|
this.owner.sendl('onclose',this.wsid);
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
var room=this.room;
|
|
|
|
if(room&&room.owner==this){
|
|
|
|
room.owner=null;
|
|
|
|
room.config=null;
|
2016-04-17 06:00:26 +00:00
|
|
|
room.servermode=false;
|
2016-04-11 01:51:47 +00:00
|
|
|
for(var i in clients){
|
|
|
|
if(clients[i].room==room&&clients[i]!=this){
|
|
|
|
clients[i].close();
|
2016-04-11 06:13:35 +00:00
|
|
|
delete clients[i];
|
2016-04-11 01:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 08:33:58 +00:00
|
|
|
delete clients[this.wsid];
|
2016-04-11 06:13:35 +00:00
|
|
|
util.updaterooms();
|
2016-04-11 01:51:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}());
|