From b518ddab668fdd126a2d2849955c764d0c98baf5 Mon Sep 17 00:00:00 2001 From: cheney Date: Fri, 10 Mar 2023 15:38:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9websocket=E4=BC=A0=E8=BE=93to?= =?UTF-8?q?ken=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/application/routing.py | 2 +- backend/application/websocketConfig.py | 9 ++++++--- backend/dvadmin/system/views/message_center.py | 2 +- web/src/api/websocket.js | 5 +++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/application/routing.py b/backend/application/routing.py index 237e5d1..3fe1a9d 100644 --- a/backend/application/routing.py +++ b/backend/application/routing.py @@ -3,6 +3,6 @@ from django.urls import path from application.websocketConfig import MegCenter websocket_urlpatterns = [ - path('ws//', MegCenter.as_asgi()), #consumers.DvadminWebSocket 是该路由的消费者 + path('ws/', MegCenter.as_asgi()), #consumers.DvadminWebSocket 是该路由的消费者 ] diff --git a/backend/application/websocketConfig.py b/backend/application/websocketConfig.py index 80515f4..21f1efc 100644 --- a/backend/application/websocketConfig.py +++ b/backend/application/websocketConfig.py @@ -58,7 +58,7 @@ class DvadminWebSocket(AsyncJsonWebsocketConsumer): async def connect(self): try: import jwt - self.service_uid = self.scope["url_route"]["kwargs"]["service_uid"] + self.service_uid = self.scope["subprotocols"][0] decoded_result = jwt.decode(self.service_uid, settings.SECRET_KEY, algorithms=["HS256"]) if decoded_result: self.user_id = decoded_result.get('user_id') @@ -68,7 +68,7 @@ class DvadminWebSocket(AsyncJsonWebsocketConsumer): self.chat_group_name, self.channel_name ) - await self.accept() + await self.accept(subprotocol=self.service_uid) # 主动推送消息 unread_count = await _get_message_unread(self.user_id) if unread_count == 0: @@ -78,7 +78,8 @@ class DvadminWebSocket(AsyncJsonWebsocketConsumer): await self.send_json( set_message('system', 'SYSTEM', "请查看您的未读消息~", unread=unread_count)) - except InvalidSignatureError: + except InvalidSignatureError as e: + print(e.__str__()) await self.disconnect(None) async def disconnect(self, close_code): @@ -121,6 +122,7 @@ class MessageCreateSerializer(CustomModelSerializer): model = MessageCenter fields = "__all__" read_only_fields = ["id"] + def websocket_push(user_id,message): username = "user_" + str(user_id) @@ -133,6 +135,7 @@ def websocket_push(user_id,message): } ) + def create_message_push(title: str, content: str, target_type: int=0, target_user: list=[], target_dept=None, target_role=None, message: dict = {'contentType': 'INFO', 'content': '测试~'}, request= Request): if message is None: diff --git a/backend/dvadmin/system/views/message_center.py b/backend/dvadmin/system/views/message_center.py index ad3c680..f633b50 100644 --- a/backend/dvadmin/system/views/message_center.py +++ b/backend/dvadmin/system/views/message_center.py @@ -100,7 +100,7 @@ def websocket_push(user_id, message): 主动推送消息 """ username = "user_"+str(user_id) - print(103,message) + # print(103,message) channel_layer = get_channel_layer() async_to_sync(channel_layer.group_send)( username, diff --git a/web/src/api/websocket.js b/web/src/api/websocket.js index 83a7e9d..494f80f 100644 --- a/web/src/api/websocket.js +++ b/web/src/api/websocket.js @@ -4,8 +4,9 @@ import store from '@/store' function initWebSocket (e) { const token = util.cookies.get('token') if (token) { - const wsUri = util.wsBaseURL() + 'ws/' + token + '/' - this.socket = new WebSocket(wsUri)// 这里面的this都指向vue + const wsUri = util.wsBaseURL() + 'ws/' + // const wsUri = util.wsBaseURL() + 'ws/' + token + '/' + this.socket = new WebSocket(wsUri, [token])// 这里面的this都指向vue this.socket.onerror = webSocketOnError this.socket.onmessage = webSocketOnMessage this.socket.onclose = closeWebsocket