fcc-core
Version:
Fusion communication center.
183 lines (171 loc) • 5.6 kB
JavaScript
import { changeEmployeeStatus } from '../nats/natsApi'
import BaseException from '../../errors/base-exception'
// const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss'
const protocol = 'ws'
// const wsuri = `${protocol}://127.0.0.1:12700`
class ExeSocket {
constructor (xw) {
this.xw = xw // 对应的融合通讯中台对象
this.websocket = null
this.events = {} // 已监听的事件集合
this.reconnectTimer = null // 重连的定时器
this.openCallback = null // 链接成功的回调函数
this.rebuildTimes = 0 // 重启次数
this.maxRebuildTimes = 5 // 最大重启次数
this.socketErr = false // socket连接错误标志
}
// 建立与exe的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 (sendType, param) {
if (sendType && param) {
let sendJson = {
'function': sendType,
param
}
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('exeSocket send fail!')
}
} 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 EXE_IP = this.xw.getSysParam('EXE_IP')
// const wsuri = EXE_IP ? `${EXE_IP}` : `${protocol}://127.0.0.1:12700`
const wsuri = 'ws://127.0.0.1:12700'
console.log('wsuri',wsuri, this.xw.getSysParam('EXE_IP'))
this.websocket = new WebSocket(wsuri)
this.websocket.onopen = () => {
clearTimeout(this.reconnectTimer)
this.socketErr = false
this.rebuildTimes = 0
console.log('exeSocket Successful connection.')
this.openCallback && this.openCallback()
}
this.websocket.onclose = () => {
if (this.rebuildTimes < this.maxRebuildTimes) {
setTimeout(() => {
this.initWebsocket()
}, 5000)
}
if (this.closeCallback) { // 如果是主动关闭连接且传了回调
try {
this.closeCallback()
} catch (e) {
}
clearTimeout(this.reconnectTimer) // 不再重连
this.closeCallback = null
}
}
this.websocket.onerror = (event) => {
this.rebuildTimes++
if (this.rebuildTimes < this.maxRebuildTimes) {
if (this.rebuildTimes !== 1) console.error(`exe通信连接失败,正在重新启动,重启次数${this.rebuildTimes}`)
window.location.href = 'NICEAPP:*Playcall /seg=6502217909510670983:1:1 /un=Playbackuser /password=Playbackuser123 /url=10.7.5.204:62070'
} else {
if (!this.socketErr) {
this.socketErr = true
this.openCallback && this.openCallback()
}
}
console.error('exeSocket error', event)
}
this.websocket.onmessage = (event) => {
let eventData = JSON.parse(event.data)
if (eventData['eventType'] && this.events[eventData['eventType']]) {
this.events[eventData['eventType']].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, event)
}
})
this.events[eventData['eventType']] = this.events[eventData['eventType']].filter(item => {
return !item.once
})
}
// 统一的错误处理
if (eventData.resultCode !== 0) {
if (eventData.resultCode === 8888) {
// console.error(eventData.resultMsg)
throw new BaseException(514, eventData.resultMsg)
}
}
}
}
}
export default ExeSocket