1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| export default class websocket { constructor({ heartCheck, isReconnection }) { this._isLogin = false; this._netWork = true; this._isClosed = false; this._timeout = 3000; this._timeoutObj = null; this._connectNum = 0; this._heartCheck = heartCheck; this._isReconnection = isReconnection; this._onSocketOpened(); } _reset() { clearTimeout(this._timeoutObj); return this; } _start() { let _this = this; this._timeoutObj = setInterval(() => { wx.sendSocketMessage({ data: JSON.stringify({ "key": 'value' }), success(res) { console.log(res) console.log("发送心跳成功"); }, fail(err) { console.log(err) _this._reset() } }); }, this._timeout); } onSocketClosed(options) { wx.onSocketClose(err => { console.log('当前websocket连接已关闭,错误信息为:' + JSON.stringify(err)); if (this._heartCheck) { this._reset(); } this._isLogin = false; if (!this._isClosed) { if (this._isReconnection) { this._reConnect(options) } } }) } onNetworkChange(options) { wx.onNetworkStatusChange(res => { console.log('当前网络状态:' + res.isConnected); if (!this._netWork) { this._isLogin = false; if (this._isReconnection) { this._reConnect(options) } } }) } _onSocketOpened() { wx.onSocketOpen(res => { console.log('websocket已打开'); this._isLogin = true; if (this._heartCheck) { this._reset()._start(); } wx.sendSocketMessage({ data: JSON.stringify({ "key": 'value' }) }) this._netWork = true; }) } onReceivedMsg(callBack) { wx.onSocketMessage(msg => { if (typeof callBack == "function") { callBack(msg) } else { console.log('参数的类型必须为函数') } }) }
initWebSocket(options) { let _this = this; if (this._isLogin) { console.log("您已经登录了"); } else { wx.getNetworkType({ success(result) { if (result.networkType != 'none') { wx.connectSocket({ url: options.url, success(res) { if (typeof options.success == "function") { options.success(res) } else { console.log('参数的类型必须为函数') } }, fail(err) { if (typeof options.fail == "function") { options.fail(err) } else { console.log('参数的类型必须为函数') } } }) } else { console.log('网络已断开'); _this._netWork = false; wx.showModal({ title: '网络错误', content: '请重新打开网络', showCancel: false, success: function (res) { if (res.confirm) { console.log('用户点击确定') } } }) } } }) } } sendWebSocketMsg(options) { wx.sendSocketMessage({ data: options.data, success(res) { if (typeof options.success == "function") { options.success(res) } else { console.log('参数的类型必须为函数') } }, fail(err) { if (typeof options.fail == "function") { options.fail(err) } else { console.log('参数的类型必须为函数') } } }) } _reConnect(options) { let timer, _this = this; if (this._connectNum < 20) { timer = setTimeout(() => { this.initWebSocket(options) }, 3000) this._connectNum += 1; } else if (this._connectNum < 50) { timer = setTimeout(() => { this.initWebSocket(options) }, 10000) this._connectNum += 1; } else { timer = setTimeout(() => { this.initWebSocket(options) }, 450000) this._connectNum += 1; } } closeWebSocket(){ wx.closeSocket(); this._isClosed = true; } }
|