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:
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
from .consumers import EchoConsumer
websocket_urlpatterns = [
path("messages/", EchoConsumer),
]
mysite>routing.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:
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:
from .consumers import EchoConsumer
from django.conf.urls import url
websocket_urlpatterns = [
url("messages/", EchoConsumer),
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.