简体   繁体   English

在 Django 通道上找不到路径

[英]No path found on Django Channels

I created a simple consumer on my Django channels application, but when i try to connect to the websocket from my frontend, i keep getting the following error:我在我的 Django 频道应用程序上创建了一个简单的消费者,但是当我尝试从我的前端连接到 websocket 时,我不断收到以下错误:

ws_protocol: ERROR - [Failure instance: Traceback: <class 'ValueError'>: No route found for path 'messages/127.0.0.1:8000/messages/'.

Here is my routing: myapp>routing.py这是我的路由: myapp>routing.py

from .consumers import EchoConsumer

websocket_urlpatterns = [
    path("messages/", EchoConsumer),
]

mysite>routing.py我的站点>路由.py

# mysite/routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import myapp.routing

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            myapp.routing.websocket_urlpatterns
        )
    ),
})

And here is how i'm trying to connect to the websocket from my frontend:这是我尝试从我的前端连接到 websocket 的方式:

var wsStart = 'ws://' + window.location.host + window.location.pathname

Can anyone help me find what i'm doing wrong, please?谁能帮我找出我做错了什么,好吗?

You are using path instead of url, just try this in routing.py:您使用的是路径而不是 url,只需在 routing.py 中尝试:

from .consumers import EchoConsumer
from django.conf.urls import url

websocket_urlpatterns = [
    url("messages/", EchoConsumer),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM