Reconnection of Client when server reboots in WebSocket

后端 未结 9 2238
青春惊慌失措
青春惊慌失措 2020-12-02 05:03

I am using web socket using PHP5 and the Chrome browser as client. I have taken the code from the site http://code.google.com/p/phpwebsocket/.

I run the server, and

9条回答
  •  囚心锁ツ
    2020-12-02 05:53

    ReconnectingWebSocket

    GitHub hosts a small JavaScript library that decorates the WebSocket API to provide a WebSocket connection that will automatically reconnect if the connection is dropped.

    Minified library with gzip compression is less than 600 bytes.

    The official repository is available here:

    https://github.com/joewalnes/reconnecting-websocket

    Server flood

    If a high number of clients are connected to the server when it reboots. It may be worthwhile to manage the reconnect timings of the clients by using an Exponential Backoff algorithm.

    The algorithm works like this:

    1. For k attempts, generate a random interval of time between 0 and 2^k - 1,
    2. If you are able to reconnect, reset k to 1,
    3. If reconnection fails, k increases by 1 and the process restarts at step 1,
    4. To truncate the max interval, when a certain number of attempts k has been reached, k stops increasing after each attempt.

    Référence:

    http://blog.johnryding.com/post/78544969349/how-to-reconnect-web-sockets-in-a-realtime-web-app

    ReconnectingWebSocket does not handle reconnections by using this algorithm.

提交回复
热议问题