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