This commit is contained in:
libccy 2016-04-11 16:49:00 +08:00
parent eb96185d8b
commit 6770fb3371
1 changed files with 19 additions and 9 deletions

View File

@ -38,8 +38,13 @@
},
send:function(id,message){
if(clients[id]&&clients[id].owner==this){
try{
clients[id].send(message);
}
catch(e){
clients[id].close();
}
}
},
close:function(id){
if(clients[id]&&clients[id].owner==this){
@ -49,12 +54,16 @@
};
var util={
sendl:function(){
if(this.closed) return;
var args=[];
for(var i=0;i<arguments.length;i++){
args.push(arguments[i]);
}
try{
this.send(JSON.stringify(args));
}
catch(e){
this.close();
}
},
getid:function(){
return (Math.floor(1000000000+9000000000*Math.random())).toString();
@ -96,17 +105,19 @@
clients[ws.wsid]=ws;
ws.sendl('roomlist',util.getroomlist());
ws.heartbeat=setInterval(function(){
if(ws.closed){
clearInterval(ws.heartbeat);
}
else if(ws.beat){
if(ws.beat){
ws.close();
clearInterval(ws.heartbeat);
}
else{
ws.beat=true;
try{
ws.send('heartbeat');
}
catch(e){
ws.close();
}
}
},60000);
ws.on('message',function(message){
if(message=='heartbeat'){
@ -137,7 +148,6 @@
});
ws.on('close',function(){
if(!clients[this.wsid]) return;
this.closed=true;
if(this.owner){
this.owner.sendl('onclose',this.wsid);
}