vue中websoket的使用
首先安装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
});
}
},
},
vue中websoket的使用的更多相关文章
- swoole中websoket创建在线聊天室(php)
swoole中websoket创建在线聊天室(php) swoole现仅支持Linix,macos 创建websocket服务器 首先现在服务器创建一个websocket服务器 <?php // ...
- vue中如何不通过路由直接获取url中的参数
前言:为什么要不通过路由直接获取url中的参数? vue中使用路由的方式设置url参数,但是这种方式必须要在路径中附带参数,而且这个参数是需要在vue的路由中提前设置好的. 相对来说,在某些情况下直接 ...
- vue中的重要特性
一.vue中的自定义组件 html的代码: <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- Vue中comoputed中的数据绑定
Vue中的数据实现响应式绑定是在初始化的时候利用definePrototype的定义set和get过滤器,在进行组件模板编译时实现water的监听搜集依赖项,当数据发生变化时在set中通过调用dep. ...
- vue中使用stompjs实现mqtt消息推送通知
最近在研究vue+webAPI进行前后端分离,在一些如前端定时循环请求后台接口判断状态等应用场景用使用mqtt进行主动的消息推送能够很大程度的减小服务端接口的压力,提高系统的效率,而且可以利用mqtt ...
- Vue中应用CORS实现AJAX跨域,及它在 form data 和 request payload 的小坑处理
基本概念部分(一):理解CORS 说道Vue的跨域AJAX,我想先梳理一遍CORS跨域,"跨域资源共享"(Cross-origin resource sharing),它是一个W3 ...
- vue中watched属性
watched属性,vue中的观察属性,可用来监听一个值的变化 默认有两个参数,新值,旧值 data (){ return { currentCity: "深圳" } } watc ...
- 七、vue中v-for有时候对页面不会重新渲染,数组变化后如何到渲染页面
v-for不能进行双向数据绑定,页面渲染完成后,再次更改v-for遍历的数据,js里面打印的数据看到数据值已经更改,但是页面的数据就是没有渲染,这是为什么呢? vue中v-for和angularj ...
- 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据
前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于“父子组件通信”的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰符做父子组件数据双向 ...
随机推荐
- COCOS学习笔记--即时动作ActionInstant
Cocos引擎中的动作类的关系图例如以下: 能够看出,Action是继承自Ref类的,之前我的博客中也有讲过,Ref类是cocos2dx全部类的基类.动作类(Action)是全部动作的基类.它通过cr ...
- 【bzoj3124】[Sdoi2013]直径
1.求树的直径: 先随便取一个点,一遍dfs找到离它最远的点l1,再以l1为起点做一遍dfs,找到离l1最远的点l2 那么l1到l2的距离即为直径 2. 求出有多少条边在这棵树的所有直径上: ...
- projecteuler---->problem=12----Highly divisible triangular number
title: The sequence of triangle numbers is generated by adding the natural numbers. So the 7th trian ...
- mybtis 逆向
mbg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfi ...
- Use of implicitly declared global variable
https://stackoverflow.com/questions/7604419/resharper-javascript-use-of-implicitly-declared-global-v ...
- 123 c#调用oracle存储过程返回数据集 --通过oracle存储过程返回数据集
c#调用oracle存储过程返回数据集 2008-12-20 10:59:57| 分类: net|字号 订阅 CREATE OR REPLACE PACKAGE pkg_tableTypeIS ...
- Python中关键字global与nonlocal的区别
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xCyansun/article/details/79672634终于下定决心学习Python了.既然 ...
- 第十四周 Leetcode 315. Count of Smaller Numbers After Self(HARD) 主席树
Leetcode315 题意很简单,给定一个序列,求每一个数的右边有多少小于它的数. O(n^2)的算法是显而易见的. 用普通的线段树可以优化到O(nlogn) 我们可以直接套用主席树的模板. 主席树 ...
- bzoj1085 [SCOI2005]骑士精神——IDA*
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 搜索,IDA*,估价就是最少需要跳的步数: 代码意外地挺好写的,memcmp 用起来好 ...
- bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车【排序+贪心】
从小到大排个序,然后能选就选 #include<iostream> #include<cstdio> #include<algorithm> using names ...