2017-10-15 68 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,