发布时间:2023-02-27 13:00
// import { showInfoMsg, showErrorMsg } from '@/utils/popInfo'
import ElementUI from 'element-ui';
function initWebSocket(e) {
console.log(e)
const wsUri = WS_API + "/webSocket/" + e;
this.socket = new WebSocket(wsUri)//这里面的this都指向vue
this.socket.onerror = webSocketOnError;
this.socket.onmessage = webSocketOnMessage;
this.socket.onclose = closeWebsocket;
}
function webSocketOnError(e) {
ElementUI.Notification({
title: '',
message: "WebSocket连接发生错误" + e,
type: 'error',
duration: 0,
});
}
function webSocketOnMessage(e) {
const data = JSON.parse(e.data);
console.log(data.msgType === "INFO", data.msgType === "INFO")
if (data.msgType === "INFO") {
ElementUI.Notification({
title: '',
message: data.msg,
type: 'success',
duration: 3000,
});
} else if (data.msgType === "ERROR") {
ElementUI.Notification({
title: '',
message: data.msg,
type: 'error',
duration: 0,
});
}
}
// 关闭websiocket
function closeWebsocket() {
console.log('连接已关闭...')
}
function close() {
this.socket.close() // 关闭 websocket
this.socket.onclose = function (e) {
console.log(e)//监听关闭事件
console.log('关闭')
}
}
function webSocketSend(agentData) {
this.socket.send(agentData);
}
export default {
initWebSocket, close
}
如果想刷新重新链接websocket 可以在App.vue页面里添加个钩子函数
mounted() {
//当在任一路由页面被刷新时,便是根组件app被从新建立,此时能够进行webSocket重连
//从localStorage中获取用户信息,是登陆状态则能够进行webSocket重连
let token = localStorage.getItem("token");
if (token) {
// userMessage = JSON.parse(userMessage);
this.$websocket.initWebSocket(token);
}
},
logout() {
// localStorage.clear();
localStorage.removeItem("token");
this.$websocket.close();
this.$store.dispatch("LogOut").then(() => {
location.reload();
});
},
2、Deep Learning in Higher Dimensions
抖音短视频数据抓取实战系列(七)——python连接MySQL数据库
【大型电商项目开发】商城业务-首页搭建-thymeleaf模板引擎-36
喜讯!喜讯!JeecgBoot Github超 30000 Star—这个低代码平台你还不知道吗?
SpringBoot接口 - 如何生成接口文档之非侵入方式(通过注释生成)Smart-Doc?
【VisionMaster SDK开发】第一讲 环境配置篇(C#/C++)
We’re sorry but (项目名称) doesn’t work properly without JavaScript enabled/Vue路由history模式跳转报错404--解决方案
Transformer新型神经网络在机器翻译中的应用 | 公开课笔记
Leetcode题78、子集(Python题解)字节跳动面试题
MindSpore报错 For primitive[TensorSummary], the v rank 必须大于等于0