欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue使用 封装websocket心跳包

程序员文章站 2022-06-27 21:52:36
这套代码可以拿过去直接用 一些注意我会在下面代码中加上注释: 谢谢支持 核心代码 websocket方法调用 此代码为本博主原创,转载请注明出处(支持原创! 谢谢~) 凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字 ......

这套代码可以拿过去直接用 一些注意我会在下面代码中加上注释:

谢谢支持

核心代码

 1 //这里需要引入vuex
 2 import store from './store';
 3 
 4 let wsconnection = {
 5   $ws: null,
 6   lockreturn: false,
 7   timeout: 60 * 1000 * 5,
 8   timeoutobj: null,
 9   timeoutnum: null,
10   servertimeoutobj: null,
11   //初始化websocket长连接
12   initwebsocket: function () {
13     let corpid = localstorage.getitem('corpid');
14     let name = localstorage.getitem('username');
15     this.$ws = new websocket(wsurl);//写入地址 这里的地址可以在initwebsocket方法加入参数
16     this.$ws.onopen = this.wsopen;
17     this.$ws.onclose = this.wsclose;
18     this.$ws.onmessage = this.wsmsg;
19     this.$ws.onerror = this.wserror;
20   },
21   //打开websocket
22   wsopen: function (e) {
23     //开始websocket心跳
24     wsconnection.startwsheartbeat();
25     console.log('ws success')
26   },
27   wsclose: function (e) {
28     console.log(e, 'ws close')
29   },
30   wsmsg: function (msg) {
31     //每次接收到服务端消息后 重置websocket心跳
32     wsconnection.resetheartbeat();
33     //服务端发送来的消息存到vuex
34     store.commit('web_socket_msg', msg)
35   },
36   wserror: function (err) {
37     console.log(err, 'ws error');
38     wsconnection.reconnect()
39   },
40   //重启websocket
41   reconnect: function () {
42     let _this = this;
43     if (_this.lockreturn) {
44       return;
45     }
46     _this.lockreturn = true;
47     _this.timeoutnum && cleartimeout(_this.timeoutnum);
48     _this.timeoutnum = settimeout(function () {
49       _this.initwebsocket();
50       _this.lockreturn = false;
51     }, 3000);
52   },
53   startwsheartbeat: function () {
54     let _this = this;
55     _this.timeoutobj && cleartimeout(_this.timeoutobj);
56     _this.servertimeoutobj && cleartimeout(_this.servertimeoutobj);
57     _this.timeoutobj = setinterval(function () {
58       //判断websocket当前状态
59       if (_this.$ws.readystate != 1) {
60         _this.reconnect()
61       }
62     }, _this.timeout);
63   },
64   //重置websocket心跳
65   resetheartbeat: function () {
66     let _this = this;
67     cleartimeout(_this.timeoutobj);
68     cleartimeout(_this.servertimeoutobj);
69     _this.startwsheartbeat()
70   }
71 };
72 
73 //抛出websocket对象
74 export default wsconnection

websocket方法调用

 

 1 //在main.js引入
 2 import wsconnection from './vuex/wsstore'
 3 //挂载vue原型链
 4 vue.prototype.$setws = wsconnection;
 5 
 6 //在使用地方调用
 7  $this.$setws.initwebsocket();
 8 
 9 //在需要使用服务端推送过来的消息时
10 //在computed方法声明
11 getwsmsg() {
12    //在核心代码接收到的服务端信息存储到vuex的属性
13     return this.$store.state.websocketmsg
14 }
15 //在watch方法   监听  getwsmsg  
16  getwsmsg: function (data, val) {
17    console.log(data);
18    //.......
19 }      

此代码为本博主原创,转载请注明出处(支持原创! 谢谢~)

 


凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~

凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~

凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~

凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~

凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~凑字数~