walter 7 月之前
父节点
当前提交
d228afe825
共有 1 个文件被更改,包括 232 次插入0 次删除
  1. 232 0
      src/api/wsService.js

+ 232 - 0
src/api/wsService.js

@@ -0,0 +1,232 @@
+
+import {getToken,generateUUID} from "@/api/auth";
+
+const url = 'wss://' + process.env.VUE_APP_WEBSCOKET + '/ws2/'
+class WsService {
+    constructor(url){
+        this.url = url
+        this.authStatus = false
+        this.businessCallback = null
+        this.wsClient = null
+        this.initInterval()
+        this.initWebSocket()
+    }
+
+    initInterval(){
+        this.interval = 3000
+    }
+
+    initWebSocket(){
+        this.wsClient = new WebSocket(this.url)
+        this.initInterval()
+        this.wsClient.onmessage = (e) => {
+            let msg = JSON.parse(e.data)
+            if(msg.type === 'ping') this.wsClient.send("wsService check")
+            if(msg.type === 'auth' && msg.code === 1) this.authStatus = true
+
+            if (typeof this.businessCallback === 'function') {
+                this.businessCallback(msg)
+                this.businessCallback = null
+            }
+        }
+        this.wsClient.onopen = () => {
+            this.log('websocket open')
+            this.userAuth()
+        }
+        this.wsClient.onerror = (event) => {
+            this.log('websocket error',event)
+        }
+        this.wsClient.onclose = (e) => {
+            this.log('websocket close',e)
+            this.authStatus = false
+            if (this.interval > 0){
+                let intervalId = setInterval(function () {
+                    this.log("interval start")
+                    if (this.wsClient.readyState === WebSocket.OPEN) {
+                        this.log("interval close")
+                        clearInterval(intervalId)
+                    } else {
+                        this.log("interval initWebSocket")
+                        this.initWebSocket()
+                    }
+                }.bind(this),this.interval)
+            }
+        }
+    }
+
+    getAuthStatus(){
+        return this.authStatus
+    }
+
+    getUrl(){
+        return this.url
+    }
+
+    setCallback(fun){
+        if (typeof fun === 'function') this.businessCallback = fun
+    }
+
+    getReadyState(){
+        return this.wsClient.readyState
+    }
+
+    userAuth(){
+
+        let token = getToken()
+        let uuid = generateUUID()
+        if (!token || !uuid || !this.wsClient) return
+
+        this.wsClient.send(JSON.stringify({type:'auth',token:token,machine:uuid}))
+    }
+
+
+
+    send(data,fun){
+        if (this.wsClient.readyState !== WebSocket.OPEN) return
+        this.businessCallback = typeof fun === 'function' ? fun : null
+
+        this.wsClient.send(typeof data === 'string' ? data : JSON.stringify(data))
+    }
+
+
+    close(){
+        this.wsClient.close()
+    }
+
+    closeForce(){
+        this.interval = 0
+        this.wsClient.close()
+    }
+
+    log(message,...args){
+        if (process.env.NODE_ENV === 'development') console.log(message,...args)
+    }
+
+}
+
+
+class WsClient{
+    constructor(url){
+        this.url = url
+        this.authStatus = false
+        this.businessCallback = null
+        this.errorCallback = null
+        this.closeCallback = null
+        this.userAuthCallback = null
+        this.wsClient = null
+    }
+
+    initWebSocket(userAuthCallback,businessCallback,errorCallback,closeCallback){
+        if (this.wsClient) {
+            this.wsClient.close()
+            this.authStatus = false
+            this.businessCallback = null
+            this.businessFun = null
+            this.errorCallback = null
+            this.closeCallback = null
+            this.userAuthCallback = null
+            this.wsClient = null
+        }
+
+        this.wsClient = new WebSocket(this.url)
+        if (typeof userAuthCallback === 'function') this.userAuthCallback = userAuthCallback
+        if (typeof businessCallback === 'function') this.businessCallback = businessCallback
+        if (typeof errorCallback === 'function') this.errorCallback = errorCallback
+        if (typeof closeCallback === 'function') this.closeCallback = closeCallback
+        this.wsClient.onmessage = (e) => {
+            let msg = JSON.parse(e.data)
+            if(msg.type === 'ping') this.wsClient.send("WsClient check")
+            if(msg.type === 'auth' && msg.code === 1) {
+                this.authStatus = true
+                if (typeof this.userAuthCallback === 'function') {
+                    this.userAuthCallback(msg)
+                    this.userAuthCallback = null
+                }
+            }
+            /*一次性回调*/
+            if (typeof this.businessCallback === 'function') {
+
+                this.businessCallback(msg)
+                this.businessCallback = null
+            }
+            /*长期回调*/
+            if (typeof this.businessFun === 'function') this.businessFun(msg)
+        }
+        this.wsClient.onopen = () => {
+            this.log('WsClient open')
+            this.userAuth()
+        }
+        this.wsClient.onerror = (event) => {
+            this.log('WsClient error',event)
+            if (typeof this.errorCallback === 'function') {
+                this.errorCallback(event)
+                this.errorCallback = null
+            }
+        }
+        this.wsClient.onclose = (e) => {
+            this.log('WsClient close',e)
+            this.authStatus = false
+            if (typeof this.closeCallback === 'function') {
+                this.closeCallback(e)
+                this.closeCallback = null
+            }
+        }
+    }
+
+    setErrorCallback(fun){
+        this.errorCallback = typeof fun === 'function' ? fun : null
+        return this
+    }
+
+    setCloseCallback(fun){
+        this.closeCallback = typeof fun === 'function' ? fun : null
+        return this
+    }
+
+    setUserAuthCallback(fun){
+        this.userAuthCallback = typeof fun === 'function' ? fun : null
+        return this
+    }
+
+    setBusinessFun(fun){
+        this.businessFun = typeof fun === 'function' ? fun : null
+        return this
+    }
+
+    getAuthStatus(){
+        return this.authStatus
+    }
+
+    userAuth(){
+        let token = getToken()
+        let uuid = generateUUID()
+        if (!token || !uuid || !this.wsClient) return
+
+        this.send(JSON.stringify({type:'auth',token:token,machine:uuid}))
+    }
+
+
+
+    send(data,businessCallback){
+        if (this.wsClient.readyState !== WebSocket.OPEN) return
+        this.businessCallback = typeof businessCallback === 'function' ? businessCallback : null
+
+        this.wsClient.send(typeof data === 'string' ? data : JSON.stringify(data))
+    }
+
+    close(){
+        if (this.wsClient) this.wsClient.close()
+    }
+
+
+    log(message,...args){
+        if (process.env.NODE_ENV === 'development') console.log(message,...args)
+    }
+
+}
+
+export const wsClient = new WsClient(url)
+
+
+
+