From bbdc923950f1911b384bfc79fb26fafdb3feea75 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Sun, 16 Nov 2025 00:00:42 +0100 Subject: [PATCH] Enhance WebSocket testing script to include session ID handling This commit updates the `testWebSocket.js` script to extract and utilize the session ID from the HTTP polling response. The WebSocket upgrade request is modified to include the session ID when available, improving the accuracy of the upgrade tests. Additionally, detailed logging for response bodies is added to aid in diagnosing potential issues during the WebSocket upgrade process. --- backend/scripts/testWebSocket.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/backend/scripts/testWebSocket.js b/backend/scripts/testWebSocket.js index 2481551..13d2caf 100644 --- a/backend/scripts/testWebSocket.js +++ b/backend/scripts/testWebSocket.js @@ -33,12 +33,14 @@ const pollingReq = http.get(pollingUrl, (res) => { console.log(` Response: ${data.substring(0, 200)}...`); // Versuche Session-ID zu extrahieren + let sessionId = null; try { const jsonMatch = data.match(/\{.*\}/); if (jsonMatch) { const json = JSON.parse(jsonMatch[0]); if (json.sid) { - console.log(` Session ID: ${json.sid}`); + sessionId = json.sid; + console.log(` Session ID: ${sessionId}`); } } } catch (e) { @@ -47,7 +49,7 @@ const pollingReq = http.get(pollingUrl, (res) => { // Test 2: WebSocket-Upgrade console.log('\n2️⃣ Teste WebSocket-Upgrade...'); - testWebSocketUpgrade(host, port); + testWebSocketUpgrade(host, port, sessionId); } else { console.error(` ❌ HTTP-Polling fehlgeschlagen: Status ${res.statusCode}`); process.exit(1); @@ -71,14 +73,17 @@ pollingReq.setTimeout(5000, () => { process.exit(1); }); -function testWebSocketUpgrade(host, port) { +function testWebSocketUpgrade(host, port, sessionId) { // WebSocket-Upgrade-Request const wsKey = Buffer.from(Math.random().toString()).toString('base64').substring(0, 16); + const path = sessionId + ? `/socket.io/?EIO=4&transport=websocket&sid=${sessionId}` + : '/socket.io/?EIO=4&transport=websocket'; const options = { hostname: host, port: port, - path: '/socket.io/?EIO=4&transport=websocket', + path: path, method: 'GET', headers: { 'Upgrade': 'websocket', @@ -100,8 +105,22 @@ function testWebSocketUpgrade(host, port) { } else { console.log(` ⚠️ WebSocket-Upgrade: Status ${res.statusCode} (erwartet: 101)`); console.log(` → Server antwortet, aber Upgrade nicht erfolgreich`); - console.log(` → Möglicherweise wird nur HTTP-Polling unterstützt`); - process.exit(0); + + // Lese Response-Body für weitere Informationen + let body = ''; + res.on('data', (chunk) => { + body += chunk; + }); + res.on('end', () => { + if (body) { + console.log(` Response Body: ${body.substring(0, 200)}`); + } + if (res.statusCode === 400) { + console.log(` → Socket.IO lehnt den Request ab (Bad Request)`); + console.log(` → Möglicherweise fehlt die Session-ID oder sie ist ungültig`); + } + process.exit(0); + }); } });