UNPKG

fcc-core

Version:

Fusion communication center.

183 lines (171 loc) 5.46 kB
import { serverUrl as configApi } from '../../config' class WebServiceSocket { constructor (xw) { this.xw = xw // 对应的融合通讯中台对象 this.websocket = null this.events = {} // 已监听的事件集合 this.reconnectTimer = null // 重连的定时器 this.openCallback = null // 链接成功的回调函数 this.closeCallback = null // this.reconnectTimes = 0 // 重启次数 this.maxReConnectTimes = 5 // 最大重启次数 this.socketErr = false // socket连接错误标志 } // 建立与连接 open (callback) { this.openCallback = callback try { this.websocket.close() this.closeCallback = null } catch (e) { } this.initWebsocket() } on (eventType, callback, callbackThis) { if (eventType && callback) { let event = { callback, callbackThis } if (this.events[eventType]) { this.events[eventType].push(event) } else { this.events[eventType] = [event] } } else { console.error('EventName and callback are required!') } return this } once (eventType, callback, callbackThis) { if (eventType && callback) { let event = { callback, callbackThis, once: true } if (this.events[eventType]) { this.events[eventType].push(event) } else { this.events[eventType] = [event] } } else { console.error('EventName and callback are required!') } return this } emit (param) { if (param) { try { let sendJson = param sendJson.acceptType = '2' sendJson.isExtend = '0' sendJson.sendId = this.xw.userInfo.USER_CODE if (this.websocket && this.websocket.readyState === 1) { this.websocket.send(JSON.stringify(sendJson)) } else { this.initWebsocket() setTimeout(() => { this.websocket.send(JSON.stringify(sendJson)) }, 3000) console.error('webService send fail!') } } catch (e) { console.error('WebService Parameter format error!') } } else { console.error('SendType and param are required!') } return this } off (eventName, fn) { if (eventName && fn) { if (this.events[eventName] && this.events[eventName].length) { this.events[eventName] = this.events[eventName].filter(item => { if (fn === item.callback) { return false } else { return true } }) } } else { console.error('EventName and fn are required!') } return this } close (callback) { this.closeCallback = callback || true // 标识为主动断连 this.websocket.close() } initWebsocket () { const usercode = this.xw.userInfo.USER_CODE const isProd = process.env.NODE_ENV === 'production' const BASE_PATH = isProd ? configApi.production : configApi.development const baseurl = BASE_PATH['UCC'] const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss' const wsuri = `${protocol}://${location.host}${baseurl}/njdf_websocket?USER_CODE=${usercode}&SOCKET_TYPE=1` this.websocket = new WebSocket(wsuri) this.websocket.onopen = () => { clearTimeout(this.reconnectTimer) this.socketErr = false this.reconnectTimes = 0 console.log('webServiceSocket Successful connection.') this.openCallback && this.openCallback() console.log(this) } this.websocket.onclose = () => { this.reconnectTimer = setTimeout(() => { this.initWebsocket() }, 5000) if (this.closeCallback) { // 如果是主动关闭连接且传了回调 try { this.closeCallback() } catch (e) { } clearTimeout(this.reconnectTimer) // 不再重连 this.closeCallback = null } } this.websocket.onerror = (event) => { console.error('webServiceSocket error', event, this) if (this.reconnectTimes < this.maxReConnectTimes) { this.reconnectTimes++ setTimeout(() => { this.initWebsocket() }, 3000) } else { if (!this.socketErr) { this.socketErr = true this.openCallback && this.openCallback() } } } this.websocket.onmessage = (event) => { let eventData = JSON.parse(event.data) if (eventData['subtype'] && this.events[eventData['subtype']]) { this.events[eventData['subtype']].forEach(item => { try { // 防止有些事件方法被销毁后阻断事件派发的进程 if (item && item.callback) { if (item.callbackThis) { item.callback.call(item.callbackThis, eventData) } else { item.callback(eventData) } } } catch (e) { console.error(e) } }) this.events[eventData['subtype']] = this.events[eventData['subtype']].filter(item => { return !item.once }) } // 统一的错误处理 // if (eventData.resultCode !== 0) { // window.app.$message.error(eventData.resultMsg) // } } } } export default WebServiceSocket