首先安装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
});
}
},
},

  

最新文章

  1. JS 判断数据类型的三种方法
  2. asp.net c#过滤html代码,净化DIV SPAN等
  3. 关于KVC、KVO
  4. UVa 10870 & 矩阵快速幂
  5. javaweb学习总结(二十一)——JavaWeb的两种开发模式
  6. GC日志介绍
  7. 端午小长假--前端基础学起来03CSS为网页添加样式
  8. Java包装类缓存
  9. 前端时间戳timestamp相关总结:
  10. Toad DBA Suite for Oracle 12.6 64-bit Commercial 简单连接
  11. 两台Linux服务器之间复制文件
  12. ansible的主机的默认配置部分
  13. Unet网络
  14. C hashtable小例子
  15. Leetcode : eImplement strStr
  16. 8.并发容器ConcurrentHashMap#put方法解析
  17. 讲一讲MySQL如何防止“老鼠屎”类型的SQL语句
  18. POJ 2230 Watchcow(有向图欧拉回路)
  19. Cocos2d-x开源、跨平台的游戏引擎
  20. 第二步 (仅供参考) sencha touch 使用cmd打包apk

热门文章

  1. Android合并两个APP的详细做法(掌握)
  2. SQLyog普通版与SQLyog企业版对比分析
  3. ubuntu安装virtualbox
  4. 分布式消息中间件Rabbit Mq的了解与使用
  5. RDA 重现率
  6. 杂项-Java:JSP
  7. 卸载CentOS7-x64自带的OpenJDK的方法
  8. bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】
  9. bzoj1015星球大战(并查集+离线)
  10. 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"