Basestructure Chat App
This commit is contained in:
18
core/chat/consumers.py
Normal file
18
core/chat/consumers.py
Normal file
@@ -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}))
|
||||
8
core/chat/routing.py
Normal file
8
core/chat/routing.py
Normal file
@@ -0,0 +1,8 @@
|
||||
# chat/routing.py
|
||||
from django.urls import re_path
|
||||
|
||||
from . import consumers
|
||||
|
||||
websocket_urlpatterns = [
|
||||
re_path(r"ws/chat/(?P<room_name>\w+)/$", consumers.ChatConsumer.as_asgi()),
|
||||
]
|
||||
@@ -5,4 +5,5 @@ from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.index, name="index"),
|
||||
path("<str:room_name>/", views.room, name="room"),
|
||||
]
|
||||
@@ -2,4 +2,7 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
def index(request):
|
||||
return render(request, "chat/index.html")
|
||||
return render(request, "chat/index.html")
|
||||
|
||||
def room(request, room_name):
|
||||
return render(request, "chat/room.html", {"room_name": room_name})
|
||||
|
||||
Reference in New Issue
Block a user