aboutsummaryrefslogtreecommitdiff
path: root/chat
diff options
context:
space:
mode:
Diffstat (limited to 'chat')
-rw-r--r--chat/__init__.py0
-rw-r--r--chat/admin.py3
-rw-r--r--chat/apps.py6
-rw-r--r--chat/consumers.py40
-rw-r--r--chat/migrations/__init__.py0
-rw-r--r--chat/models.py3
-rw-r--r--chat/routing.py6
-rw-r--r--chat/tests.py3
-rw-r--r--chat/views.py3
9 files changed, 64 insertions, 0 deletions
diff --git a/chat/__init__.py b/chat/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/chat/__init__.py
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
--- /dev/null
+++ b/chat/migrations/__init__.py
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.