How configure reconnecting in socket.io?

前端 未结 4 1790
孤独总比滥情好
孤独总比滥情好 2021-02-02 08:40

I create socket.io connection with next code

var socket = new io.connect(\'http://localhost:8181\', {
    \'reconnect\': true,
    \'reconnection delay\': 500,
          


        
4条回答
  •  星月不相逢
    2021-02-02 09:17

    I realise this is an old question, but I've been having some trouble with socket io reconnecting and found this post high in the search results, so thought I would contribue. Try debugging exactly which events are firing using the following code:

    # coffeescript. compile if you're writing javascript, obviously.
    
    socket.on 'connect',-> console.log 'connected'
    socket.on 'reconnect',-> console.log 'reconnect'
    socket.on 'connecting',-> console.log 'connecting'
    socket.on 'reconnecting',-> console.log 'reconnecting'
    socket.on 'connect_failed',-> console.log 'connect failed'
    socket.on 'reconnect_failed',-> console.log 'reconnect failed'
    socket.on 'close',-> console.log 'close'
    socket.on 'disconnect',-> console.log 'disconnect'
    

    This should give you more insight into the state of the client socket.

    Also, try looking in the Network tab of your web inspector to see if it is firing XHR requests as a fallback. Finally, in your web console, try typing io.sockets and expand it out to see whether it is actually trying to reconnect or not.

    I have encountered problems with reconnect_failed not firing, and the reconnect tally not resetting. The following are links to discussions of these issues on github.

    reconnection delay - exponential back off not resetting properly

    reconnect_failed gets never fired

    some potential fixes/workarounds

提交回复
热议问题