vue uses encapsulation websocket heartbeat package

Posted by mforan on Fri, 25 Oct 2019 14:00:45 +0200

This set of code can be used directly. Note that I will add comments in the following code:

Thanks for your support.

Core code

 1 //We need to introduce 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   //Initialization webSocket Long connection
12   initWebSocket: function () {
13     let corpId = localStorage.getItem('corpId');
14     let name = localStorage.getItem('username');
15     this.$ws = new WebSocket(wsurl);//Write address the address here can be initWebSocket Method add parameter
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   //open websocket
22   wsOpen: function (e) {
23     //start websocket heartbeat
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     //Reset every time a server message is received websocket heartbeat
32     wsConnection.resetHeartbeat();
33     //The message sent by the server is saved in vuex
34     store.commit('web_socket_msg', msg)
35   },
36   wsError: function (err) {
37     console.log(err, 'ws error');
38     wsConnection.reconnect()
39   },
40   //restart 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       //judge websocket current state
59       if (_this.$ws.readyState != 1) {
60         _this.reconnect()
61       }
62     }, _this.timeout);
63   },
64   //Reset websocket heartbeat
65   resetHeartbeat: function () {
66     let _this = this;
67     clearTimeout(_this.timeoutObj);
68     clearTimeout(_this.serverTimeoutObj);
69     _this.startWsHeartbeat()
70   }
71 };
72 
73 //Throw out websocket object
74 export default wsConnection

websocket method call

 

 1 //stay main.js Introduce
 2 import wsConnection from './vuex/wsStore'
 3 //mount vue Prototype chain
 4 Vue.prototype.$setWs = wsConnection;
 5 
 6 //Called where used
 7  $this.$setWs.initWebSocket();
 8 
 9 //When you need to use messages pushed by the server
10 //stay computed Method statement
11 getWsMsg() {
12    //The server information received in the core code is stored in the vuex Attribute
13     return this.$store.state.webSocketMsg
14 }
15 //stay watch Method monitoring  getWsMsg  
16  getWsMsg: function (data, val) {
17    console.log(data);
18    //.......
19 }      

This code is the original of the blogger, please indicate the source of reprint (support original! Thank you ~)

 


Number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words~

Number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words~

Number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words~

Number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words~

Number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words ~ number of words~

Topics: Javascript Vue Attribute