WebSockets in Chrome and Firefox Disconnecting After One Minute of Inactivity

前端 未结 4 1757
陌清茗
陌清茗 2021-02-07 04:08

I have found that WebSockets in Chrome and Firefox disconnect after exactly one minute of inactivity. Based on stuff I\'ve seen online, I was all set to blame proxies or some s

4条回答
  •  逝去的感伤
    2021-02-07 04:26

    It seems like if sockets are disconnected by the server after one minute of inactivity that would apply to IE and Edge just as much as Chrome and Firefox.

    Hmmm, no, it doesn't. IE and Edge might be implementing a ping packet as part of the WebSocket protocol.

    The WebSocket protocol includes support for a protocol level ping that the JavaScript API doesn't expose. It's a bit lower-level than the user level pinging that is often implemented.

    This ping-pong traffic resets the timers in any network intermediaries (proxies, load balancers, etc') - and they all time connections to mark stale connections for closure (for example, the Heroku setup times connections at 55 seconds).

    Most browsers trust the server to implement the ping, which is polite (since servers need to manage their load and their timeout for pinging...

    ...however it's also slightly frustrating, since browsers have no idea if a connection was abnormally lost. This is why many JavaScript clients implement a user level ping (i.e., a JSON {event: "ping", data: {...}} or another "empty" event message).

    Anyway, I just wanted to point out that your assumption was incorrect, this is still a timeout occurring and the difference in browser behavior is probably related to the browsers themselves.

    For a few specifics regarding nginx default timeouts (when proxying WebSocket connections) you can read @Hendry's answer.

提交回复
热议问题