chat v3
This commit is contained in:
SDE
2023-08-11 23:25:28 +03:00
parent aeb90e106e
commit 78ade08e65
7 changed files with 83 additions and 4 deletions

View File

@@ -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'),
]

View File

@@ -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': 'Тикеты'},
),
]

View File

@@ -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())
]

View File

@@ -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
}))

View File

@@ -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
)
)
})

View File

@@ -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

View File

@@ -6,3 +6,5 @@ Pillow
django-modeltranslation==0.18.10
overpass
geopy
channels==4.0.0
daphne==4.0.0