我使用websocket_server
来提供单向(服务器到客户端)websocket连接。
我在服务器上有几个线程,它们以给定的时间间隔(while True: ... time.sleep(60)
)查询API,然后执行server.send_message()
调用来更新客户端。所有这一切都很好。
有时候,没有任何特别的原因,我会崩溃:
Exception in thread Thread-3:
Traceback (most recent call last):
File "C:\Python35\lib\threading.py", line 914, in _bootstrap_inner
self.run()
File "C:\Python35\lib\threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "D:/Dropbox/dev/domotique/webserver.py", line 266, in calendar
server.send_message(client, json.dumps({"calendar": events}))
File "C:\Python35\lib\site-packages\websocket_server\websocket_server.py", line 71, in send_message
self._unicast_(client, msg)
File "C:\Python35\lib\site-packages\websocket_server\websocket_server.py", line 119, in _unicast_
to_client['handler'].send_message(msg)
File "C:\Python35\lib\site-packages\websocket_server\websocket_server.py", line 194, in send_message
self.send_text(message)
File "C:\Python35\lib\site-packages\websocket_server\websocket_server.py", line 240, in send_text
self.request.send(header + payload)
BrokenPipeError: [WinError 10058] A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call
我的代码中没有关机调用。 还有什么可以关闭websocket?
答案 0 :(得分:2)
WebSocket客户端可以要求服务器关闭连接(或直接关闭它)。来自the library's code:
if not b1:
logger.info("Client closed connection.")
self.keep_alive = 0
return
if opcode == CLOSE_CONN:
logger.info("Client asked to close connection.")
self.keep_alive = 0
return
您可以检查self.keep_alive
以了解套接字是否仍处于打开状态。