How configure reconnecting in socket.io?

前端 未结 4 1793
孤独总比滥情好
孤独总比滥情好 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

    This is an old question, but I had the same question (for a different reason) when using v1.4.5. My chat room app worked beautifully, but when I hit Ctrl+C in the terminal, my browser continued to loop and report ERR_CONNECTION_REFUSED every few seconds until I shut it down.

    Changing a previous answer just a bit gave me the solution.

    For v1.4.5, here is my original code for "var socket" in my client js file:

        var socket = io();
    

    And here is the solution:

        var socket = io({
            'reconnection': true,
            'reconnectionDelay': 1000,
            'reconnectionDelayMax' : 5000,
            'reconnectionAttempts': 5
        });
    

    Obviously I could change the values if I want, but the important point is that this killed the never ending reconnection requests.

提交回复
热议问题