即使套接字断开连接,socketio也会触发事件

时间:2019-05-07 19:04:49

标签: node.js socket.io

我有一台每秒发射一次的服务器。一个timeupdate事件。

当某些客户端连接到服务器时,我启动了10秒钟的socket.disconnect(true)...

socket.on('disconnect')告诉我(10秒后)其断开连接 浏览器/网页也会在10秒后停止更新时间

但是'timeUpdate'中的console.log()仍然在所有套接字上触发吗?!

setInterval(function(){
   now = new Date();
   myEmitter.emit('timeUpdate');
 }, 1000);

 ...
 ...

 io.on('connection', (socket) => { 
  var clientIp = socket.request.connection.remoteAddress;
  console.log(now+': Client connected ID: '+socket.id+' - IP: '+clientIp+''.green);  


  setTimeout(() => socket.disconnect(true), 10000);

  socket.on('disconnect', (reason) => {
     console.log(reason);
   });



   myEmitter.on('timeUpdate', function(msg){
    socket.emit('timeUpdate', now);
    console.log(now+': ad: '+socket.id+' - IP: '+clientIp+''.green);  
  });

});

192.168.178.27
Tue May 07 2019 09:26:55 GMT+0200 (GMT+02:00): ad: KKEtFsvMuBYj3IHfAAAB - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:55 GMT+0200 (GMT+02:00): ad: gmCJHquzy0t8cs2FAAAC - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:55 GMT+0200 (GMT+02:00): ad: wxv8sp82lbzX_Qs4AAAD - IP: ::ffff:192.168.178.27
server namespace disconnect
Tue May 07 2019 09:26:56 GMT+0200 (GMT+02:00): ad: Wnm9lqwuBf3gYY9yAAAA - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:56 GMT+0200 (GMT+02:00): ad: KKEtFsvMuBYj3IHfAAAB - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:56 GMT+0200 (GMT+02:00): ad: gmCJHquzy0t8cs2FAAAC - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:56 GMT+0200 (GMT+02:00): ad: wxv8sp82lbzX_Qs4AAAD - IP: ::ffff:192.168.178.27
server namespace disconnect
server namespace disconnect
Tue May 07 2019 09:26:57 GMT+0200 (GMT+02:00): ad: Wnm9lqwuBf3gYY9yAAAA - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:57 GMT+0200 (GMT+02:00): ad: KKEtFsvMuBYj3IHfAAAB - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:57 GMT+0200 (GMT+02:00): ad: gmCJHquzy0t8cs2FAAAC - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:57 GMT+0200 (GMT+02:00): ad: wxv8sp82lbzX_Qs4AAAD - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:58 GMT+0200 (GMT+02:00): ad: Wnm9lqwuBf3gYY9yAAAA - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:58 GMT+0200 (GMT+02:00): ad: KKEtFsvMuBYj3IHfAAAB - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:58 GMT+0200 (GMT+02:00): ad: gmCJHquzy0t8cs2FAAAC - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:58 GMT+0200 (GMT+02:00): ad: wxv8sp82lbzX_Qs4AAAD - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:59 GMT+0200 (GMT+02:00): ad: Wnm9lqwuBf3gYY9yAAAA - IP: ::ffff:192.168.178.27
Tue May 07 2019 09:26:59 GMT+0200 (GMT+02:00): ad: KKEtFsvMuBYj3IHfAAAB - IP: ::ffff:192.168.178.27

0 个答案:

没有答案