我正在使用websocket-client连接到一个WebSocket服务器。我连接的服务器似乎希望我定期向它发送ping,否则它会将我断开连接。
当我连接到服务器时,它会立即向我发送此消息:
0{"sid":"SomeID","upgrades":[],"pingInterval":25000,"pingTimeout":60000}
这似乎告诉我ping间隔和ping超时时间。我注意到我的websocket客户端在连接后大约1分25秒后会不断地断开连接。如果将这些数字相加,60秒+25秒,就得到了1分25秒。所以看来我需要定期ping这个服务器,以便它不会让我断开连接。
如何ping服务器?我尝试了ws.ping()
,但似乎不存在。我需要以某种格式向服务器发送数据吗?还是有一些内置的ping命令?
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://socket.serverssite.com/socket.io/?transport=websocket",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
注意:我在node中创建了一个websocket客户端(不是python),它在1分25秒后仍未关闭。因此,似乎它具有一些内置的ping,这个python websocket客户端没有...
------------------------编辑-------------------------
尝试设置ping_interval和ping_timeout,但它仍然关闭:
2017-11-06 12:49:14.340037--------------------- Doing stuff
2017-11-06 12:49:14.340309--------------------- Doing stuff
send: '\x89\x80\\\xd9\xc4\xdd'
2017-11-06 12:49:19.341680--------------------- Doing stuff
2017-11-06 12:49:19.341958--------------------- Doing stuff
send: '\x89\x80\xd9\x06\xef\xa8'
2017-11-06 12:49:24.343426--------------------- Doing stuff
2017-11-06 12:49:24.343769--------------------- Doing stuff
send: "\x89\x80\xe6\x92'\xb8"
send: '\x88\x823\xfb$\xd10\x13'
closed (Here is where the closed method gets called, the server shut me down)