Django Channels - Route not found

I have been grinding on this from past 4 days and still couldn't get the websockets connected , it always shows Not found : ws/play/testroomcode

This is my consumers.py:

from channels.generic.websocket 
import WebsocketConsumer from asgiref.sync 
import async_to_sync 
import json

class GameRoom(WebsocketConsumer):
    def connect(self,event):

    print('Websocket Received...',event)
    self.room_name = self.scope['url_route']['kwargs']['room_code']
    self.room_group_name = 'room_%s' % self.room_name
    print(self.room_group_name)

    async_to_sync(self.channel_layer.group_add)(
        self.room_group_name,
        self.channel_name
    )

    self.accept()

    def receive(self,event):
    print('Websocket Received...',event)

    def disconnect(self,event):
    print('Websocket Disconnected...',event)

game/asgi.py

import osfrom django.core.asgi 
import get_asgi_applicationfrom channels.routingimport ProtocolTypeRouter, URLRouterfrom channels.auth 
import AuthMiddlewareStackfrom app1 
import routing  # Import your routingfrom app1.consumers import GameRoomfrom django.urls import path

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'game.settings')websocket_urlpatterns = [path('ws/play/<room_code>', GameRoom.as_asgi()),]

application = ProtocolTypeRouter({'http': get_asgi_application(),'websocket': AuthMiddlewareStack(URLRouter(websocket_urlpatterns)),})

game/settings.py

INSTALLED_APPS = ['channels','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','app1',
] 

ASGI_APPLICATION = 'game.asgi.application' 
#WSGI_APPLICATION = 'game.wsgi.application'

 CHANNEL_LAYERS = {"default": {"BACKEND": "channels_redis.core.RedisChannelLayer","CONFIG": {"hosts": [("127.0.0.1", 6379)],  # Use quotes around the IP address},},}

I have tried following every YouTube tutorial nothing works for me, installed django, channel, channel-redis , virtual environment like every possible thing on internet but still got no where on how to connect it.

Read all the solutions on Stack overflow that have similar errors like mine but none of them works for me.

Вернуться на верх