简体   繁体   English

Socket.io 与flask-socketio python。 如何设置套接字保持活动/超时

[英]Socket.io with flask-socketio python. How to set socket keepalive/timeout

I am struggling to find any documentation about a timeout value for socket.io.我正在努力寻找有关 socket.io 超时值的任何文档。 I am using //cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js on the client and Flask-SocketIO on the server side.我在客户端使用 //cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js 在服务器端使用 Flask-SocketIO。

Here is how I am creating the socket:这是我创建套接字的方式:

namespace = '/coregrapher'

var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);

socket.on('connect', function() {
    socket.emit('my event', {data: 'I\'m connected!'});
});

socket.on('my response', function(msg) {
    $('#result').append(msg.data);
});

The issue is, if the server does not send anything to the client or visaversa for even one minute, the client disconnects and if the server tries to do another emit to the client, it fails because the client has already disconnected.问题是,如果服务器甚至一分钟都没有向客户端或 Visaversa 发送任何内容,则客户端会断开连接,如果服务器尝试向客户端再次发送,则会失败,因为客户端已经断开连接。 How can I make the client stay connected?如何让客户端保持连接?

Thanks!谢谢!

You can also set parameters in the server-side with Flask-SocketIO:您还可以使用 Flask-SocketIO 在服务器端设置参数:

socketio = SocketIO(ping_timeout=10, ping_interval=5)

:param ping_timeout: The time in seconds that the client waits for the
                     server to respond before disconnecting. The default is
                     60 seconds.
:param ping_interval: The interval in seconds at which the client pings
                      the server. The default is 25 seconds.

The big picture issue is that if your server is non-responsive to keep-alive packets for some extended period of time, the client will drop the connection and try to reconnect.大问题是,如果您的服务器在一段时间内对保持活动的数据包没有响应,客户端将断开连接并尝试重新连接。 If it cannot reconnect, eventually, it will stop trying.如果它无法重新连接,最终它将停止尝试。

That said, if you want to modify the configuration of the retry logic, then you can send an options object as the 2nd argument to your .connect() call.也就是说,如果您想修改重试逻辑的配置,那么您可以将选项对象作为第二个参数发送到您的.connect()调用。 Per the documentation here , there is control over the following options:根据此处的文档,可以控制以下选项:

Options:选项:

  • reconnection whether to reconnect automatically (true) reconnection 是否自动重连(true)
  • reconnectionDelay how long to wait before attempting a new reconnection (1000) reconnectionDelay 在尝试新的重新连接之前等待多长时间 (1000)
  • reconnectionDelayMax maximum amount of time to wait between reconnections (5000). reconnectionDelayMax 重新连接之间等待的最长时间 (5000)。 Each attempt increases the reconnection by the amount specified by reconnectionDelay.每次尝试都会按 reconnectionDelay 指定的数量增加重新连接。
  • timeout connection timeout before a connect_error and connect_timeout events are emitted (20000)发出 connect_error 和 connect_timeout 事件之前的超时连接超时(20000)

So, if you want it to keep trying to reconnect automatically for a much longer period of time, you can increase the times for the last three options.因此,如果您希望它在更长的时间内继续尝试自动重新连接,您可以增加最后三个选项的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM