diff --git a/core/chat/consumers.py b/core/chat/consumers.py new file mode 100644 index 0000000..c81bf2e --- /dev/null +++ b/core/chat/consumers.py @@ -0,0 +1,18 @@ +# chat/consumers.py +import json + +from channels.generic.websocket import WebsocketConsumer + + +class ChatConsumer(WebsocketConsumer): + def connect(self): + self.accept() + + def disconnect(self, close_code): + pass + + def receive(self, text_data): + text_data_json = json.loads(text_data) + message = text_data_json["message"] + + self.send(text_data=json.dumps({"message": message})) \ No newline at end of file diff --git a/core/chat/routing.py b/core/chat/routing.py new file mode 100644 index 0000000..4c48d8f --- /dev/null +++ b/core/chat/routing.py @@ -0,0 +1,8 @@ +# chat/routing.py +from django.urls import re_path + +from . import consumers + +websocket_urlpatterns = [ + re_path(r"ws/chat/(?P\w+)/$", consumers.ChatConsumer.as_asgi()), +] \ No newline at end of file diff --git a/core/chat/urls.py b/core/chat/urls.py index 96bbee4..7fd818a 100644 --- a/core/chat/urls.py +++ b/core/chat/urls.py @@ -5,4 +5,5 @@ from . import views urlpatterns = [ path("", views.index, name="index"), + path("/", views.room, name="room"), ] \ No newline at end of file diff --git a/core/chat/views.py b/core/chat/views.py index 751e781..dc9fffb 100644 --- a/core/chat/views.py +++ b/core/chat/views.py @@ -2,4 +2,7 @@ from django.shortcuts import render def index(request): - return render(request, "chat/index.html") \ No newline at end of file + return render(request, "chat/index.html") + +def room(request, room_name): + return render(request, "chat/room.html", {"room_name": room_name}) diff --git a/core/core/asgi.py b/core/core/asgi.py index 8a1496f..8c68abf 100644 --- a/core/core/asgi.py +++ b/core/core/asgi.py @@ -9,6 +9,10 @@ https://docs.djangoproject.com/en/5.1/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 django.core.asgi import get_asgi_application from channels.routing import ProtocolTypeRouter @@ -16,9 +20,15 @@ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') #application = get_asgi_application() +django_asgi_app = get_asgi_application() + +from chat.routing import websocket_urlpatterns + application = ProtocolTypeRouter( { - "http": get_asgi_application(), - # Just HTTP for now. (We can add other protocols later.) + "http": django_asgi_app, + "websocket": AllowedHostsOriginValidator( + AuthMiddlewareStack(URLRouter(websocket_urlpatterns)) + ), } ) \ No newline at end of file diff --git a/core/templates/chat/room.html b/core/templates/chat/room.html new file mode 100644 index 0000000..667c80c --- /dev/null +++ b/core/templates/chat/room.html @@ -0,0 +1,50 @@ + + + + + + Chat Room + + +
+
+ + {{ room_name|json_script:"room-name" }} + + + \ No newline at end of file