UNPKG

xcloudsdk

Version:

xcloud sdk

206 lines (205 loc) 9.46 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const entity_1 = require("../entity"); const utils_1 = require("../utils"); const appSdkBase_1 = require("./appSdkBase"); class CloudPrint extends appSdkBase_1.appSdkBase { constructor() { super("Print"); this.isOpen = false; this.socketTask = null; this.printerList = []; this.printResult = []; } // 启动心跳机制 startHeartbeat() { if (this.hearteatInterval) { clearInterval(this.hearteatInterval); } this.hearteatInterval = setInterval(() => { if (this.socketTask && this.isOpen) { this.socketTask.send({ data: "3", }); // 发送 ping } }, 10000); // Socket.IO 默认心跳间隔 25 秒 } close() { this.isOpen = false; clearInterval(this.hearteatInterval); } Init(printServer) { if (this.socketTask) { this.socketTask.close({}); } return new Promise((resolve, reject) => { this.socketTask = uni.connectSocket({ url: printServer.wssUrl, complete: () => { }, }); this.socketTask.onMessage((res) => __awaiter(this, void 0, void 0, function* () { var _a; console.log("收到服务器内容:" + res.data); var respStr = res.data; var regGroup = ["", "", ""]; var temp = /^([0-9]{1,2})(.*?)$/gi.exec(respStr); if (temp != null) { regGroup = temp; } const code = regGroup[1]; const messageStr = regGroup[2]; if (code == `2`) { (_a = this.socketTask) === null || _a === void 0 ? void 0 : _a.send({ data: "3", }); //心跳反馈 } else if (code == `0`) { this.startHeartbeat(); //由于事件绑定原因,有可能执行不到 //连接完成,将服务端配置设置上来 //var k = `"0{"sid":"a1ea3039-8c52-441b-bba1-d2e5035edce6","upgrades":[],"pingInterval":60000,"pingTimeout":60000}"`; } // else if (code == `40`) { // //由于事件绑定原因,有可能执行不到 // return; // } else if (code == `41`) { // //主动断开连接 // return; // } else else if (code == `42`) { const messageObj = JSON.parse(messageStr); const eventName = messageObj[0]; //判断是否是数组 if (Array.isArray(messageObj) && messageObj.length == 2) { switch (eventName) { case "clients": this.printerList = []; yield (0, utils_1.SyncEachObj)(messageObj[1], (obj, key) => __awaiter(this, void 0, void 0, function* () { if (obj && obj.printerList) { const printList = obj.printerList; if (printList && printList.length > 0) { this.printerList.push(...printList.map((m) => { return { displayName: m.displayName, name: m.name, description: m.description, status: m.status, hostName: obj.hostname, isDefault: m.isDefault, ip: obj.ip, client: obj.clientId, }; })); } } return true; })); break; case "printerList": const printerList = messageObj[1]; if (printerList.length > 0) { this.printerList = printerList.map((m) => { return { displayName: m.displayName, name: m.name, hostName: m.server.hostname, isDefault: m.isDefault, ip: m.server.ip, client: m.server.clientId, description: m.description, status: m.status, }; }); } resolve(entity_1.SdkResult.Success(this.printerList)); break; case "render-print-success": case "error": case "render-print-error": this.printResult.forEach((m) => { m(eventName, messageObj[1]); }); this.printResult = []; break; } } } else { console.log("code:" + res.data); } })); this.socketTask.onOpen((res) => { this.isOpen = true; console.log("WebSocket 连接已打开"); // 发送消息 uni.sendSocketMessage({ data: `40${JSON.stringify({ token: printServer.token, })}`, }); }); this.socketTask.onClose((res) => { this.close(); console.log("WebSocket 已关闭!"); }); this.socketTask.onError((res) => { this.close(); reject(res); console.log("WebSocket 连接打开失败,请检查!"); }); }); } Print(template, data, client, printerName) { return new Promise((resolve, reject) => { let printer = this.printerList.find((m) => m.client == client && m.isDefault); if (printerName) { printer = this.printerList.find((m) => m.client == client && m.name === printerName); } if (printer) { this.printResult.push((eventName, resultObj) => { var msg = resultObj === null || resultObj === void 0 ? void 0 : resultObj.msg; if (eventName.includes("print-success")) { resolve(entity_1.SdkResult.Success(msg || "打印成功!")); } else { resolve(entity_1.SdkResult.Fail(msg || "打印失败,请联系管理员!")); } }); const printObj = { data, template: template, client: printer.client, printer: printerName ? printer.name : "", pageSize: { height: template.panels[0].height * 1000, width: template.panels[0].width * 1000, }, }; if (this.socketTask && this.isOpen) { this.socketTask.send({ data: `42["render-print",${JSON.stringify(printObj)}]`, }); } } else { reject(entity_1.SdkResult.Fail(`没有找到打印机或连接未打开!`)); } }); } Close() { var _a; (_a = this.socketTask) === null || _a === void 0 ? void 0 : _a.close({}); return entity_1.SdkResult.Success(null); } } exports.default = new CloudPrint();