From 10f3323676e4a48f8109c423ea96971bace16616 Mon Sep 17 00:00:00 2001 From: a0049873 <79py69t9wb@privaterelay.appleid.com> Date: Thu, 21 Jul 2022 14:34:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E4=B8=AD=E5=BF=83=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20websocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/views/home/components/header.vue | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/front/src/views/home/components/header.vue b/front/src/views/home/components/header.vue index 934d6595..6858f470 100644 --- a/front/src/views/home/components/header.vue +++ b/front/src/views/home/components/header.vue @@ -122,6 +122,7 @@ import { mynotice } from '@/api/home' import { useStore } from 'vuex' import { getSgcTotal } from '@/api/home' + import Cookies from 'js-cookie' import mybus from '@/myplugins/mybus' const store = useStore() const router = useRouter() @@ -284,6 +285,18 @@ // getMynotice() // }) // } + // 建立链接 -- 携带cookie参数 + const token = Cookies.get('ucsToken') + console.log('token=================>', token) + var ws = new WebSocket( + `ws://15.2.21.238:8888/renren-admin/websocket?token=${token}` + ) + // ws.send()给服务器发送信息 + // 服务器每次返回信息都会执行一次onmessage方法 + ws.onmessage = function (e) { + // console.log('服务器返回的信息: ' + e.data) + getMynotice() + } onMounted(() => { getSgcTotal().then((res) => { // console.log('初始化========================>', res.data.data.count) @@ -322,6 +335,8 @@ onBeforeUnmount(() => { mybus.off('getSgcNum') mybus.off('getMynotice') + // 4.卸载前, 关闭链接 + ws.close() })