Node.js and Socket.IO - How to reconnect as soon as disconnect happens

前端 未结 4 1254
星月不相逢
星月不相逢 2020-12-04 06:29

I\'m building a small prototype with node.js and socket.io. Everything is working well, the only issue I\'m facing is that my node.js connection will disconnect and I\'m fo

4条回答
  •  被撕碎了的回忆
    2020-12-04 06:39

    Start reconnecting even if the first attempt fails

    If the first connection attempt fails, socket.io 0.9.16 doesn't try to reconnect for some reason. This is how I worked around that.

    //if this fails, socket.io gives up
    var socket = io.connect();
    
    //tell socket.io to never give up :)
    socket.on('error', function(){
      socket.socket.reconnect();
    });
    

提交回复
热议问题