This commit is contained in:
libccy 2016-04-11 16:33:58 +08:00
parent a59667881d
commit eb96185d8b
2 changed files with 25 additions and 3 deletions

View File

@ -11149,6 +11149,10 @@
} }
}, },
onmessage:function(messageevent){ onmessage:function(messageevent){
if(messageevent.data=='heartbeat'){
this.send('heartbeat');
return;
}
var message; var message;
try{ try{
message=JSON.parse(messageevent.data); message=JSON.parse(messageevent.data);

View File

@ -45,10 +45,11 @@
if(clients[id]&&clients[id].owner==this){ if(clients[id]&&clients[id].owner==this){
clients[id].close(); clients[id].close();
} }
} },
}; };
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]);
@ -94,8 +95,24 @@
ws.wsid=util.getid(); ws.wsid=util.getid();
clients[ws.wsid]=ws; clients[ws.wsid]=ws;
ws.sendl('roomlist',util.getroomlist()); ws.sendl('roomlist',util.getroomlist());
ws.heartbeat=setInterval(function(){
if(ws.closed){
clearInterval(ws.heartbeat);
}
else if(ws.beat){
ws.close();
clearInterval(ws.heartbeat);
}
else{
ws.beat=true;
ws.send('heartbeat');
}
},60000);
ws.on('message',function(message){ ws.on('message',function(message){
if(this.owner){ if(message=='heartbeat'){
this.beat=false;
}
else if(this.owner){
this.owner.sendl('onmessage',this.wsid,message); this.owner.sendl('onmessage',this.wsid,message);
} }
else{ else{
@ -120,6 +137,7 @@
}); });
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);
} }
@ -135,8 +153,8 @@
} }
} }
} }
delete clients[this.wsid];
} }
delete clients[this.wsid];
util.updaterooms(); util.updaterooms();
}); });
}); });