'WebSocketProtocol' object has no attribute 'application_queue' 错误 Django Channels

'WebSocketProtocol' object has no attribute 'application_queue' error Django Channels

在我的 Django 项目中,我想在学习本教程时使用 channels.But 创建一个聊天应用程序: https://channels.readthedocs.io/en/stable/tutorial/part_2.html,我遇到了一个问题,即连接后 websocket 自动断开连接:

 Exception in callback AsyncioSelectorReactor.callLater.<locals>.run()
at
 C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\twisted\internet\asyncioreact
 or.py:287 handle: <TimerHandle when=19405.993
 AsyncioSelectorReactor.callLater.<locals>.run() at
 C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\twisted\interne
 t\asyncioreactor.py:287> Traceback (most recent call last):   File
 "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py",
 line 145, in _run
     self._callback(*self._args)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\twisted\internet\asyncioreactor.py",
 line 290, in run
     f(*args, **kwargs)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\twisted\internet\tcp.py",
 line 289, in connectionLost
     protocol.connectionLost(reason)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\autobahn\twisted\websocket.py",
 line 128, in connectionLost
     self._connectionLost(reason)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\autobahn\websocket\protocol.py",
 line 2467, in _connectionLost
     WebSocketProtocol._connectionLost(self, reason)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\autobahn\websocket\protocol.py",
 line 1096, in _connectionLost
     self._onClose(self.wasClean, WebSocketProtocol.CLOSE_STATUS_CODE_ABNORMAL_CLOSE, "connection was
 closed uncleanly (%s)" % self.wasNotCleanReason)   File
 "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\autobahn\twisted\websocket.py",
 line 171, in _onClose
     self.onClose(wasClean, code, reason)   File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\daphne\ws_protocol.py",
 line 146, in onClose
     self.application_queue.put_nowait({ AttributeError: 'WebSocketProtocol' object has no attribute 'application_queue'

此错误上方应该是路径列表,路径顶部是真正的错误。在我的例子中是路由 url.

迁移帮我解决了

python manage.py migrate

只是为了向所有在遵循我的频道教程时遇到此错误的人澄清错误是在我的路径中

2018-05-17 15:55:36,382 - ERROR - ws_protocol - [Failure instance: Traceback: : No route found for path 'ws/chat/me/'.

您可以在堆栈跟踪顶部找到它

我定义的无效 URL 模式 "buzz" 因为我喜欢改变现状。本质上它的工作方式与 django url 模式相同,因此只需要确保您的路由结构与您指定的相匹配。

websocket_urlpatterns = [
    url(r'^ws/buzz/(?P<room_name>[^/]+)/$', consumers.ChatConsumer),
]

在我的例子中,路由路径与我的 websocket 模式不匹配 我必须将路由路径更新为正确 -> 通过将我的 websocket_urlpatterns 改回聊天

websocket_urlpatterns = [
    url(r'^ws/chat/(?P<room_name>[^/]+)/$', consumers.ChatConsumer),
]

所以系统可以解决我的问题。希望它能帮助 运行 将来参与其中的人。祝你好运。 P.T.