From 1535c8795b9d987ea1cf57532860c6a28d061b29 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Sat, 15 Nov 2025 23:27:42 +0100 Subject: [PATCH] Refactor WebSocket testing script to utilize HTTP polling and WebSocket upgrade checks This commit updates the `testWebSocket.js` script to replace the Socket.IO client connection with direct HTTP polling for the initial handshake. It introduces structured tests for both HTTP polling and WebSocket upgrades, providing detailed logging for success and error scenarios. The changes enhance the script's ability to verify server connectivity and support for WebSocket protocols, improving the overall testing process for real-time communication. --- backend/scripts/testWebSocket.js | 135 ++++++++++++++++++++++--------- 1 file changed, 98 insertions(+), 37 deletions(-) diff --git a/backend/scripts/testWebSocket.js b/backend/scripts/testWebSocket.js index b00a470..2481551 100644 --- a/backend/scripts/testWebSocket.js +++ b/backend/scripts/testWebSocket.js @@ -7,57 +7,118 @@ * node scripts/testWebSocket.js localhost 3050 */ -import { io } from 'socket.io-client'; +import http from 'http'; const host = process.argv[2] || 'localhost'; const port = process.argv[3] || '3050'; const url = `http://${host}:${port}`; -console.log(`🔌 Teste WebSocket-Verbindung zu ${url}...`); +console.log(`🔌 Teste Socket.IO-Server auf ${url}...\n`); -const socket = io(url, { - path: '/socket.io/', - transports: ['websocket', 'polling'], - reconnection: false, - timeout: 5000, +// Test 1: HTTP-Polling (Socket.IO Handshake) +console.log('1️⃣ Teste HTTP-Polling (Socket.IO Handshake)...'); +const pollingUrl = `${url}/socket.io/?EIO=4&transport=polling`; + +const pollingReq = http.get(pollingUrl, (res) => { + let data = ''; + + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + if (res.statusCode === 200) { + console.log(' ✅ HTTP-Polling erfolgreich!'); + console.log(` Status: ${res.statusCode}`); + console.log(` Response: ${data.substring(0, 200)}...`); + + // Versuche Session-ID zu extrahieren + try { + const jsonMatch = data.match(/\{.*\}/); + if (jsonMatch) { + const json = JSON.parse(jsonMatch[0]); + if (json.sid) { + console.log(` Session ID: ${json.sid}`); + } + } + } catch (e) { + // Ignoriere JSON-Parse-Fehler + } + + // Test 2: WebSocket-Upgrade + console.log('\n2️⃣ Teste WebSocket-Upgrade...'); + testWebSocketUpgrade(host, port); + } else { + console.error(` ❌ HTTP-Polling fehlgeschlagen: Status ${res.statusCode}`); + process.exit(1); + } + }); }); -socket.on('connect', () => { - console.log('✅ WebSocket-Verbindung erfolgreich!'); - console.log(` Socket ID: ${socket.id}`); - console.log(` Transport: ${socket.io.engine.transport.name}`); - - // Test: Trete einem Club-Raum bei - socket.emit('join-club', 1); - console.log(' Test: join-club Event gesendet'); - - // Nach 2 Sekunden trennen - setTimeout(() => { - socket.disconnect(); - console.log(' Verbindung getrennt.'); - process.exit(0); - }, 2000); -}); - -socket.on('connect_error', (error) => { - console.error('❌ WebSocket-Verbindungsfehler:'); +pollingReq.on('error', (error) => { + console.error(' ❌ HTTP-Polling Fehler:'); console.error(` ${error.message}`); - if (error.message.includes('ECONNREFUSED')) { + if (error.code === 'ECONNREFUSED') { console.error(' → Server läuft möglicherweise nicht oder ist nicht erreichbar'); + console.error(` → Prüfe: netstat -tlnp | grep ${port}`); } process.exit(1); }); -socket.on('disconnect', (reason) => { - console.log(` Getrennt: ${reason}`); +pollingReq.setTimeout(5000, () => { + pollingReq.destroy(); + console.error(' ❌ Timeout: Keine Antwort innerhalb von 5 Sekunden'); + process.exit(1); }); -// Timeout nach 10 Sekunden -setTimeout(() => { - if (socket.connected) { - socket.disconnect(); - } - console.error('❌ Timeout: Keine Verbindung innerhalb von 10 Sekunden'); - process.exit(1); -}, 10000); +function testWebSocketUpgrade(host, port) { + // WebSocket-Upgrade-Request + const wsKey = Buffer.from(Math.random().toString()).toString('base64').substring(0, 16); + + const options = { + hostname: host, + port: port, + path: '/socket.io/?EIO=4&transport=websocket', + method: 'GET', + headers: { + 'Upgrade': 'websocket', + 'Connection': 'Upgrade', + 'Sec-WebSocket-Key': wsKey, + 'Sec-WebSocket-Version': '13', + 'Sec-WebSocket-Protocol': 'chat, superchat' + } + }; + + const wsReq = http.request(options, (res) => { + if (res.statusCode === 101) { + console.log(' ✅ WebSocket-Upgrade erfolgreich!'); + console.log(` Status: ${res.statusCode} (Switching Protocols)`); + console.log(` Upgrade Header: ${res.headers.upgrade}`); + console.log(` Connection Header: ${res.headers.connection}`); + console.log('\n✅ Socket.IO-Server ist erreichbar und unterstützt WebSockets!'); + process.exit(0); + } 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); + } + }); + + wsReq.on('error', (error) => { + console.error(' ❌ WebSocket-Upgrade Fehler:'); + console.error(` ${error.message}`); + console.log('\n⚠️ HTTP-Polling funktioniert, aber WebSocket-Upgrade schlägt fehl.'); + console.log(' → Das könnte ein Problem mit der Apache-Konfiguration sein.'); + process.exit(0); + }); + + wsReq.setTimeout(5000, () => { + wsReq.destroy(); + console.error(' ❌ Timeout: Keine Antwort innerhalb von 5 Sekunden'); + process.exit(1); + }); + + wsReq.end(); +}