2017-10-15 135 views
0

我正在学习如何使用websockets。我通过正常的ws.send将数据返回到服务器的前端,但我同时希望有一个设定的间隔时间来ping前端,以查看是否存在clints仍然存在..这是我目前使用的代码。带节点js的websocket ping

ws.on('connection', function connection(ws, req) { 

    ws.on('message', function incoming(message) { 
    return_data = message; 
    heart_beat = {status:"Accepted", currentTime:"2013-02-01T20:53:32.486Z", "heartbeatInterval":300} 

    ws.send(JSON.stringify(heart_beat)); 
    const interval = setInterval(function ping() { 
    ws.clients.forEach(function each(ws) { 
     if (ws.isAlive === false) return ws.terminate(); 

     ws.isAlive = false; 
     ws.ping('', false, true); 
     }); 
    }, 300); 
    }); 
}); 

我只想等待300秒,如果没有从前端响应由我

这里是我的前端代码发送第一个心跳后,我将终止WebSocket的。

ws.onopen = function(){ 
    console.log("Connected"); 
} 
ws.onclose = function(){ 
    console.log("Disconnected"); 
    ws.send('Disconnected'); 
} 

请告诉我现在发生的事情是,当我关闭在前端的浏览器在后端服务器仍然可以ping通,如果我CONSOLE.LOG它。

+0

你所说的'客户端的意思是被还活着吗? –

+0

@BrahmaDev客户端(在浏览器中) - 如果他们关闭了浏览器,他们不在了,那就是我的意思。 –

+0

如果他们关闭浏览器,websocket连接也不见了。你不应该再在'ws.clients'中找到它们。 –

回答

0

你要清楚的超时与clearInterval

这里与uWebSocket工作例如:

'use strict'; 

const uws = require('uws'); 

const PORT = 3000 


/********************************************************************* 
*        Server        * 
*********************************************************************/ 

var wsServer = new uws.Server({ 'port': PORT }); 

let nextId=1; 

wsServer.on('connection', function(ws) { 
    console.log('connection !'); 

    ws.id='cnx'+nextId++; 

    ws.on('message', function(mess){console.log('message : '+mess); }); 

    ws.on('error', function(error) { 
     console.log('Cannot start server'); 
    }); 

    ws.on('close', function(code, message) { 
      console.log('Disconnection: ' + code + ', ' + message); 
      clearInterval(ws.timer); 
      }); 

    ws.on('pong',function(mess) { console.log(ws.id+' receive a pong : '+mess); }); 

    ws.timer=setInterval(function(){pingpong(ws);},1000); 

}); 

function pingpong(ws) { 
    console.log(ws.id+' send a ping'); 
    ws.ping('coucou',{},true); 
} // end of pingpong 


/********************************************************************* 
*        Client        * 
*********************************************************************/ 

var wsClient1 = createClient('client1'); 
var wsClient2 = createClient('client2'); 

function createClient(id) { 
    var client = new uws('ws://localhost:'+PORT); 
    client.id=id; 
    client.on('ping',function(mess){ console.log(this.id+' receive a ping : '+mess); }); 
    client.on('message',function(mess){ console.log(this.id+' receive a message : '+mess); }); 
    client.on('close',function(){ console.log(this.id+' closed'); }); 
    return client; 
} 

function closeClient(client) { 
    console.log('close '+client.id); client.close(); 
} 

setTimeout(function(){ closeClient(wsClient1); closeClient(wsClient2); wsServer.close(); },2500); 

输出为:

connection ! 
connection ! 
cnx1 send a ping 
cnx2 send a ping 
client2 receive a ping : coucou 
client1 receive a ping : coucou 
cnx1 receive a pong : coucou 
cnx2 receive a pong : coucou 
cnx1 send a ping 
cnx2 send a ping 
client2 receive a ping : coucou 
client1 receive a ping : coucou 
cnx1 receive a pong : coucou 
cnx2 receive a pong : coucou 
close client1 
close client2 
client1 closed 
client2 closed 
Disconnection: 0, 
Disconnection: 0,