wsService.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { getToken, generateUUID } from "@/api/auth";
  2. const url = 'wss://' + process.env.VUE_APP_WEBSCOKET + '/ws2/'
  3. class WsService {
  4. constructor(url) {
  5. this.url = url
  6. this.authStatus = false
  7. this.businessCallback = null
  8. this.wsClient = null
  9. this.initInterval()
  10. this.initWebSocket()
  11. }
  12. initInterval() {
  13. this.interval = 3000
  14. }
  15. initWebSocket() {
  16. this.wsClient = new WebSocket(this.url)
  17. this.initInterval()
  18. this.wsClient.onmessage = (e) => {
  19. let msg = JSON.parse(e.data)
  20. if (msg.type === 'ping') this.wsClient.send("wsService check")
  21. if (msg.type === 'auth' && msg.code === 1) this.authStatus = true
  22. if (typeof this.businessCallback === 'function') {
  23. this.businessCallback(msg)
  24. this.businessCallback = null
  25. }
  26. }
  27. this.wsClient.onopen = () => {
  28. this.log('websocket open')
  29. this.userAuth()
  30. }
  31. this.wsClient.onerror = (event) => {
  32. this.log('websocket error', event)
  33. }
  34. this.wsClient.onclose = (e) => {
  35. this.log('websocket close', e)
  36. this.authStatus = false
  37. if (this.interval > 0) {
  38. let intervalId = setInterval(function () {
  39. this.log("interval start")
  40. if (this.wsClient.readyState === WebSocket.OPEN) {
  41. this.log("interval close")
  42. clearInterval(intervalId)
  43. } else {
  44. this.log("interval initWebSocket")
  45. this.initWebSocket()
  46. }
  47. }.bind(this), this.interval)
  48. }
  49. }
  50. }
  51. getAuthStatus() {
  52. return this.authStatus
  53. }
  54. getUrl() {
  55. return this.url
  56. }
  57. setCallback(fun) {
  58. if (typeof fun === 'function') this.businessCallback = fun
  59. }
  60. getReadyState() {
  61. return this.wsClient.readyState
  62. }
  63. userAuth() {
  64. let token = getToken()
  65. let uuid = generateUUID()
  66. if (!token || !uuid || !this.wsClient) return
  67. this.wsClient.send(JSON.stringify({ type: 'auth', token: token, machine: uuid }))
  68. }
  69. send(data, fun) {
  70. if (this.wsClient.readyState !== WebSocket.OPEN) return
  71. this.businessCallback = typeof fun === 'function' ? fun : null
  72. this.wsClient.send(typeof data === 'string' ? data : JSON.stringify(data))
  73. }
  74. close() {
  75. this.wsClient.close()
  76. }
  77. closeForce() {
  78. this.interval = 0
  79. this.wsClient.close()
  80. }
  81. log(message, ...args) {
  82. if (process.env.NODE_ENV === 'development') console.log(message, ...args)
  83. }
  84. }
  85. class WsClient {
  86. constructor(url) {
  87. this.url = url
  88. this.authStatus = false
  89. this.businessCallback = null
  90. this.errorCallback = null
  91. this.closeCallback = null
  92. this.userAuthCallback = null
  93. this.wsClient = null
  94. }
  95. initWebSocket(userAuthCallback, businessCallback, errorCallback, closeCallback) {
  96. if (this.wsClient) {
  97. this.wsClient.close()
  98. this.authStatus = false
  99. this.businessCallback = null
  100. this.businessFun = null
  101. this.errorCallback = null
  102. this.closeCallback = null
  103. this.userAuthCallback = null
  104. this.wsClient = null
  105. }
  106. this.wsClient = new WebSocket(this.url)
  107. if (typeof userAuthCallback === 'function') this.userAuthCallback = userAuthCallback
  108. if (typeof businessCallback === 'function') this.businessCallback = businessCallback
  109. if (typeof errorCallback === 'function') this.errorCallback = errorCallback
  110. if (typeof closeCallback === 'function') this.closeCallback = closeCallback
  111. this.wsClient.onmessage = (e) => {
  112. let msg = JSON.parse(e.data)
  113. if (msg.type === 'ping') this.wsClient.send("WsClient check")
  114. if (msg.type === 'auth' && msg.code === 1) {
  115. this.authStatus = true
  116. if (typeof this.userAuthCallback === 'function') {
  117. this.userAuthCallback(msg)
  118. this.userAuthCallback = null
  119. }
  120. }
  121. /*一次性回调*/
  122. if (typeof this.businessCallback === 'function') {
  123. this.businessCallback(msg)
  124. this.businessCallback = null
  125. }
  126. /*长期回调*/
  127. if (typeof this.businessFun === 'function') this.businessFun(msg)
  128. }
  129. this.wsClient.onopen = () => {
  130. this.log('WsClient open')
  131. this.userAuth()
  132. }
  133. this.wsClient.onerror = (event) => {
  134. this.log('WsClient error', event)
  135. if (typeof this.errorCallback === 'function') {
  136. this.errorCallback(event)
  137. this.errorCallback = null
  138. }
  139. }
  140. this.wsClient.onclose = (e) => {
  141. this.log('WsClient close', e)
  142. this.authStatus = false
  143. if (typeof this.closeCallback === 'function') {
  144. this.closeCallback(e)
  145. this.closeCallback = null
  146. }
  147. }
  148. }
  149. setErrorCallback(fun) {
  150. this.errorCallback = typeof fun === 'function' ? fun : null
  151. return this
  152. }
  153. setCloseCallback(fun) {
  154. this.closeCallback = typeof fun === 'function' ? fun : null
  155. return this
  156. }
  157. setUserAuthCallback(fun) {
  158. this.userAuthCallback = typeof fun === 'function' ? fun : null
  159. return this
  160. }
  161. setBusinessFun(fun) {
  162. this.businessFun = typeof fun === 'function' ? fun : null
  163. return this
  164. }
  165. getAuthStatus() {
  166. return this.authStatus
  167. }
  168. userAuth() {
  169. let token = getToken()
  170. let uuid = generateUUID()
  171. if (!token || !uuid || !this.wsClient) return
  172. this.send(JSON.stringify({ type: 'auth', token: token, machine: uuid }))
  173. }
  174. send(data, businessCallback) {
  175. if (this.wsClient.readyState !== WebSocket.OPEN) return
  176. this.businessCallback = typeof businessCallback === 'function' ? businessCallback : null
  177. this.wsClient.send(typeof data === 'string' ? data : JSON.stringify(data))
  178. }
  179. close() {
  180. if (this.wsClient) this.wsClient.close()
  181. }
  182. log(message, ...args) {
  183. if (process.env.NODE_ENV === 'development') console.log(message, ...args)
  184. }
  185. }
  186. export const wsClient = new WsClient(url)