Je n'arrive aps a faire le tuto django channels
Bonjour, je n'ai jamais utilisé Django Channels, c'est une découverte pour moi, j'ai donc décidé de suivre les tutos dans la doc officielle, j'en suis au 2 ème, je vous met le lien : https://channels.readthedocs.io/en/stable/tutorial/part_2.html
J'ai suivi exactement ce qu'il y a dans le tuto, mais ca ne marche pas.
je vous partage mes fichiers
chat/consumers.py :
# chat/consumers.py
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
class ChatConsumer(WebsocketConsumer):
def connect(self):
print("ChatConsumer.connect")
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
self.room_group_name = "chat_%s" % self.room_name
# Join room group
async_to_sync(self.channel_layer.group_add)(
self.room_group_name, self.channel_name
)
self.accept()
def disconnect(self, close_code):
print("ChatConsumer.disconnect")
# Leave room group
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name, self.channel_name
)
# Receive message from WebSocket
def receive(self, text_data):
print("ChatConsumer.receive")
text_data_json = json.loads(text_data)
message = text_data_json["message"]
# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name, {"type": "chat_message", "message": message}
)
# Receive message from room group
def chat_message(self, event):
print("ChatConsumer.chat_message")
message = event["message"]
# Send message to WebSocket
self.send(text_data=json.dumps({"message": message}))
chat/routing.py:
# chat/routing.py
from django.urls import re_path
from .consumers import ChatConsumer
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<room_name>\w+)/$', ChatConsumer.as_asgi(), name='chat'),
]
project_config/asgi.py:
"""
ASGI config for project_config project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application
from chat.routing import websocket_urlpatterns
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": AllowedHostsOriginValidator(
AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
),
})
message dans le terminal :
[17/Dec/2023 12:33:38] "GET /chat/lobby/ HTTP/1.1" 200 1660
Not Found: /favicon.ico
Not Found: /ws/chat/lobby/
[17/Dec/2023 12:33:38,771] - Broken pipe from ('127.0.0.1', 64331)
[17/Dec/2023 12:33:38] "GET /ws/chat/lobby/ HTTP/1.1" 404 2232
merci pour votre aide ! :)
Inscris-toi
(c'est gratuit !)
Tu dois créer un compte pour participer aux discussions.
Créer un compte