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:
Daniel 2026-03-07 11:02:36 +01:00
parent e748fc97e9
commit e4895a792c
6 changed files with 60 additions and 32 deletions

View file

@ -367,7 +367,7 @@ export default function WatchTogetherTab({ data }: { data: any }) {
// ── WebSocket connect ──
const connectWs = useCallback(() => {
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) return;
if (wsRef.current && (wsRef.current.readyState === WebSocket.OPEN || wsRef.current.readyState === WebSocket.CONNECTING)) return;
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
const ws = new WebSocket(`${proto}://${location.host}/ws/watch-together`);
@ -382,7 +382,9 @@ export default function WatchTogetherTab({ data }: { data: any }) {
};
ws.onclose = () => {
wsRef.current = null;
if (wsRef.current === ws) {
wsRef.current = null;
}
if (currentRoomRef.current) {
reconnectTimerRef.current = setTimeout(() => {
reconnectDelayRef.current = Math.min(reconnectDelayRef.current * 2, 10000);
@ -401,6 +403,7 @@ export default function WatchTogetherTab({ data }: { data: any }) {
setError(null);
connectWs();
const startTime = Date.now();
const waitForWs = () => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
wsSend({
@ -409,6 +412,8 @@ export default function WatchTogetherTab({ data }: { data: any }) {
userName: userName.trim(),
password: roomPassword.trim() || undefined,
});
} else if (Date.now() - startTime > 10000) {
setError('Verbindung zum Server fehlgeschlagen.');
} else {
setTimeout(waitForWs, 100);
}
@ -422,6 +427,7 @@ export default function WatchTogetherTab({ data }: { data: any }) {
setError(null);
connectWs();
const startTime = Date.now();
const waitForWs = () => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
wsSend({
@ -430,6 +436,8 @@ export default function WatchTogetherTab({ data }: { data: any }) {
roomId,
password: password?.trim() || undefined,
});
} else if (Date.now() - startTime > 10000) {
setError('Verbindung zum Server fehlgeschlagen.');
} else {
setTimeout(waitForWs, 100);
}