diff --git a/ChatServiceApp/websocket_views.py b/ChatServiceApp/websocket_views.py index 811b952..8eb1323 100644 --- a/ChatServiceApp/websocket_views.py +++ b/ChatServiceApp/websocket_views.py @@ -14,10 +14,10 @@ class ChatConsumer(WebsocketConsumer): print('ws connect') self.room_group_name = 'test' - # async_to_sync(self.channel_layer.group_add)( - # f'user_{self.scope["user"]["id"]}', - # self.channel_name - # ) + async_to_sync(self.channel_layer.group_add)( + f'user_{self.scope["user"].id}', + self.channel_name + ) print(f'self.room_group_name = {self.room_group_name}') print(f'self.channel_name = {self.channel_name}') self.accept() @@ -25,25 +25,34 @@ class ChatConsumer(WebsocketConsumer): def receive(self, text_data): print(f'ws receive text_data = {text_data}') + data = json.loads(text_data) + from .funcs import send_msg Dict = send_msg(text_data) + + # print(f'send_msg res = {len(resDict)}') + + # self.send(text_data='!!!!') + group_name = f'user_{data["receiver"]}' resDict = { - 'type': 'ws_send_msg', + 'type': 'echo', + 'message': f'group = {group_name}', + # 'sender': resDict['sender'] + } + async_to_sync(self.channel_layer.group_send)( + group_name, + resDict + ) + + resDict = { + 'type': 'echo', 'message': Dict, # 'sender': resDict['sender'] } - print(f'send_msg res = {len(resDict)}') - - # self.send(text_data='!!!!') - + group_name = f'user_{data["sender"]}' async_to_sync(self.channel_layer.group_send)( - self.room_group_name, + group_name, resDict - # { - # 'type': 'chat_message', - # 'message': message, - # 'sender': sender - # } ) def ws_send_msg(self, data): diff --git a/TWB/asgi.py b/TWB/asgi.py index 7d8ce38..ad656f2 100644 --- a/TWB/asgi.py +++ b/TWB/asgi.py @@ -22,7 +22,7 @@ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TWB.settings') application = ProtocolTypeRouter({ 'http': get_asgi_application(), - "websocket": AuthMiddlewareStack( + "websocket": QueryAuthMiddleware( URLRouter( websocket_urlpatterns ) diff --git a/TWB/settings.py b/TWB/settings.py index a409e2d..837e851 100644 --- a/TWB/settings.py +++ b/TWB/settings.py @@ -93,11 +93,11 @@ ASGI_APPLICATION = 'TWB.asgi.application' CHANNEL_LAYERS = { 'default': { - # 'BACKEND': 'channels.layers.InMemoryChannelLayer' - "BACKEND": "channels_redis.core.RedisChannelLayer", - "CONFIG": { - "hosts": [("127.0.0.1", 6379)], - }, + 'BACKEND': 'channels.layers.InMemoryChannelLayer' + # "BACKEND": "channels_redis.core.RedisChannelLayer", + # "CONFIG": { + # "hosts": [("127.0.0.1", 6379)], + # }, } } diff --git a/TWB/ws_middleware.py b/TWB/ws_middleware.py index 017bbc7..193d17c 100644 --- a/TWB/ws_middleware.py +++ b/TWB/ws_middleware.py @@ -1,10 +1,15 @@ +import json + from channels.db import database_sync_to_async @database_sync_to_async -def get_user(user_id): +def get_user(data): from django.contrib.auth.models import User, AnonymousUser try: - return User.objects.get(id=user_id) + data = data.decode("utf-8") + param_list = data.split('&') + param_Dict = {item.split('=')[0]: item.split('=')[1] for item in param_list} + return User.objects.get(id=param_Dict['user_id']) except User.DoesNotExist: return AnonymousUser() @@ -21,6 +26,7 @@ class QueryAuthMiddleware: # Look up user from query string (you should also do things like # checking if it is a valid user ID, or if scope["user"] is already # populated). - scope['user'] = await get_user(int(scope["query_string"])) + data = scope['query_string'] + scope['user'] = await get_user(data) return await self.app(scope, receive, send) \ No newline at end of file