You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
function uuid() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ) } var ZWSURL = "ws://" + window.location.hostname + ":" + window.location.port + "/ws/cmd";
class ZCommand { /******************************************************************************************************************* * ===================================================BASE======================================================== * *******************************************************************************************************************/ constructor() { if (!('WebSocket' in window)) { alert('当前浏览器 Not support websocket') } this.connected = false; this.connecting = false; this.cmdServerListener = new Map(); this.onConnect; this.firstConnect = true; }
start_auto_connect(wsurl, onFirstConnect) { var ThisClass = this; this.onFirstConnect = onFirstConnect; this.wsurl = wsurl; ThisClass.connectToCmdServer(); setInterval(function () { if (!ThisClass.isConnect()) { console.info("ZcmdService try connect to server"); ThisClass.connectToCmdServer(); } }, 2000); }
isConnect() { return this.connected; }
isConnecting() { return this.connecting; } connectToCmdServer() { this.connecting = true; console.info('websocket create socket:' + this.wsurl); this.cmdServer = new WebSocket(this.wsurl); var ThisClass = this;
this.cmdServer.onopen = function () { ThisClass.connecting = false; ThisClass.connected = true; console.info("ZcmdService connect to server"); if (ThisClass.firstConnect && ThisClass.onFirstConnect) { ThisClass.onFirstConnect(); } ThisClass.firstConnect = false; }
this.cmdServer.onerror = function () { ThisClass.connecting = false; ThisClass.connected = false; console.info("websocket onerror"); }; var ThisClass = this; this.cmdServer.onmessage = function (event) { var obj = $.parseJSON(event.data); var messageId = obj["messageId"]; if (ThisClass.cmdServerListener.has(messageId)) { var txcontext = ThisClass.cmdServerListener.get(messageId); txcontext.onreceipt(txcontext, obj); ThisClass.cmdServerListener.delete(messageId); } else { if (ThisClass.onreport) { ThisClass.onreport(obj) } } } this.cmdServer.onclose = function () { ThisClass.connected = false; console.warn("websocket close"); } };
sendCmd(txcontext) { var ThisClass = this; var messageId = uuid();; txcontext.message["messageId"] = messageId; txcontext.message["need_receipt"] = true;
// TODO:这里修改成异步等待链接成功,并发送
if (!this.isConnect()) { console.error("Servcer is unconnected"); setTimeout(function () { txcontext.onOvertime(); }, txcontext.overtime) return; } txcontext.timerId = setTimeout(function () { console.error("cmd is overtime"); txcontext.onOvertime(); ThisClass.cmdServerListener.delete(messageId); }, txcontext.overtime)
txcontext.onreceipt = function (_txcontext, receipt) { clearTimeout(_txcontext.timerId) _txcontext.onMessage(receipt) if (ThisClass.onreceipt) { ThisClass.onreceipt(_txcontext.message, receipt) } } this.cmdServerListener.set(messageId, txcontext); if (this.onsendraw) { this.onsendraw(txcontext.message); } this.cmdServer.send(JSON.stringify(txcontext.message)); };
/******************************************************************************************************************* * =================================================通用发送消息================================================== * *******************************************************************************************************************/ send_message(jsonobject, _overtime = 3000) { var p = new Promise( (resolve, reject) => { this.sendCmd({ message: jsonobject, onMessage: function (message) { if (resolve) resolve(message); }, onOvertime: function () { }, onError: function () { }, overtime: _overtime, }); } ) return p; }; set_onreceipt(onreceipt) { this.onreceipt = onreceipt; } set_onreport(onreport) { this.onreport = onreport; } set_onsendraw(onsendraw) { this.onsendraw = onsendraw; } } // var zcmd = new ZCommand();
// zcmd.start_auto_connect(() => { });
|