* 启动 WebSocket 服务 * @param portOverride 可选端口 * @param forceRestart 是否强制重启
(
portOverride?: number,
forceRestart: boolean = false,
)
| 44 | * @param forceRestart 是否强制重启 |
| 45 | */ |
| 46 | public async start( |
| 47 | portOverride?: number, |
| 48 | forceRestart: boolean = false, |
| 49 | ): Promise<{ port: number }> { |
| 50 | const store = useStore(); |
| 51 | const websocketConfig = store.get("websocket"); |
| 52 | const portFromStore = websocketConfig?.port; |
| 53 | const port = portOverride ?? portFromStore ?? 25885; |
| 54 | |
| 55 | // 如果服务已在运行 |
| 56 | if (this.wss && this.currentPort !== null) { |
| 57 | // 如果端口相同,直接返回 |
| 58 | if (this.currentPort === port) { |
| 59 | return { port: this.currentPort }; |
| 60 | } |
| 61 | // 如果端口不同且需要强制重启,先停止再启动 |
| 62 | if (forceRestart) { |
| 63 | await this.stop(); |
| 64 | } else { |
| 65 | // 否则返回当前端口 |
| 66 | return { port: this.currentPort }; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | socketLog.info(`🔌 Trying to start WebSocket server on port ${port}`); |
| 71 | |
| 72 | // 先验证端口是否可用 |
| 73 | const isAvailable = await this.testPort(port); |
| 74 | if (!isAvailable) throw new Error(`端口 ${port} 不可用`); |
| 75 | |
| 76 | return new Promise<{ port: number }>((resolve, reject) => { |
| 77 | try { |
| 78 | const wss = new WebSocketServer({ port }); |
| 79 | this.wss = wss; |
| 80 | this.currentPort = port; |
| 81 | |
| 82 | wss.on("connection", (socket: WebSocket) => { |
| 83 | this.handleClientConnection(socket); |
| 84 | }); |
| 85 | |
| 86 | wss.once("listening", () => { |
| 87 | socketLog.info(`✅ WebSocket server started on port ${port}`); |
| 88 | resolve({ port }); |
| 89 | }); |
| 90 | |
| 91 | wss.once("error", (error: Error) => { |
| 92 | socketLog.error("❌ WebSocket server failed to start:", error); |
| 93 | this.cleanupServer(); |
| 94 | reject(error); |
| 95 | }); |
| 96 | } catch (error) { |
| 97 | socketLog.error("❌ WebSocket server creation error:", error); |
| 98 | this.cleanupServer(); |
| 99 | reject(error instanceof Error ? error : new Error(String(error))); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 |
no test coverage detected