diff --git a/src/utils/XDNoticeSdk.js b/src/utils/XDNoticeSdk.js new file mode 100644 index 00000000..3742e5b6 --- /dev/null +++ b/src/utils/XDNoticeSdk.js @@ -0,0 +1,255 @@ +'use strict' +/* + XDNoticeSdk + */ +class XDNoticeSdk { + + VERSION="1.0.0" + MAX_SENT_RETRY_COUNT = 9; + WS_URL = 'ws://'+process.env.VUE_APP_WS_HOST+'/ws/'; + + userId = null; + + + AUTH_TOKEN=null; + + WS_INIT = 0; + WS_OPEN = 1; + WS_CLOSING = 2; + WS_CLOSE = 3; + + // 和服务端连接的socket对象 + ws = null; + // WS 回调函数 + onOpenCallback =[]; + onCloseCallback=[]; + onErrorCallback =[]; + // onMessageCallback =[]; + + onMessageCallback=[]; + + // 自定义 回调函数 + callbacks ={} + + // 标识:是否连接成功 , 记录重试的次数,重新连接尝试的次数 + connected = false; + printerIsRead = false; + sendRetryCount = 0; + connectRetryCount = 0; + + static instance = null; + constructor() { + this.instance = null + } + static getInstance() { + if (!this.instance) { + this.instance = new XDNoticeSdk() + } + return this.instance + } + // 外部方法---------------------------------------------------------------- + init(userId){ + // 初始化参数建立连接 + // this.token=getToken(); + this.userId=userId; + this.connect(); + this.start(); + } + + setOnOpen(callback){ + this.onOpenCallback.push(callback); + + if(this.connected){ + if(callback!=null){ + callback(); + } + } + } + + setOnClose(callback){ + this.onCloseCallback.push(callback); + + if(!this.connected){ + if(callback!=null){ + callback(); + } + } + } + + setOnError(callback){ + this.onErrorCallback.push(callback); + } + + setOnMessage(callback){ + this.onMessageCallback.push(callback); + if(this.connected){ + if(callback!=null){ + callback(); + } + } + + } + + getRequestId() { + let requestId = 'req' + new Date().getTime().toString(36) + Math.random().toString(36).substring(2, 9); + return requestId; + } + + //--内部方法-------------------------------------------------------------- + + // 连接服务器的方法 + connect() { + // 连接服务器 + if (!window.WebSocket) { + console.log('您的浏览器不支持WebSocket'); + return null; + } + + if (!this.ws || this.getWSReadyState() === this.WS_CLOSING || this.getWSReadyState() === this.WS_CLOSE) { + try { + this.ws = new WebSocket(this.WS_URL+""+this.userId); + + this.ws.onerror = () => { + // console.log("ws onerror!"); + if(this.onErrorCallback.length>0){ + this.onErrorCallback.forEach(callback=>{ + if(callback!=null){ + callback(); + } + }) + } + } + this.ws.onopen = () => { + // console.log("ws onopen!"); + + this.connected = true + this.connectRetryCount = 0 + + if(this.onOpenCallback.length>0){ + this.onOpenCallback.forEach(callback=>{ + if(callback!=null){ + callback(); + } + }) + } + + }; + + this.ws.onpong = () => { + // console.log("ws onpong!"); + } + + this.ws.onclose = () => { + // console.log("ws onclose!"); + + this.connected = false; + this.connectRetryCount++; + + if(this.connectRetryCount>120){ + this.connectRetryCount=0; + } + + if(this.onCloseCallback.length>0){ + this.onCloseCallback.forEach(callback=>{ + if(callback!=null){ + callback(); + } + }) + } + // * this.connectRetryCount + + setTimeout(() => { + this.connect() + }, 1000 ) + }; + + this.ws.onmessage = e => { + // console.log('ws onmessage:' + e.data) + if(this.onMessageCallback.length>0){ + this.onMessageCallback.forEach(callback=>{ + if(callback!=null){ + callback(e); + } + }) + } + // if (e.data.charAt(0) === "{") { + // var data = this.jsonToObj(e.data); + // if(this.onMessageCallback){ + // this.onMessageCallback(data); + // } + // // if(msg["CMD"]=='printerIsRead'){ + // // printerIsRead=true; + // // } + // // else{ + + // // } + // } else { + // console.log("数据格式非法:" + e.data); + // } + }; + + } catch (ex) { + setTimeout(() => { + this.connect() + }, 1000 ) + + } + } + + return this.ws; + } + + jsonToObj(json){ + return JSON.parse(json) + } + + objToJson(obj){ + return JSON.stringify(obj) + } + + /** + * readyState->0:尚未建立连接;1:已经建立连接;2:正在关闭;3:已经关闭或不可用 + * @param thisWs + * @returns {*} + */ + getWSReadyState() { + if (this.ws.readyState !== undefined) { + return this.ws.readyState; + } + return this.WS_INIT; + }; + + // 发送数据的方法 + send(data) { + // console.log("send:"+data) + if (this.getWSReadyState() === this.WS_OPEN ) { + this.sendRetryCount = 0 + this.ws.send(data) + return 1; + } else { + console.log("Ws is abnormal,send faild!!") + return -1 + } + } + + // 关闭socket连接 + close() { + if (this.ws) { + this.ws.close(); + this.connected = false; // 更新连接状态 + clearInterval(this.heartbeatInterval); // 清除心跳检测定时器 + } + } + // 开启心跳检测 + start() { + // this.heartbeatInterval = setInterval(() => { + // if (this.getWSReadyState() === this.WS_OPEN ) { + // // this.ws.ping(); + // this.ws.send(0x9); + // } + // }, 1000); + } +} + + +export default XDNoticeSdk;