Fix: WebSocket-Konflikt zwischen Streaming und Watch Together
Root Cause: ws-Library killt WS-Verbindungen mit HTTP 400 wenn
mehrere WebSocketServer mit { server, path } registriert werden.
Der erste WSS (streaming) hat abortHandshake() fuer watch-together
Verbindungen aufgerufen.
- Beide WSS auf noServer-Modus umgestellt
- Zentrales upgrade-Routing in index.ts nach Pathname
- Frontend: connectWs prueft jetzt auch CONNECTING-State
- Frontend: onclose clobbert wsRef nur noch wenn gleicher Socket
- Frontend: waitForWs hat jetzt 10s Timeout statt Endlosschleife
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
e748fc97e9
commit
e4895a792c
6 changed files with 60 additions and 32 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import express from 'express';
|
||||
import path from 'node:path';
|
||||
import http from 'node:http';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { Client } from 'discord.js';
|
||||
import { createClient } from './core/discord.js';
|
||||
import { addSSEClient, removeSSEClient, sseBroadcast, getSSEClientCount } from './core/sse.js';
|
||||
|
|
@ -168,8 +169,29 @@ async function boot(): Promise<void> {
|
|||
|
||||
// Start Express (http.createServer so WebSocket can attach)
|
||||
const httpServer = http.createServer(app);
|
||||
attachWebSocket(httpServer);
|
||||
attachWatchTogetherWs(httpServer);
|
||||
|
||||
// Create WebSocket servers (noServer mode to avoid path conflicts)
|
||||
const wssStreaming = new WebSocketServer({ noServer: true });
|
||||
const wssWatchTogether = new WebSocketServer({ noServer: true });
|
||||
|
||||
// Route WebSocket upgrade requests to the correct server
|
||||
httpServer.on('upgrade', (request, socket, head) => {
|
||||
const { pathname } = new URL(request.url!, `http://${request.headers.host}`);
|
||||
if (pathname === '/ws/streaming') {
|
||||
wssStreaming.handleUpgrade(request, socket, head, (ws) => {
|
||||
wssStreaming.emit('connection', ws, request);
|
||||
});
|
||||
} else if (pathname === '/ws/watch-together') {
|
||||
wssWatchTogether.handleUpgrade(request, socket, head, (ws) => {
|
||||
wssWatchTogether.emit('connection', ws, request);
|
||||
});
|
||||
} else {
|
||||
socket.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
attachWebSocket(wssStreaming);
|
||||
attachWatchTogetherWs(wssWatchTogether);
|
||||
httpServer.listen(PORT, () => console.log(`[HTTP] Listening on :${PORT}`));
|
||||
|
||||
// Login Discord bots
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import type express from 'express';
|
||||
import http from 'node:http';
|
||||
import { WebSocketServer, WebSocket } from 'ws';
|
||||
import crypto from 'node:crypto';
|
||||
import type { Plugin, PluginContext } from '../../core/plugin.js';
|
||||
|
|
@ -265,9 +264,9 @@ const streamingPlugin: Plugin = {
|
|||
},
|
||||
};
|
||||
|
||||
/** Call after httpServer is created to attach WebSocket signaling */
|
||||
export function attachWebSocket(server: http.Server): void {
|
||||
wss = new WebSocketServer({ server, path: '/ws/streaming' });
|
||||
/** Attach WebSocket signaling to a pre-created WebSocketServer (noServer mode) */
|
||||
export function attachWebSocket(existingWss: WebSocketServer): void {
|
||||
wss = existingWss;
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
const clientId = crypto.randomUUID();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import type express from 'express';
|
||||
import http from 'node:http';
|
||||
import { WebSocketServer, WebSocket } from 'ws';
|
||||
import crypto from 'node:crypto';
|
||||
import type { Plugin, PluginContext } from '../../core/plugin.js';
|
||||
|
|
@ -515,9 +514,9 @@ const watchTogetherPlugin: Plugin = {
|
|||
},
|
||||
};
|
||||
|
||||
/** Call after httpServer is created to attach WebSocket */
|
||||
export function attachWatchTogetherWs(server: http.Server): void {
|
||||
wss = new WebSocketServer({ server, path: '/ws/watch-together' });
|
||||
/** Attach WebSocket to a pre-created WebSocketServer (noServer mode) */
|
||||
export function attachWatchTogetherWs(existingWss: WebSocketServer): void {
|
||||
wss = existingWss;
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
const clientId = crypto.randomUUID();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue