简体   繁体   English

心跳上的Socket.io事件

[英]Socket.io event on heartbeat

Socket.IO uses heartbeat pattern to be sure that a client is still connected. Socket.IO使用心跳模式来确保客户端仍处于连接状态。 It would solve my algorithm if I would be able to subscribe to the hearbeat event on server. 如果我能够订阅服务器上的listenbeat事件,它将解决我的算法。

for instance: socket.on "heartbeat", -> mycode 例如:socket.on“ heartbeat”,-> mycode

any idea if this is possible? 知道这是否可能吗?

The heartbeat happens very frequently, are you sure that's the right thing? 心跳非常频繁地发生,您确定这是对的吗? I use the "connect" event, that seems like it might do what you need: 我使用“连接”事件,看来它可以满足您的需求:

  connect: (url, options, connectCallback) =>
    someFunction = =>
      @doSomeRedisStuff() if @connected

    @logger.debug "starting connection to url: #{url}"
    @socket = @socket.connect(url, options)
    @logger.debug "connecting ..."
    @socket.on "connect", =>    
      @logger.info "connected (socket id #{(@socket.socket.sessionid)})"
      @connected = true 
      setTimeout someFunction, 2000
      connectCallback() if connectCallback?
     @socket.on "disconnect", =>
       @connected = false 

If the connection to the server is interrupted (eg the server is restarted) the connect event will kick off when the connection comes back. 如果与服务器的连接中断(例如,服务器已重新启动),则当连接恢复时,连接事件将开始。 Socket.IO is smart about this. Socket.IO对此很聪明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM