2026-05-12 08:48:33 +08:00
|
|
|
|
// src/utils/WebSocketManager.ts
|
|
|
|
|
|
|
|
|
|
|
|
type WSStatus = 'disconnected' | 'connecting' | 'connected';
|
|
|
|
|
|
|
|
|
|
|
|
type MessageHandler = (data: any) => void;
|
|
|
|
|
|
|
|
|
|
|
|
interface WSMessage {
|
2026-06-05 15:57:36 +08:00
|
|
|
|
type: string;
|
|
|
|
|
|
[key: string]: any;
|
2026-05-12 08:48:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketManager {
|
2026-06-05 15:57:36 +08:00
|
|
|
|
private static instance: WebSocketManager | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
private ws: WebSocket | null = null;
|
|
|
|
|
|
private status: WSStatus = 'disconnected';
|
|
|
|
|
|
private messageHandlers: MessageHandler[] = [];
|
|
|
|
|
|
private reconnectTimer: NodeJS.Timeout | null = null;
|
|
|
|
|
|
private currentSerialRef: string | null = null;
|
|
|
|
|
|
private isManualCloseRef: boolean = false;
|
|
|
|
|
|
private wsSerialRef: string | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
// 状态监听器
|
|
|
|
|
|
private statusListeners: ((status: WSStatus) => void)[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
// 🔒 私有构造函数,防止外部 new
|
|
|
|
|
|
private constructor() { }
|
|
|
|
|
|
|
|
|
|
|
|
// 🎯 获取单例实例
|
|
|
|
|
|
public static getInstance(): WebSocketManager {
|
|
|
|
|
|
if (!WebSocketManager.instance) {
|
|
|
|
|
|
WebSocketManager.instance = new WebSocketManager();
|
|
|
|
|
|
}
|
|
|
|
|
|
return WebSocketManager.instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 状态管理 ====================
|
|
|
|
|
|
|
|
|
|
|
|
public getStatus(): WSStatus {
|
|
|
|
|
|
return this.status;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public setStatus(status: WSStatus) {
|
|
|
|
|
|
this.status = status;
|
|
|
|
|
|
// 通知所有状态监听器
|
|
|
|
|
|
this.statusListeners.forEach(listener => listener(status));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public onStatusChange(listener: (status: WSStatus) => void): () => void {
|
|
|
|
|
|
this.statusListeners.push(listener);
|
|
|
|
|
|
listener(this.status);
|
|
|
|
|
|
// 返回取消订阅函数
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
const index = this.statusListeners.indexOf(listener);
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
|
|
this.statusListeners.splice(index, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 连接管理 ====================
|
|
|
|
|
|
|
|
|
|
|
|
public connect(serialNumber: string, wsUrl: string): void {
|
|
|
|
|
|
console.log("创建开始");//ws://192.168.2.56:9002/ws/
|
|
|
|
|
|
// 清除之前的重连计时器
|
|
|
|
|
|
if (this.reconnectTimer) {
|
|
|
|
|
|
clearTimeout(this.reconnectTimer);
|
|
|
|
|
|
this.reconnectTimer = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果序列号相同且已连接,跳过
|
|
|
|
|
|
if (this.ws && this.currentSerialRef === serialNumber && this.status === 'connected') {
|
|
|
|
|
|
console.log('WebSocket already connected for this device');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果序列号不同,先关闭旧连接
|
|
|
|
|
|
if (this.ws && this.currentSerialRef !== serialNumber) {
|
|
|
|
|
|
this.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.currentSerialRef = serialNumber;
|
|
|
|
|
|
this.setStatus('connecting');
|
|
|
|
|
|
this.isManualCloseRef = false;
|
|
|
|
|
|
|
|
|
|
|
|
console.log('Connecting WebSocket for', serialNumber);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (serialNumber == null || serialNumber.trim() === "") {
|
|
|
|
|
|
console.log("serialNumber 是空/空白,停止执行");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.ws = new WebSocket(wsUrl);
|
|
|
|
|
|
this.wsSerialRef = serialNumber;
|
|
|
|
|
|
|
|
|
|
|
|
this.ws.onopen = () => {
|
|
|
|
|
|
console.log('WebSocket Connected');
|
|
|
|
|
|
this.setStatus('connected');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const dataStr = localStorage.getItem('userInfo');
|
|
|
|
|
|
if (!dataStr) {
|
2026-05-12 08:48:33 +08:00
|
|
|
|
console.error('用户信息未找到,无法发送认证包');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-06-05 15:57:36 +08:00
|
|
|
|
let parse = JSON.parse(dataStr);
|
|
|
|
|
|
var token = parse?.token;
|
|
|
|
|
|
|
|
|
|
|
|
console.log('WebSocket%', token);
|
|
|
|
|
|
console.log('WebSocket%', parse);
|
|
|
|
|
|
console.log("开始发送认证包");//ws://192.168.2.56:9002/ws/
|
|
|
|
|
|
// 发送 AUTH 认证消息
|
|
|
|
|
|
this.send({
|
|
|
|
|
|
type: 'AUTH',
|
|
|
|
|
|
token: token,
|
|
|
|
|
|
userName: parse.username,
|
|
|
|
|
|
deviceId: serialNumber,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.ws.onmessage = (event) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
|
|
this.handleMessage(data);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('WS message parse error', e);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.ws.onerror = (err) => {
|
|
|
|
|
|
console.error('WebSocket error', err);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.ws.onclose = (event) => {
|
|
|
|
|
|
const { code, reason, wasClean } = event;
|
|
|
|
|
|
console.log('[WebSocket closed]', 'code =', code, 'reason =', reason || '(no reason)', 'wasClean =', wasClean);
|
|
|
|
|
|
|
|
|
|
|
|
// 如果不是当前设备的 ws,忽略
|
|
|
|
|
|
if (this.wsSerialRef !== this.currentSerialRef) {
|
|
|
|
|
|
console.log('🟡 ignore old ws close');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.ws = null;
|
|
|
|
|
|
this.setStatus('disconnected');
|
|
|
|
|
|
|
|
|
|
|
|
// 主动关闭不重连
|
|
|
|
|
|
if (this.isManualCloseRef || !this.currentSerialRef) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5 秒后自动重连
|
|
|
|
|
|
this.reconnectTimer = setTimeout(() => {
|
|
|
|
|
|
if (this.currentSerialRef === this.wsSerialRef) {
|
|
|
|
|
|
this.connect(this.currentSerialRef, wsUrl);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 5000);
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 断开连接 ====================
|
|
|
|
|
|
|
|
|
|
|
|
public close(): void {
|
|
|
|
|
|
this.isManualCloseRef = true;
|
|
|
|
|
|
|
|
|
|
|
|
if (this.reconnectTimer) {
|
|
|
|
|
|
clearTimeout(this.reconnectTimer);
|
|
|
|
|
|
this.reconnectTimer = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.ws) {
|
|
|
|
|
|
console.log('Closing WebSocket');
|
|
|
|
|
|
this.ws.close();
|
|
|
|
|
|
this.ws = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.currentSerialRef = null;
|
|
|
|
|
|
this.wsSerialRef = null;
|
|
|
|
|
|
this.setStatus('disconnected');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 消息发送 ====================
|
|
|
|
|
|
|
|
|
|
|
|
public send(data: WSMessage): boolean {
|
|
|
|
|
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
|
|
|
|
console.log('WebSocket not connected, cannot send message');
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log('WebSocket send message');
|
|
|
|
|
|
this.ws.send(JSON.stringify(data));
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 消息接收 ====================
|
|
|
|
|
|
|
|
|
|
|
|
private handleMessage(data: WSMessage): void {
|
|
|
|
|
|
// 通知所有消息处理器
|
|
|
|
|
|
this.messageHandlers.forEach(handler => handler(data));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public onMessage(handler: MessageHandler): () => void {
|
|
|
|
|
|
this.messageHandlers.push(handler);
|
|
|
|
|
|
// 返回取消订阅函数
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
const index = this.messageHandlers.indexOf(handler);
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
|
|
this.messageHandlers.splice(index, 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 工具方法 ====================
|
|
|
|
|
|
|
|
|
|
|
|
public getCurrentSerial(): string | null {
|
|
|
|
|
|
return this.currentSerialRef;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public isConnected(): boolean {
|
|
|
|
|
|
return this.status === 'connected' && this.ws?.readyState === WebSocket.OPEN;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 销毁单例(测试用)====================
|
|
|
|
|
|
|
|
|
|
|
|
public static destroyInstance(): void {
|
|
|
|
|
|
if (WebSocketManager.instance) {
|
|
|
|
|
|
WebSocketManager.instance.close();
|
|
|
|
|
|
WebSocketManager.instance = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-05-12 08:48:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 🎯 导出单例实例
|
|
|
|
|
|
export const wsManager = WebSocketManager.getInstance();
|
|
|
|
|
|
|
|
|
|
|
|
// 导出类型
|
|
|
|
|
|
export type { WSStatus, MessageHandler, WSMessage };
|