vue中websoket的使用
2024-09-04 10:05:27
首先安装npm install --save websocket-heartbeat-js@^1.0.7
在main.js中 引入并挂载全局方法
import WebsocketHeartbeatJs from 'websocket-heartbeat-js';
if (JSON.parse(getStore('userInfo'))!= null) {
let userId = JSON.parse(getStore('userInfo')).id;
let websocketHeartbeatJs = new WebsocketHeartbeatJs({
// url: 'ws://192.168.0.68:8888/websocket/' + userId
url: 'ws://192.168.0.22:8888/websocket/' + userId
pongTimeout: 1800000,reconnectTimeout: 100
});
Vue.prototype.websocketHeartbeatJs = websocketHeartbeatJs;
}
在app.vue中 建立连接
mounted() {
// console.log(JSON.parse(this.getStore("userInfo")),'--------JSON.parse(this.getStore("userInfo"))-------')
if (JSON.parse(this.getStore("userInfo")).id != "") {
// console.log(JSON.parse(this.getStore("userInfo")).id);
if ("WebSocket" in window) {
this.websocketHeartbeatJs.onopen = function() {};
var that = this;
this.websocketHeartbeatJs.onmessage = function(e) {
// console.log(JSON.parse(e.data),'-------------JSON.parse(e.data)-----------------');
that.$store.state.websocketMsg = JSON.parse(e.data);
在store中存放webscoket信息
// console.log(that.$store.state.websocketMsg);
};
this.websocketHeartbeatJs.onreconnect = function() {};
} else {
alert("当前浏览器 Not support websocket");
}
}
},
在vue组件中使用
computed: {
websocketInfo() {
return this.$store.state.websocketMsg;
//创建完成时存在store中的信息
}
},
watch: {
websocketInfo: function (nv, ov) {
console.log(nv);
this.websocketSuccess = nv.result.success;
this.websocketMsg = nv.result.message;
console.log(this.websocketSuccess, '-----------websocketSuccess----------');
console.log(this.websocketMsg, '----------websocketMsg-------------');
// console.log(nv.result.result.error.code);
if (this.websocketSuccess == true && this.websocketMsg == "true") {
// 创建云盘成功后挂载云主机
if (nv.result.result.inventory != undefined) {
this.diskid = nv.result.result.inventory.uuid;
this.id = this.$store.state.vmId;
}
this.$Notice.destroy({
name: "warning",
duration: 1
});
var imageNames = this.vmName == "" ? "" : "'" + this.vmName + "'";
this.$Notice.success({
title: this.operation == "" ?
this.$store.state.operationed : "云盘"+ imageNames + this.operation +"成功"
});
this.getImagesList(this.pageSize, this.pageNumber);
this.$refs.child.getinfo();
this.$refs.child.chishihua();
this.value2 = true;
// 创建完成挂载云主机
if(this.operation == '' && this.$store.state.operationed == '创建镜像成功'){
this.guazai();
};
this.operation == "" ;
} else if (
this.websocketSuccess == true &&
this.websocketMsg == "false"
) {
var str = nv.result.result.error.details;
// str = str.split("不")[0];
// console.log(str);
this.$Notice.warning({
title: str
});
} else if (
this.websocketSuccess == true &&
this.websocketMsg == ""
) {
this.$Notice.warning({
title: this.$store.state.operationed + "创建中",
duration: 1
});
}
},
},
最新文章
- JS 判断数据类型的三种方法
- asp.net c#过滤html代码,净化DIV SPAN等
- 关于KVC、KVO
- UVa 10870 &; 矩阵快速幂
- javaweb学习总结(二十一)——JavaWeb的两种开发模式
- GC日志介绍
- 端午小长假--前端基础学起来03CSS为网页添加样式
- Java包装类缓存
- 前端时间戳timestamp相关总结:
- Toad DBA Suite for Oracle 12.6 64-bit Commercial 简单连接
- 两台Linux服务器之间复制文件
- ansible的主机的默认配置部分
- Unet网络
- C hashtable小例子
- Leetcode : eImplement strStr
- 8.并发容器ConcurrentHashMap#put方法解析
- 讲一讲MySQL如何防止“老鼠屎”类型的SQL语句
- POJ 2230 Watchcow(有向图欧拉回路)
- Cocos2d-x开源、跨平台的游戏引擎
- 第二步 (仅供参考) sencha touch 使用cmd打包apk
热门文章
- Android合并两个APP的详细做法(掌握)
- SQLyog普通版与SQLyog企业版对比分析
- ubuntu安装virtualbox
- 分布式消息中间件Rabbit Mq的了解与使用
- RDA 重现率
- 杂项-Java:JSP
- 卸载CentOS7-x64自带的OpenJDK的方法
- bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】
- bzoj1015星球大战(并查集+离线)
- document.write()、onclick=";alert(xxx)、innerHTML、image.src.match(";xxx";)、id2.style.color=";blue";;、isNaN(id2)、document.write(";糟糕!文档消失了。";)、alert(id2.outerHTML)、id2.className=";id06";;、onclick=";return registe";