/** * Created by admin on 2017/8/19. */ // import Vue from 'vue' // import axios from './HTTP.js' // Vue.use(axios) import * as DB from './DBDATA.js' // import qs from 'qs' const WebSocketMsg = function (_self, url) { let obj = {} obj.infoData = DB.INFO_DATA obj.ws = null obj.lockReconnect = false // 避免重复链接 // obj.wsUrl = 'ws://192.168.1.90:8080/service-jcj/websocket' // 通信地址 obj.createWebSocket = function () { // 通信开启事件 try { obj.ws = new WebSocket(url) obj.initEventHandle() } catch (e) { obj.reconnect(url) } } obj.initEventHandle = function () { // 通信操做事件 开启 消息 关闭 错误 obj.ws.onclose = function () { obj.reconnect(url) // 通信重连 console.log('通信关闭') } obj.ws.onerror = function () { obj.reconnect(url) // 通信重连 console.log('通信错误') } obj.ws.onopen = function () { // 心跳检测重置 obj.heartCheck.reset() // 心跳检测重置 obj.heartCheck.start() // 心跳检测开启 console.log('通信开启') } obj.ws.onmessage = function (event) { // 若是获取到消息,心跳检测重置 // 拿到任何消息都说明当前链接是正常的 obj.heartCheck.reset() // 心跳检测重置 obj.heartCheck.start() // 心跳检测开启 console.log('通信消息') console.log(event) } } obj.reconnect = function () { // 通信重连 if (obj.lockReconnect) { // 若是为真 结束事件 为假 执行后续事件 return } obj.lockReconnect = true // 没链接上会一直重连,设置延迟避免请求过多 setTimeout(function () { obj.createWebSocket(url) // 新开启通信 obj.lockReconnect = false }, 2000) } // 心跳检测 obj.heartCheck = { timeout: 6000, // 60秒 timeoutObj: null, serverTimeoutObj: null, reset: function () { clearTimeout(this.timeoutObj) clearTimeout(this.serverTimeoutObj) return this }, start: function () { let that = this that.timeoutObj = setTimeout(function () { // 这里发送一个心跳,后端收到后,返回一个心跳消息, // onmessage拿到返回的心跳就说明链接正常 obj.ws.send('@') // 向后端发送消息 若是后端接收 则重置心跳 that.serverTimeoutObj = setTimeout(function () { // 若是超过必定时间还没重置,说明后端主动断开了 obj.ws.close() // 若是onclose会执行reconnect,咱们执行ws.close()就好了.若是直接执行reconnect 会触发onclose致使重连两次 }, that.timeout) }, that.timeout) } } return obj } export default WebSocketMsg