From 3d45e6d355eb151539ce7daf60849206637aa371 Mon Sep 17 00:00:00 2001 From: Michel Roegl-Brunner Date: Wed, 26 Nov 2025 09:00:25 +0100 Subject: [PATCH] Revert WebSocket handling to simpler approach - Go back to attaching WebSocketServer directly with path option - Remove manual upgrade event handling that was causing errors - The path option should filter to only /ws/script-execution - Next.js should handle its own HMR WebSocket upgrades naturally --- server.js | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/server.js b/server.js index 2b61bb0..37b546b 100644 --- a/server.js +++ b/server.js @@ -79,16 +79,15 @@ class ScriptExecutionHandler { * @param {import('http').Server} server */ constructor(server) { - // Create WebSocketServer without attaching to server - // We'll handle upgrades manually to avoid interfering with Next.js HMR + // Create WebSocketServer attached to server with path filter + // The path option should ensure it only handles /ws/script-execution this.wss = new WebSocketServer({ - noServer: true, + server, path: '/ws/script-execution' }); this.activeExecutions = new Map(); this.db = getDatabase(); this.setupWebSocket(); - this.server = server; } /** @@ -1188,23 +1187,6 @@ app.prepare().then(() => { // Create WebSocket handlers const scriptHandler = new ScriptExecutionHandler(httpServer); - - // Manually handle upgrade events to ensure Next.js HMR WebSocket works - // We handle /ws/script-execution ourselves and pass everything else to Next.js - httpServer.on('upgrade', (request, socket, head) => { - const parsedUrl = parse(request.url || '', true); - const { pathname } = parsedUrl; - - if (pathname === '/ws/script-execution') { - // Handle our custom WebSocket endpoint - scriptHandler.wss.handleUpgrade(request, socket, head, (ws) => { - scriptHandler.wss.emit('connection', ws, request); - }); - } else { - // Pass all other WebSocket upgrades (including Next.js HMR) to Next.js handler - handle(request, socket, head); - } - }); // Note: TerminalHandler removed as it's not being used by the current application httpServer