When I was trying to connect my websocket then it is showing this error:
Error:
WebSocket HANDSHAKING /ws/play/testroomcode [127.0.0.1:57522] Exception inside application: call() missing 1 required positional argument: 'send'
The code inside my asgi.py :
import os
from django.urls import path
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from tictactoe.consumers import GameRoom
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'advancesource.settings')
application = get_asgi_application()
ws_pattern = [
path('ws/play/<room_code>', GameRoom)
]
application= ProtocolTypeRouter(
{
'websocket': AuthMiddlewareStack(URLRouter(
ws_pattern
))
}
)
The code inside my consumer.py :
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
import json
class GameRoom(WebsocketConsumer):
def connect(self):
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.groups_add)(
self.room_group_name,
self.channel_name
)
self.accept()
def disconnect(self, code):
async_to_sync(self.channel_layer.groups_discard)(
self.room_group_name,
self.channel_name
)
def receive(self, text_data):
print(text_data)
async_to_sync(self.channel_layer.groups_send)(
self.room_group_name,{
'type' : 'run_game',
'payload': text_data
}
)
In websocketking it is showing:
Could not connect to "ws://127.0.0.1:8000/ws/tictactoe/play/testroomcode". You may be able to find more information using Inspector/Dev Tools on this page.
I have found the remedy.
I have changed my asgi.py :
path('ws/play/<room_code>', GameRoom.as_asgi())
]