Is it safe to set a high close timeout on socket.i

2019-02-05 00:14发布

I have a web application where the user needs to be constantly connected. By default, socket.io will disconnect the connection after 60 seconds. I have 'reconnection' turned on though, so it is essentially closing and reopening the connection every minute. This can cause issues with feeds/notifications to my connected clients. Would it be safe to set this timeout to lets say, 10 minutes, or possibly higher? Is there a reason it is so low right now?

3条回答
Ridiculous、
2楼-- · 2019-02-05 00:24

You might have already figured this out, but your socket might be disconnecting after 60 seconds because you're not sending a heartbeat ("2::") back to the server.

Here's some Python code that works with the websocket client module.

# on_message handles messages from the server
def on_message(ws, message):
    if message[:3] == '2::':
        ws.send('2::')
查看更多
Luminary・发光体
3楼-- · 2019-02-05 00:28

I don't believe your socket should disconnect after 60 seconds. I would investigate why that is actually happening. After handshaking correctly the socket should heartbeat and stay open indefinitely (barring network issues out of your control) until either the client or the server closes the connection, that is definitely my experience.

The fact that your connection is actually closing sounds like it may not be handshaking correctly, or heartbeats are not being received.

查看更多
Juvenile、少年°
4楼-- · 2019-02-05 00:44

My guess is that you may be misinterpreting the 'close timeout' configuration. It does not cause the connection to be closed after 60 seconds. (Heartbeats would be pointless if clients constantly reconnected).

If a client disconnects, close timeout is the amount of time the server will wait before releasing resources associated with that connection. Essentially, this allows clients with intermittent connectivity issues to attempt to reconnect before the server has forgotten about them. Setting close timeout to ten minutes is probably a bad idea since it will tie up server resources.

If your clients are, in fact, disconnecting every 60 seconds, then, like samjm said, something else is wrong.

查看更多
登录 后发表回答