| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- /**
- * 修改 心跳开始结束的时机;20181212
- * 签入后心跳开始;签出后心跳结束;
- * 增加心跳 false 处理(签出)
- */
- import useSocketStore from '@/store/modules/socket'
- import { ElMessage } from 'element-plus'
- import { AES_Encrypt ,AES_Decrypt} from '@/utils/aes.js'
- import useUserStore from '@/store/modules/user'
- let ws // websocket 实例
- let cls = 0
- let lockReconnect = false // 避免重复连接
- let websocktTimer // 定时器 39.164.159.226
- // let wsUrl = 'ws://192.168.8.17:9000/ws/'+useSocketStore().accountNumber
- // let wsUrl = 'ws://39.164.159.226:9002'
- // let wsUrl = 'ws://123.13.222.95:9002'
- // let wsUrl = 'wss://kf.longing.net.cn:9002'
- let wsUrl
- let globalCallback
- const errorType = ['SayBusy','SayFree','Logout','Login','Auth']
- // const wsUrl = 'ws://' + process.env.SOCKET_IP + ':' + process.env.SOCKET_PORT
- // 创建scoket连接
- export function createWebSocket(wsUrl) {
- console.log(wsUrl)
- try {
- if ('WebSocket' in window) {
- ws = new WebSocket(wsUrl)
- init(wsUrl)
- } else {
- console.log('当前浏览器不支持Websocket!')
- }
- } catch (e) {
- reconnect()
- }
- }
- // 连接
- function init(wsUrl) {
- ws.onopen = function () {
- // console.groupCollapsed(`telWebsocket:`)
- // console.log(wsUrl)
- console.log(new Date() + 'telWebsocket' + '建立连接:' + wsUrl)
- const timestamp = new Date().getTime()
- const dataSocket = AES_Encrypt(timestamp+'zxkf')
- // const scoketDatas = {
- // Type: 'Auth',
- // Data: dataSocket, // 工号
- // }
- // ws.send(JSON.stringify(scoketDatas))
- }
- // 接收到消息的回调方法
- ws.onmessage = function (evt) {
- // 如果获取到消息,心跳检测重置
- // 拿到任何消息都说明当前连接是正常的
- heartCheck.reset().start()
- console.log(
- '%c' +
- new Date() +
- '%c receive%c telWebsocket %ctype:' +
- JSON.parse(evt.data).Type +
- '%c ' +
- evt.data,
- 'color: #111',
- 'color: #67C23A',
- 'color: #111',
- 'color: #67C23A',
- 'color: #111'
- )
- if (JSON.parse(evt.data).Type ==='Heart') {
- return
- }
- const telWSData = JSON.parse(evt.data)
- dealMsg(JSON.parse(evt.data))
-
- // 修改 store 中telwebsocket的数据
-
- }
- // 连接关闭的回调方法
- ws.onclose = function (evt) {
- if (cls === 0) {
- cls = 1
- console.log('telWebsocket连接关闭')
- reconnect()
- }
- }
- // 连接发生错误的回调方法
- ws.onerror = function (evt) {
- // 产生异常
- console.warn('telWebsocket连接出现异常!')
- if (ws == null || ws.readyState !== ws.OPEN) {
- console.log(new Date() + 'telWebsocket开始重连')
- reconnect()
- }
- }
- }
- /**
- * 重新连接
- */
- function reconnect() {
- if (lockReconnect) return
- lockReconnect = true
- // 没连接上会一直重连,设置延迟避免请求过多
- websocktTimer && clearTimeout(websocktTimer)
- websocktTimer = setTimeout(function () {
- console.log(new Date() + ' ' + 'telWebsocket重连中……')
- // createWebSocket()
- lockReconnect = false
- }, 2000)
- }
- /**
- * 发送
- * @ socketDatas 发送的数据 type {}
- */
- export function Send(scoketDatas, callback) {
- globalCallback = callback
- if (ws.readyState !== ws.OPEN) {
- reconnect()
- }
- if (ws.readyState === ws.OPEN) {
- console.log(
- '%c' +
- new Date() +
- '%c send %c telWebsocket ' +
- JSON.stringify(scoketDatas),
- 'color: #111',
- 'color: #409EFF',
- 'color: #111'
- )
- ws.send(JSON.stringify(scoketDatas))
- }
- }
- // 心跳检测
- const heartCheck = {
- timeout: 100*1000, // 5秒
- timeoutObj: null,
- serverTimeoutObj: null,
- reset: function () {
- clearTimeout(this.timeoutObj)
- clearTimeout(this.serverTimeoutObj)
- return this
- },
- start: function () {
- const _self = this
- this.timeoutObj = setTimeout(function () {
- // 这里发送一个心跳,后端收到后,返回一个心跳消息,
- // onmessage拿到返回的心跳就说明连接正常
- _self.sendHeart()
- }, this.timeout)
- },
-
- sendHeart: function () {
- const scoketDatas = {
- Type: 'Heart',
- Result: true,
- AgentId: useSocketStore().accountNumber, // 工号
- AgentExten: useSocketStore().extensionNumber, // 分机号
- AgentGroup: useSocketStore().groupNumber // 坐席组id
- }
- console.log(useSocketStore())
- Send(scoketDatas)
- }
- }
- /**
- * 处理接收的消息
- * @ msgData 接收的数据 type {}
- */
- function dealMsg(msgData) {
- if (msgData) {
- if (globalCallback) {
- globalCallback(msgData)
- }
-
- if (msgData.Result !== true) {
- if (errorType.indexOf(msgData.Type)!==-1) {
- ElMessage.error(msgData.Result)
- }
- }
- // const rlt = msgData.Result
- // if (rlt === true) {
- // switch (msgData.Type.toLowerCase()) {
- // case 'incoming': // 来电
- // IncomingBack(msgData)
- // break
- // }
- // } else if (rlt === false) {
- // const errCode = msgData.Error % 10000
- // console.error(`telWebsocket错误信息: ${errTips}(errorCode:${errCode})!`)
- // } else {
- // if (msgData.Type.toLowerCase() === 'linestate') {
- // LineStateAgentBack(msgData)
- // } else if (msgData.Type.toLowerCase() === 'monitor') {
- // //console.log(msgData)
- // updateSeatMonitorlists(msgData)
- // } else {
- // }
- // // Message.error(`telWebsocket错误信息: ${rlt} !`)
- // }
- }
- }
- // function updateSeatMonitorlists(msgData) {}
- export { ws }
|