我已经愉快地使用 Django-Channels 几个月了。但是,我在 Django 项目中添加了第二个依赖于 websocket 的应用程序,但遇到了麻烦。
我得到的错误是websocket connection failed websocket is closed before the connection is established
。奇怪的是,第一个应用程序在部署第二个应用程序之前就已经运行了。此外,只要第二应用程序不运行,第一应用程序就继续工作。
The Django 频道文档 https://channels.readthedocs.io/en/latest/topics/routing.html#routing says:
Channels routers only work on the scope level, not on the level of individual events, which means you can only have one consumer for any given connection. Routing is to work out what single consumer to give a connection, not how to spread events from one connection across multiple consumers.
我认为这意味着 Django-Channels 不支持多个 websocket 连接的路由。也就是说,我想我正在尝试对两个不同的应用程序使用相同的 websocket 连接/端口。我的routing.py
文件如下所示:
application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(
URLRouter([
path("first_application/stream/", app_1_consumers.AsyncApp1),
path("second_application/stream/", app_2_consumers.AsyncApp2),
])
)
})
当我尝试使用下面的设置时,它找不到第一个应用程序的路径:
application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(
URLRouter([
path("second_application/stream/", app_2_consumers.AsyncApp2),
])
),
"websocket02": AuthMiddlewareStack(
URLRouter([
path("first_application/stream/", app_1_consumers.AsyncApp1),
])
),
})
如何设置我的 Django 应用程序以使用 Django-Channels 提供两个不同的 websocket 连接?是否可以?或者我只是配置不当?