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
| import moment from 'moment' import Vue from 'vue' Vue.prototype.$moment = moment var websock = null var ws = null var globalCallback = function () {};
let rec; let isConnect = false; let checkMsg = "ping"; var data = { to:-1, text:'/' }; let createWebSocket = () => { try { var ishttps = 'https:' == document.location.protocol ? true : false; var ws if(ishttps) { ws = 'wss://wdev.xxx.com/api/screen/webSocket/'+moment().valueOf() } else { ws = 'ws://wdev.xxx.com/api/screen/webSocket/'+moment().valueOf(); } initWebSocket(); } catch (e) { console.log("尝试创建连接失败"); reConnect(); } };
let reConnect = () => { console.log("尝试重新连接"); if (isConnect) return; rec && clearTimeout(rec); rec = setTimeout(function () { createWebSocket(); }, 5000); }; var heartCheck = { timeout: 20000, timeoutObj: null,
start: function () { this.timeoutObj = setTimeout(function () { websock.send(JSON.stringify(data)); }, this.timeout); },
reset: function () { clearTimeout(this.timeoutObj); this.start(); } };
function websocketonmessage (e) { globalCallback(JSON.parse(e.data)) }
function websocketsend (agentData) {
setTimeout(() => { if (websock.readyState === websock.OPEN) { websock.send(agentData) } if (websock.readyState === websock.CLOSED) { console.log("重连中,请稍候") isConnect = false; reConnect(); } }, 500) }
function websocketclose (e) { isConnect = false ; console.log('connection closed (' + e.code + ')') console.log('websocket 连接关闭') }
function websocketOpen (e) { console.log('连接成功') } function initWebSocket (path) { if (typeof(WebSocket) === "undefined") { alert("您的浏览器不支持socket") } else { var ishttps = 'https:' == document.location.protocol ? true : false; var ws if(ishttps) { ws = 'wss://wdev.xxx.com/api/screen/webSocket/'+moment().valueOf() } else { ws = 'ws://wdev.xxx.com/api/screen/webSocket/'+moment().valueOf() } websock = new WebSocket(ws) websock.onmessage = function (e) { websocketonmessage(e) heartCheck.reset(); } websock.onclose = function (e) { websocketclose(e) reConnect(); } websock.onopen = function () { websocketOpen() heartCheck.start(); } websock.onerror = function () { console.log('WebSocket连接发生错误') isConnect = false; reConnect(); } } }
function sendSock (agentData, callback) { globalCallback = callback if (websock.readyState === websock.OPEN) { websocketsend(agentData) } else if (websock.readyState === websock.CONNECTING) { setTimeout(function () { sendSock(agentData, callback) }, 1000) } else { setTimeout(function () { sendSock(agentData, callback) }, 1000) } }
function getSock(callback) { globalCallback = callback }
function closeSock () { if (websock) { websock.close() } } initWebSocket()
export { initWebSocket, sendSock, getSock, closeSock }
|