import WebSocket from 'ws'; import EventEmitter from 'events'; // 打印组件核心管理类 const defaultComponents = [ { name: '抖音', port: '13888' }, { name: '拼多多', port: '5000' }, { name: '快手', port: '16888/ks/printer' }, { name: '京东', port: '9113' }, { name: '菜鸟', port: '13528' }, { name: '视频号', port: '12705' }, { name: '小红书', port: '10818' }, { name: 'LODOP', port: '8000/c_webskt' } ]; class PrinterComponentCore extends EventEmitter { constructor(app) { super(); this.app = app; this.components = new Map(); // {name: ws实例} this.ports = new Map(); // {name: port} this.status = new Map(); // {name: status} this.reconnectTimers = new Map(); // {name: timer} this.reconnectInterval = 3000; defaultComponents.forEach(c => { this.ports.set(c.name, c.port); this.status.set(c.name, 'disconnected'); }); } // 统一初始化所有组件 initAll() { defaultComponents.forEach(c => { this.initComponent(c.name); }); } // 初始化单个组件,带自动重连 initComponent(name) { const port = this.ports.get(name); if (!port) return; let url = port.includes('/') ? `ws://127.0.0.1:${port}` : `ws://127.0.0.1:${port}`; const connect = () => { if (this.components.has(name)) { try { this.components.get(name).terminate(); } catch {} this.components.delete(name); } if (this.reconnectTimers.has(name)) { clearTimeout(this.reconnectTimers.get(name)); this.reconnectTimers.delete(name); } const ws = new WebSocket(url); ws.on('open', () => { this._updateStatus(name, 'connected'); console.log(`[${name}] 连接成功: ${url}`); }); ws.on('close', () => { this._updateStatus(name, 'disconnected'); //console.log(`[${name}] 连接关闭,准备重连`); if (!ws._manualClose) { this._reconnect(name, connect); } }); ws.on('message', msg => { if (Buffer.isBuffer(msg)) { msg = msg.toString('utf8'); } console.log(`[=======>${name}] 收到消息:`, msg); switch (name) { case 'LODOP': this.app.emit('lodopMessage', msg); break; case '抖音': this.app.emit('douyinMessage', msg); break; case '拼多多': this.app.emit('pinduoduoMessage', msg); break; case '快手': this.app.emit('kuaishouMessage', msg); break; case '京东': this.app.emit('jingdongMessage', msg); break; case '菜鸟': this.app.emit('cainiaoMessage', msg); break; case '视频号': this.app.emit('shipinhaoMessage', msg); break; case '小红书': this.app.emit('xiaohongshuMessage', msg); break; default: this.app.emit('printerComponentMessage', { name, message: msg }); } }); ws.on('error', err => { this._updateStatus(name, 'disconnected'); //console.log(`[${name}] 连接错误:`, err.message); if (!ws._manualClose) { this._reconnect(name, connect); } }); this.components.set(name, ws); }; connect(); } _reconnect(name, connectFn) { if (this.reconnectTimers.has(name)) return; this.reconnectTimers.set(name, setTimeout(() => { connectFn(); this.reconnectTimers.delete(name); }, this.reconnectInterval)); //console.log(`[${name}] ${this.reconnectInterval}ms 后重试连接...`); } addComponent(name, wsInstance) { this.components.set(name, wsInstance); } getComponent(name) { return this.components.get(name); } removeComponent(name) { if (this.components.has(name)) { const ws = this.components.get(name); if (ws) { ws._manualClose = true; if (typeof ws.close === 'function') ws.close(); } this.components.delete(name); this.status.set(name, 'disconnected'); if (this.reconnectTimers.has(name)) { clearTimeout(this.reconnectTimers.get(name)); this.reconnectTimers.delete(name); } } } getAllComponents() { return Array.from(this.components.entries()).map(([name, ws]) => ({ name, ws })); } getPort(name) { return this.ports.get(name); } getAllPorts() { return Array.from(this.ports.entries()).map(([name, port]) => ({ name, port })); } getStatus(name) { return this.status.get(name); } getAllStatus() { return Array.from(this.status.entries()).map(([name, status]) => ({ name, status })); } // 更新状态并触发事件 _updateStatus(name, newStatus) { //检测原来的状态,如果发生变化才触发事件 if (this.status.get(name) !== newStatus) { this.status.set(name, newStatus); this.app.emit('printerComponentStatusChange', { name, status: newStatus }); } } } export default PrinterComponentCore;