WebSocket 未连接|。 Django 频道 | WebSocket
我正在使用 WebsocketConsumer 消费者,当我断开 Websocket 连接时,会出现以下错误。 另外,当我已经有一个打开的连接并且当我尝试使用相同的路由创建连接时,它会断开连接并给出相同的
Application instance <Task pending name='Task-5' coro=<StaticFilesWrapper.__call__() running at /Volumes/Data/Projects/LocoNav-LM/vt-lm-server/venv/lib/python3.8/site-packages/channels/staticfiles.py:44> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/futures.py:360, <TaskWakeupMethWrapper object at 0x1076a2580>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 54789] path=b'/ws/users/'> took too long to shut down and was killed.
我的代码:-
class TestMultipleUsers(WebsocketConsumer):
def connect(self):
logging.info('connecting....')
self.accept()
redis_client = RedisClient(host='localhost', port=6379, db=0)
redis_client.subscribe_and_listen()
redis_client.subscribe_channel('test')
for message in redis_client.sub.listen():
if message is not None and isinstance(message, dict):
print(message)
self.send(text_data="1")
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
self.send(text_data=json.dumps({
'message': message
}))
def disconnect(self, close_code):
logging.info('disconnecting....')
logging.info(close_code)
但是,每当我从 Postman 断开 Web 套接字时,断开连接方法永远不会被调用。
requirements.txt
channels==3.0.4
Django==4.0
celery==5.2.3
这是我的日志:-
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO ] HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO ] Configuring endpoint tcp:port=8000:interface=127.0.0.1
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO ] Listening on TCP address 127.0.0.1:8000
WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145387810816 [INFO ] connecting....
WebSocket CONNECT /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket CONNECT /ws/users/ [127.0.0.1:60683]
WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:12 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60685]
WebSocket DISCONNECT /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:17 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket DISCONNECT /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:28 Process: 21283 Thread: 123145371021312 [WARNING ] Application instance <Task pending name='Task-7' coro=<StaticFilesWrapper.__call__() running at /Volumes/Data/Projects/LocoNav-LM/vt-lm-server/venv/lib/python3.8/site-packages/channels/staticfiles.py:44> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/futures.py:360, <TaskWakeupMethWrapper object at 0x10f495940>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 60685] path=b'/ws/users/'> took too long to shut down and was killed.
I am using the WebsocketConsumer
consumer and when I disconnect the websocket connection it gives the below error.
Also when I have already one open connection and when I try to create a connection with the same route it gets Disconnected and gives the same
Application instance <Task pending name='Task-5' coro=<StaticFilesWrapper.__call__() running at /Volumes/Data/Projects/LocoNav-LM/vt-lm-server/venv/lib/python3.8/site-packages/channels/staticfiles.py:44> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/futures.py:360, <TaskWakeupMethWrapper object at 0x1076a2580>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 54789] path=b'/ws/users/'> took too long to shut down and was killed.
My code:-
class TestMultipleUsers(WebsocketConsumer):
def connect(self):
logging.info('connecting....')
self.accept()
redis_client = RedisClient(host='localhost', port=6379, db=0)
redis_client.subscribe_and_listen()
redis_client.subscribe_channel('test')
for message in redis_client.sub.listen():
if message is not None and isinstance(message, dict):
print(message)
self.send(text_data="1")
def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
self.send(text_data=json.dumps({
'message': message
}))
def disconnect(self, close_code):
logging.info('disconnecting....')
logging.info(close_code)
However, whenever I disconnect the web socket from Postman the disconnect method never gets called.
requirements.txt
channels==3.0.4
Django==4.0
celery==5.2.3
Here are my Logs:-
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO ] HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO ] Configuring endpoint tcp:port=8000:interface=127.0.0.1
2022-02-27 20:28:06 Process: 21283 Thread: 123145371021312 [INFO ] Listening on TCP address 127.0.0.1:8000
WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145387810816 [INFO ] connecting....
WebSocket CONNECT /ws/users/ [127.0.0.1:60683]
2022-02-27 20:28:09 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket CONNECT /ws/users/ [127.0.0.1:60683]
WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:12 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket HANDSHAKING /ws/users/ [127.0.0.1:60685]
WebSocket DISCONNECT /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:17 Process: 21283 Thread: 123145371021312 [INFO ] WebSocket DISCONNECT /ws/users/ [127.0.0.1:60685]
2022-02-27 20:28:28 Process: 21283 Thread: 123145371021312 [WARNING ] Application instance <Task pending name='Task-7' coro=<StaticFilesWrapper.__call__() running at /Volumes/Data/Projects/LocoNav-LM/vt-lm-server/venv/lib/python3.8/site-packages/channels/staticfiles.py:44> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/futures.py:360, <TaskWakeupMethWrapper object at 0x10f495940>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 60685] path=b'/ws/users/'> took too long to shut down and was killed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论