From 394d74b553360a82f803a34205a8df609777fdff Mon Sep 17 00:00:00 2001 From: Bobby Date: Mon, 15 May 2023 07:30:20 -0400 Subject: Simple working live chat --- chat/__init__.py | 0 chat/admin.py | 3 +++ chat/apps.py | 6 ++++++ chat/consumers.py | 40 ++++++++++++++++++++++++++++++++++++++++ chat/migrations/__init__.py | 0 chat/models.py | 3 +++ chat/routing.py | 6 ++++++ chat/tests.py | 3 +++ chat/views.py | 3 +++ 9 files changed, 64 insertions(+) create mode 100644 chat/__init__.py create mode 100644 chat/admin.py create mode 100644 chat/apps.py create mode 100644 chat/consumers.py create mode 100644 chat/migrations/__init__.py create mode 100644 chat/models.py create mode 100644 chat/routing.py create mode 100644 chat/tests.py create mode 100644 chat/views.py (limited to 'chat') diff --git a/chat/__init__.py b/chat/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/chat/admin.py b/chat/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/chat/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/chat/apps.py b/chat/apps.py new file mode 100644 index 00000000..5f75238d --- /dev/null +++ b/chat/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ChatConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "chat" diff --git a/chat/consumers.py b/chat/consumers.py new file mode 100644 index 00000000..be1cf265 --- /dev/null +++ b/chat/consumers.py @@ -0,0 +1,40 @@ +import json +from channels.generic.websocket import WebsocketConsumer +from asgiref.sync import async_to_sync + +class ChatConsumer(WebsocketConsumer): + def connect(self): + self.room_group_name = 'chat' + async_to_sync(self.channel_layer.group_add)( + self.room_group_name, + self.channel_name + ) + self.accept() + + def receive(self, text_data): + text_data_json = json.loads(text_data) + message = text_data_json['message'] + username = text_data_json['username'] + async_to_sync(self.channel_layer.group_send)( + self.room_group_name, + { + 'type': 'chat', + 'message': message, + 'username': username + } + ) + + def chat(self, event): + message = event['message'] + username = event['username'] + self.send(text_data=json.dumps({ + 'message': message, + 'username': username + })) + + def disconnect(self, close_code): + async_to_sync(self.channel_layer.group_discard)( + self.room_group_name, + self.channel_name + ) + diff --git a/chat/migrations/__init__.py b/chat/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/chat/models.py b/chat/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/chat/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/chat/routing.py b/chat/routing.py new file mode 100644 index 00000000..69101be1 --- /dev/null +++ b/chat/routing.py @@ -0,0 +1,6 @@ +from django.urls import re_path +from . import consumers + +websocket_urlpatterns = [ + re_path(r'ws/chat', consumers.ChatConsumer.as_asgi()), +] diff --git a/chat/tests.py b/chat/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/chat/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/chat/views.py b/chat/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/chat/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. -- cgit v1.2.3