websocket 钩子

This commit is contained in:
a0049873 2022-07-26 14:05:58 +08:00
parent 20e75d78cc
commit 486c3e8d46
1 changed files with 12 additions and 3 deletions

View File

@ -2,7 +2,7 @@
* @Author: hisense.wuhongjian * @Author: hisense.wuhongjian
* @Date: 2022-05-06 11:12:00 * @Date: 2022-05-06 11:12:00
* @LastEditors: hisense.liangjunhua * @LastEditors: hisense.liangjunhua
* @LastEditTime: 2022-07-21 17:09:10 * @LastEditTime: 2022-07-26 11:27:50
* @Description: 告诉大家这是什么 * @Description: 告诉大家这是什么
--> -->
<template> <template>
@ -31,19 +31,28 @@
const locale = zhCN const locale = zhCN
const token = Cookies.get('ucsToken') const token = Cookies.get('ucsToken')
console.log('token=================>', token) console.log('token=================>', token)
var ws = new WebSocket( let ws = new WebSocket(
`ws://${window.SITE_CONFIG['websocketURL']}/websocket?token=${token}` `ws://${window.SITE_CONFIG['websocketURL']}/websocket?token=${token}`
) )
// ws.send() // ws.send()
// onmessage // onmessage
ws.onmessage = function (e) { ws.onmessage = function (e) {
console.log('服务器返回的信息: ' + e.data) console.log('WebSocket服务器返回的信息: ' + e.data)
mybus.emit('getMynotice') mybus.emit('getMynotice')
// //
if (router.currentRoute.value.name === 'mynoticeView') { if (router.currentRoute.value.name === 'mynoticeView') {
mybus.emit('noticeListInit') mybus.emit('noticeListInit')
} }
} }
ws.onerror = function (e) {
console.log('WebSocket连接异常============================>', e)
ws = new WebSocket(
`ws://${window.SITE_CONFIG['websocketURL']}/websocket?token=${token}`
)
}
ws.onclose = function (e) {
console.log('WebSocket连接断开============================>', e)
}
onBeforeUnmount(() => { onBeforeUnmount(() => {
// 4., // 4.,
ws.close() ws.close()