【发布时间】:2019-02-26 00:42:42
【问题描述】:
几个月来,我一直在愉快地使用 Django-Channels。但是,我在我的 Django 项目中添加了第二个依赖于 websocket 的应用程序,但我遇到了麻烦。
我得到的错误是websocket connection failed websocket is closed before the connection is established。奇怪的是,在部署第二个应用程序之前,第一个应用程序正在运行。此外,只要第二个应用程序没有运行,第一个应用程序就继续工作。
Django Channels documentation 说:
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 连接?可能吗?还是我只是配置不当?
【问题讨论】:
标签: django websocket django-channels