I am using Django websocket for making a real video chat app and I am beginner in Websocket.
I/m trying that user first give the room name and then the username and then it will be connected to the meeting room. I’ve written it like below
For Routing.py
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/(?P<room_name>w+)/(?P<name>w+)/$', consumers.ChatVideoConsumer.as_asgi())
]
For urls.py
from django.urls import path
from . import views
urlpatterns = [
path('<str:room_name>/<str:name>/', views.index, name='index'),
]
For Views.py
from django.shortcuts import render
# Create your views here.
def index(request, *args, **kwargs):
return render(request, "chat/room.html")
For Room.js
console.log("Hello World from room.js");
console.log(window.location.pathname);
let wsStart = "ws://";
const loc = window.location;
if(loc.protocol === "https:") {
wsStart = "wss://";
}
const endpoint = wsStart + loc.host + loc.pathname;
console.log("Endpoint", endpoint);
const websocket = new WebSocket(endpoint);
websocket.onopen = (e) => {
console.log("Open", e);
};
// websocket.onmessage = (e) => {
// console.log("Message", e);
// };
websocket.onclose = (e) => {
console.log("Close", e);
};
For Consumers.py
from channels.generic.websocket import AsyncWebsocketConsumer
import json
class ChatVideoConsumer(AsyncWebsocketConsumer):
async def connect(self):
print("connect")
await self.accept()
self.send(text_data=json.dumps({
"type" : "Message.Accept",
"message" : "Hello Connect Established"
}))
async def disconnect(self, close_code):
pass
asgi.py
import os
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
import chat.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
application = ProtocolTypeRouter({
"http": django_asgi_app,
# Just HTTP for now. (We can add other protocols later.)
"websocket" : AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns
)
)
})
Error Occuring:
WebSocket HANDSHAKING /yash/yash/ [127.0.0.1:43816]
Exception inside application: No route found for path 'yash/yash/'.
Traceback (most recent call last):
File "/media/groot/New Volume/Myprojects/Django/Video_Chat_App/.venv/lib/python3.11/site-packages/django/contrib/staticfiles/handlers.py", line 101, in __call__
return await self.application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/groot/New Volume/Myprojects/Django/Video_Chat_App/.venv/lib/python3.11/site-packages/channels/routing.py", line 62, in __call__
return await application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/groot/New Volume/Myprojects/Django/Video_Chat_App/.venv/lib/python3.11/site-packages/channels/sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/groot/New Volume/Myprojects/Django/Video_Chat_App/.venv/lib/python3.11/site-packages/channels/sessions.py", line 263, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/groot/New Volume/Myprojects/Django/Video_Chat_App/.venv/lib/python3.11/site-packages/channels/auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/groot/New Volume/Myprojects/Django/Video_Chat_App/.venv/lib/python3.11/site-packages/channels/middleware.py", line 24, in __call__
return await self.inner(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/groot/New Volume/Myprojects/Django/Video_Chat_App/.venv/lib/python3.11/site-packages/channels/routing.py", line 134, in __call__
raise ValueError("No route found for path %r." % path)
ValueError: No route found for path 'yash/yash/'.
WebSocket DISCONNECT /yash/yash/ [127.0.0.1:43816]
Kindly Give me solution for it.
Please.