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
This commit is contained in:
Michel Roegl-Brunner
2025-11-26 09:00:25 +01:00
parent b01c029b18
commit 3d45e6d355

View File

@@ -79,16 +79,15 @@ class ScriptExecutionHandler {
* @param {import('http').Server} server * @param {import('http').Server} server
*/ */
constructor(server) { constructor(server) {
// Create WebSocketServer without attaching to server // Create WebSocketServer attached to server with path filter
// We'll handle upgrades manually to avoid interfering with Next.js HMR // The path option should ensure it only handles /ws/script-execution
this.wss = new WebSocketServer({ this.wss = new WebSocketServer({
noServer: true, server,
path: '/ws/script-execution' path: '/ws/script-execution'
}); });
this.activeExecutions = new Map(); this.activeExecutions = new Map();
this.db = getDatabase(); this.db = getDatabase();
this.setupWebSocket(); this.setupWebSocket();
this.server = server;
} }
/** /**
@@ -1188,23 +1187,6 @@ app.prepare().then(() => {
// Create WebSocket handlers // Create WebSocket handlers
const scriptHandler = new ScriptExecutionHandler(httpServer); 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 // Note: TerminalHandler removed as it's not being used by the current application
httpServer httpServer