From 2aa363f6ce68b6792d5229d6472ebe1940cee172 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 11 Jun 2021 17:10:22 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96websocket=EF=BC=8C?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E8=8E=B7=E5=8F=96=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit perf: 优化提示i18n perf: remove yarn .. --- src/i18n/langs/cn.json | 1 + src/i18n/langs/en.json | 1 + .../components/NavHeader/SiteMessages.vue | 33 +++++++++++-------- yarn.lock | 5 +++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/i18n/langs/cn.json b/src/i18n/langs/cn.json index 2fe6cf88c..235d1c872 100644 --- a/src/i18n/langs/cn.json +++ b/src/i18n/langs/cn.json @@ -214,6 +214,7 @@ "ReLogin": "重新登录" }, "common": { + "ConnectWebSocketError": "连接 WebSocket 失败", "Action": "动作", "RequestTickets": "申请工单", "Actions": "操作", diff --git a/src/i18n/langs/en.json b/src/i18n/langs/en.json index 2b500bef2..9431f5d0d 100644 --- a/src/i18n/langs/en.json +++ b/src/i18n/langs/en.json @@ -213,6 +213,7 @@ "ReLogin": "Re-Login" }, "common": { + "ConnectWebSocketError": "Connect Websocket failed", "Nothing": "Nothing", "Action": "Action", "CustomCol":"Custom table col", diff --git a/src/layout/components/NavHeader/SiteMessages.vue b/src/layout/components/NavHeader/SiteMessages.vue index 97dfa154f..1bf0af33a 100644 --- a/src/layout/components/NavHeader/SiteMessages.vue +++ b/src/layout/components/NavHeader/SiteMessages.vue @@ -121,7 +121,6 @@ export default { } }, markAsRead(msg) { - console.log(`${msg}`) const url = `/api/v1/notifications/site-message/mark-as-read/` this.$axios.patch(url, { ids: [msg.id] }).then(res => { this.msgDetailVisible = false @@ -133,19 +132,27 @@ export default { cancelRead() { this.msgDetailVisible = false }, - pullMsgCount() { - const url = '/api/v1/notifications/site-message/unread-total/' - this.$axios.get(url).then(res => { - this.unreadMsgCount = res.total - }).catch(err => { - this.$message(err.detail) - }) - }, enablePullMsgCount() { - this.pullMsgCount() - setInterval(() => { - this.pullMsgCount() - }, 10000) + const ws = new WebSocket(`ws://localhost:9528/ws/notifications/site-msg/`) + ws.onopen = (event) => { + this.$log.debug('Websocket connected: ', event) + } + ws.onmessage = (event) => { + try { + const data = JSON.parse(event.data) + this.$log.debug('Data: ', data) + const unreadCount = data['unread_count'] + if (unreadCount !== undefined) { + this.unreadMsgCount = unreadCount + } + } catch (e) { + this.$log.debug('Recv site message error') + } + } + ws.onerror = (error) => { + this.$message.error(this.$t('common.ConnectWebSocketError')) + this.$log.debug('site message ws error: ', error) + } } } } diff --git a/yarn.lock b/yarn.lock index dce5f44e6..e4534de6f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11288,6 +11288,11 @@ yargs@^11.0.0: y18n "^3.2.1" yargs-parser "^9.0.2" +yarn@^1.22.10: + version "1.22.10" + resolved "https://registry.npm.taobao.org/yarn/download/yarn-1.22.10.tgz#c99daa06257c80f8fa2c3f1490724e394c26b18c" + integrity sha1-yZ2qBiV8gPj6LD8UkHJOOUwmsYw= + yorkie@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/yorkie/-/yorkie-2.0.0.tgz#92411912d435214e12c51c2ae1093e54b6bb83d9"