Nessuna descrizione

telWebsocket.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * 修改 心跳开始结束的时机;20181212
  3. * 签入后心跳开始;签出后心跳结束;
  4. * 增加心跳 false 处理(签出)
  5. */
  6. import useSocketStore from '@/store/modules/socket'
  7. import { ElMessage } from 'element-plus'
  8. import { AES_Encrypt ,AES_Decrypt} from '@/utils/aes.js'
  9. import useUserStore from '@/store/modules/user'
  10. let ws // websocket 实例
  11. let cls = 0
  12. let lockReconnect = false // 避免重复连接
  13. let websocktTimer // 定时器 39.164.159.226
  14. // let wsUrl = 'ws://192.168.8.17:9000/ws/'+useSocketStore().accountNumber
  15. // let wsUrl = 'ws://39.164.159.226:9002'
  16. // let wsUrl = 'ws://123.13.222.95:9002'
  17. // let wsUrl = 'wss://kf.longing.net.cn:9002'
  18. let wsUrl
  19. let globalCallback
  20. const errorType = ['SayBusy','SayFree','Logout','Login','Auth']
  21. // const wsUrl = 'ws://' + process.env.SOCKET_IP + ':' + process.env.SOCKET_PORT
  22. // 创建scoket连接
  23. export function createWebSocket(wsUrl) {
  24. console.log(wsUrl)
  25. try {
  26. if ('WebSocket' in window) {
  27. ws = new WebSocket(wsUrl)
  28. init(wsUrl)
  29. } else {
  30. console.log('当前浏览器不支持Websocket!')
  31. }
  32. } catch (e) {
  33. reconnect()
  34. }
  35. }
  36. // 连接
  37. function init(wsUrl) {
  38. ws.onopen = function () {
  39. // console.groupCollapsed(`telWebsocket:`)
  40. // console.log(wsUrl)
  41. console.log(new Date() + 'telWebsocket' + '建立连接:' + wsUrl)
  42. const timestamp = new Date().getTime()
  43. const dataSocket = AES_Encrypt(timestamp+'zxkf')
  44. // const scoketDatas = {
  45. // Type: 'Auth',
  46. // Data: dataSocket, // 工号
  47. // }
  48. // ws.send(JSON.stringify(scoketDatas))
  49. }
  50. // 接收到消息的回调方法
  51. ws.onmessage = function (evt) {
  52. // 如果获取到消息,心跳检测重置
  53. // 拿到任何消息都说明当前连接是正常的
  54. heartCheck.reset().start()
  55. console.log(
  56. '%c' +
  57. new Date() +
  58. '%c receive%c telWebsocket %ctype:' +
  59. JSON.parse(evt.data).Type +
  60. '%c ' +
  61. evt.data,
  62. 'color: #111',
  63. 'color: #67C23A',
  64. 'color: #111',
  65. 'color: #67C23A',
  66. 'color: #111'
  67. )
  68. if (JSON.parse(evt.data).Type ==='Heart') {
  69. return
  70. }
  71. const telWSData = JSON.parse(evt.data)
  72. dealMsg(JSON.parse(evt.data))
  73. // 修改 store 中telwebsocket的数据
  74. }
  75. // 连接关闭的回调方法
  76. ws.onclose = function (evt) {
  77. if (cls === 0) {
  78. cls = 1
  79. console.log('telWebsocket连接关闭')
  80. reconnect()
  81. }
  82. }
  83. // 连接发生错误的回调方法
  84. ws.onerror = function (evt) {
  85. // 产生异常
  86. console.warn('telWebsocket连接出现异常!')
  87. if (ws == null || ws.readyState !== ws.OPEN) {
  88. console.log(new Date() + 'telWebsocket开始重连')
  89. reconnect()
  90. }
  91. }
  92. }
  93. /**
  94. * 重新连接
  95. */
  96. function reconnect() {
  97. if (lockReconnect) return
  98. lockReconnect = true
  99. // 没连接上会一直重连,设置延迟避免请求过多
  100. websocktTimer && clearTimeout(websocktTimer)
  101. websocktTimer = setTimeout(function () {
  102. console.log(new Date() + ' ' + 'telWebsocket重连中……')
  103. // createWebSocket()
  104. lockReconnect = false
  105. }, 2000)
  106. }
  107. /**
  108. * 发送
  109. * @ socketDatas 发送的数据 type {}
  110. */
  111. export function Send(scoketDatas, callback) {
  112. globalCallback = callback
  113. if (ws.readyState !== ws.OPEN) {
  114. reconnect()
  115. }
  116. if (ws.readyState === ws.OPEN) {
  117. console.log(
  118. '%c' +
  119. new Date() +
  120. '%c send %c telWebsocket ' +
  121. JSON.stringify(scoketDatas),
  122. 'color: #111',
  123. 'color: #409EFF',
  124. 'color: #111'
  125. )
  126. ws.send(JSON.stringify(scoketDatas))
  127. }
  128. }
  129. // 心跳检测
  130. const heartCheck = {
  131. timeout: 100*1000, // 5秒
  132. timeoutObj: null,
  133. serverTimeoutObj: null,
  134. reset: function () {
  135. clearTimeout(this.timeoutObj)
  136. clearTimeout(this.serverTimeoutObj)
  137. return this
  138. },
  139. start: function () {
  140. const _self = this
  141. this.timeoutObj = setTimeout(function () {
  142. // 这里发送一个心跳,后端收到后,返回一个心跳消息,
  143. // onmessage拿到返回的心跳就说明连接正常
  144. _self.sendHeart()
  145. }, this.timeout)
  146. },
  147. sendHeart: function () {
  148. const scoketDatas = {
  149. Type: 'Heart',
  150. Result: true,
  151. AgentId: useSocketStore().accountNumber, // 工号
  152. AgentExten: useSocketStore().extensionNumber, // 分机号
  153. AgentGroup: useSocketStore().groupNumber // 坐席组id
  154. }
  155. console.log(useSocketStore())
  156. Send(scoketDatas)
  157. }
  158. }
  159. /**
  160. * 处理接收的消息
  161. * @ msgData 接收的数据 type {}
  162. */
  163. function dealMsg(msgData) {
  164. if (msgData) {
  165. if (globalCallback) {
  166. globalCallback(msgData)
  167. }
  168. if (msgData.Result !== true) {
  169. if (errorType.indexOf(msgData.Type)!==-1) {
  170. ElMessage.error(msgData.Result)
  171. }
  172. }
  173. // const rlt = msgData.Result
  174. // if (rlt === true) {
  175. // switch (msgData.Type.toLowerCase()) {
  176. // case 'incoming': // 来电
  177. // IncomingBack(msgData)
  178. // break
  179. // }
  180. // } else if (rlt === false) {
  181. // const errCode = msgData.Error % 10000
  182. // console.error(`telWebsocket错误信息: ${errTips}(errorCode:${errCode})!`)
  183. // } else {
  184. // if (msgData.Type.toLowerCase() === 'linestate') {
  185. // LineStateAgentBack(msgData)
  186. // } else if (msgData.Type.toLowerCase() === 'monitor') {
  187. // //console.log(msgData)
  188. // updateSeatMonitorlists(msgData)
  189. // } else {
  190. // }
  191. // // Message.error(`telWebsocket错误信息: ${rlt} !`)
  192. // }
  193. }
  194. }
  195. // function updateSeatMonitorlists(msgData) {}
  196. export { ws }