From ab9beff86769b1fec1f26f179aeb926c80595736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=BF=E5=B0=8F=E5=A4=A9?= <1638245306@qq.com> Date: Tue, 14 Mar 2023 23:52:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(websoket):=20=E2=9C=A8=20websoket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit websoket优化 --- web/src/utils/websocket.ts | 5 +- web/src/utils/websocket1.ts | 92 ------------------------------------- 2 files changed, 3 insertions(+), 94 deletions(-) delete mode 100644 web/src/utils/websocket1.ts diff --git a/web/src/utils/websocket.ts b/web/src/utils/websocket.ts index 4a58f48..72fe410 100644 --- a/web/src/utils/websocket.ts +++ b/web/src/utils/websocket.ts @@ -1,7 +1,7 @@ import {ElNotification as message} from 'element-plus' -import store from '@/store' import {Session} from "/@/utils/storage"; import {getWsBaseURL} from "/@/utils/baseUrl"; +// @ts-ignore import socket from '@/types/api/socket' const websocket: socket = { @@ -77,10 +77,11 @@ const websocket: socket = { websocket.send(data) }, websocket.hearbeat_interval) }, - send: (data, callback = null) => { + send: (data:string, callback = null) => { // 开启状态直接发送 if (websocket.websocket.readyState === websocket.websocket.OPEN) { websocket.websocket.send(JSON.stringify(data)) + // @ts-ignore callback && callback() } else { clearInterval(websocket.hearbeat_timer) diff --git a/web/src/utils/websocket1.ts b/web/src/utils/websocket1.ts deleted file mode 100644 index a04278f..0000000 --- a/web/src/utils/websocket1.ts +++ /dev/null @@ -1,92 +0,0 @@ -import {Notification} from 'element-plus' -import store from '@/store' -import {Session} from "/@/utils/storage"; -import {getWsBaseURL} from "/@/utils/baseUrl"; - -let socket=null; -function initWebSocket () { - const token = Session.get('token'); - if (token) { - const wsUri = getWsBaseURL() + 'ws/' + token + '/' - this.socket = new WebSocket(wsUri)// 这里面的this都指向vue - this.socket.onerror = webSocketOnError - this.socket.onmessage = webSocketOnMessage - this.socket.onclose = closeWebsocket - } -} - -function webSocketOnError (e) { - Notification({ - title: '', - message: 'WebSocket连接发生错误' + JSON.stringify(e), - type: 'error', - position: 'bottom-right', - duration: 3000 - }) -} - -/** - * 接收消息 - * @param e - * @returns {any} - */ -function webSocketOnMessage (e) { - const data = JSON.parse(e.data) - const { unread } = data - store.dispatch('d2admin/messagecenter/setUnread', unread || 0) - if (data.contentType === 'SYSTEM') { - Notification({ - title: '系统消息', - message: data.content, - type: 'success', - position: 'bottom-right', - duration: 3000 - }) - } else if (data.contentType === 'ERROR') { - Notification({ - title: '', - message: data.content, - type: 'error', - position: 'bottom-right', - duration: 0 - }) - } else if (data.contentType === 'INFO') { - Notification({ - title: '温馨提示', - message: data.content, - type: 'success', - position: 'bottom-right', - duration: 0 - }) - } else { - Notification({ - title: '温馨提示', - message: data.content, - type: 'info', - position: 'bottom-right', - duration: 3000 - }) - } -} -// 关闭websiocket -function closeWebsocket () { - console.log('连接已关闭...') - Notification({ - title: 'websocket', - message: '连接已关闭...', - type: 'danger', - position: 'bottom-right', - duration: 3000 - }) -} - -/** - * 发送消息 - * @param message - */ -function webSocketSend (message) { - this.socket.send(JSON.stringify(message)) -} -export default { - initWebSocket, closeWebsocket, webSocketSend -}