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.
 
 

186 lines
6.2 KiB

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.onReceipts = [];
this.onReports = [];
this.onSendraws = [];
this.onConnects = [];
this.onDisConconnects = [];
}
call_onReceipts(message, receipt) {
for (var i = 0; i < this.onReceipts.length; i++) {
this.onReceipts[i](message, receipt);
}
}
call_onReports(message) {
for (var i = 0; i < this.onReports.length; i++) {
this.onReports[i](message);
}
}
call_onSendraw(message) {
for (var i = 0; i < this.onSendraws.length; i++) {
this.onSendraws[i](message);
}
}
call_onConnect() {
for (var i = 0; i < this.onConnects.length; i++) {
this.onConnects[i]();
}
}
call_onDisConconnect() {
for (var i = 0; i < this.onDisConconnects.length; i++) {
this.onDisConconnects[i]();
}
}
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");
ThisClass.call_onConnect();
}
this.cmdServer.onerror = function () {
ThisClass.connecting = false;
ThisClass.connected = false;
console.error("ZcmdService connect to server error");
ThisClass.connectToCmdServer();
};
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 {
ThisClass.call_onReports(obj);
}
}
this.cmdServer.onclose = function () {
ThisClass.connected = false;
console.warn("websocket close");
ThisClass.call_onDisConconnect();
}
};
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)
ThisClass.call_onReceipts(_txcontext.message, receipt)
}
this.cmdServerListener.set(messageId, txcontext);
this.call_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.onReceipts.push(onReceipt);
}
set_onReport(onReport) {
this.onReports.push(onReport);
}
set_onSendraw(onSendraw) {
this.onSendraws.push(onSendraw);
}
set_onConnect(onConnect) {
this.onConnects.push(onConnect);
}
set_onDisConconnect(onDisConconnect) {
this.onDisConconnects.push(onDisConconnect);
}
}
// var zcmd = new ZCommand();
// zcmd.start_auto_connect(() => { });