From 78ade08e65674f2c00f420dc35374ba80a5f2a75 Mon Sep 17 00:00:00 2001 From: SDE Date: Fri, 11 Aug 2023 23:25:28 +0300 Subject: [PATCH] 0.3.0 chat v3 --- ChatServiceApp/js_urls.py | 2 +- ..._message_options_alter_msggroup_options.py | 21 ++++++++++ ChatServiceApp/websocket_urls.py | 7 ++++ ChatServiceApp/websocket_views.py | 38 +++++++++++++++++++ TWB/asgi.py | 12 +++++- TWB/settings.py | 5 ++- requirements.pip | 2 + 7 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 ChatServiceApp/migrations/0004_alter_message_options_alter_msggroup_options.py create mode 100644 ChatServiceApp/websocket_urls.py create mode 100644 ChatServiceApp/websocket_views.py diff --git a/ChatServiceApp/js_urls.py b/ChatServiceApp/js_urls.py index 9650d9b..9b5c21d 100644 --- a/ChatServiceApp/js_urls.py +++ b/ChatServiceApp/js_urls.py @@ -11,6 +11,6 @@ urlpatterns = [ path('create_ticket/', create_ticket_ajax, name='create_ticket_ajax'), path('support_show_chat_by_ticket/', support_show_chat_by_ticket_ajax, name='support_show_chat_by_ticket_ajax'), path('send_msg/', send_msg_ajax, name='send_msg_ajax'), - path('update_chat/', update_chat_ajax2, name='update_chat_ajax'), + # path('update_chat/', update_chat_ajax2, name='update_chat_ajax'), path('show_chat_w_user/', show_chat_w_user_ajax, name='show_chat_w_user_ajax'), ] \ No newline at end of file diff --git a/ChatServiceApp/migrations/0004_alter_message_options_alter_msggroup_options.py b/ChatServiceApp/migrations/0004_alter_message_options_alter_msggroup_options.py new file mode 100644 index 0000000..91ad925 --- /dev/null +++ b/ChatServiceApp/migrations/0004_alter_message_options_alter_msggroup_options.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.2 on 2023-08-11 23:21 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ChatServiceApp', '0003_msggroup_text'), + ] + + operations = [ + migrations.AlterModelOptions( + name='message', + options={'verbose_name': 'Сообщение', 'verbose_name_plural': 'Сообщения'}, + ), + migrations.AlterModelOptions( + name='msggroup', + options={'verbose_name': 'Тикет', 'verbose_name_plural': 'Тикеты'}, + ), + ] diff --git a/ChatServiceApp/websocket_urls.py b/ChatServiceApp/websocket_urls.py new file mode 100644 index 0000000..a5a4c24 --- /dev/null +++ b/ChatServiceApp/websocket_urls.py @@ -0,0 +1,7 @@ +from django.urls import re_path +from .websocket_views import * + +websocket_urlpatterns = [ + re_path(r'ws/socket-server/', ChatConsumer.as_asgi()) + +] \ No newline at end of file diff --git a/ChatServiceApp/websocket_views.py b/ChatServiceApp/websocket_views.py new file mode 100644 index 0000000..b148664 --- /dev/null +++ b/ChatServiceApp/websocket_views.py @@ -0,0 +1,38 @@ +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 = 'test' + + 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'] + sender = text_data_json['sender'] + + async_to_sync(self.channel_layer.group_send)( + self.room_group_name, + { + 'type': 'chat_message', + 'message': message, + 'sender': sender + } + ) + + def chat_message(self, event): + message = event['message'] + sender = event['sender'] + + self.send(text_data=json.dumps({ + 'type': 'chat', + 'message': message, + 'sender': sender + })) \ No newline at end of file diff --git a/TWB/asgi.py b/TWB/asgi.py index 59da03b..64e910b 100644 --- a/TWB/asgi.py +++ b/TWB/asgi.py @@ -10,7 +10,17 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ import os from django.core.asgi import get_asgi_application +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.auth import AuthMiddlewareStack +from ChatServiceApp.websocket_urls import websocket_urlpatterns os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TWB.settings') -application = get_asgi_application() +application = ProtocolTypeRouter({ + 'http': get_asgi_application(), + 'websocket': AuthMiddlewareStack( + URLRouter( + websocket_urlpatterns + ) + ) +}) diff --git a/TWB/settings.py b/TWB/settings.py index 93b9941..65d4fb4 100644 --- a/TWB/settings.py +++ b/TWB/settings.py @@ -33,6 +33,7 @@ ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = [ + 'channels', 'modeltranslation', 'django.contrib.admin', @@ -85,8 +86,8 @@ TEMPLATES = [ }, ] -WSGI_APPLICATION = 'TWB.wsgi.application' - +# WSGI_APPLICATION = 'TWB.wsgi.application' +ASGI_APPLICATION = 'TWB.asgi.application' # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases diff --git a/requirements.pip b/requirements.pip index a930794..2680187 100644 --- a/requirements.pip +++ b/requirements.pip @@ -6,3 +6,5 @@ Pillow django-modeltranslation==0.18.10 overpass geopy +channels==4.0.0 +daphne==4.0.0