Skip to content

Commit 3f400f4

Browse files
Patrick HagemeisterPatrick Hagemeister
authored andcommitted
Fix devserver mode in combination with websocket/http merge
1 parent 3e0b509 commit 3f400f4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

demo/bin/demo.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,34 @@ if (DEV_MODE) {
525525
strictPort: true,
526526
},
527527
});
528+
528529
await vite.listen();
530+
531+
// Attach WebSocket handler AFTER Vite has fully initialized
532+
// Use prependListener (not prependOnceListener) so it runs for every request
533+
// This ensures our handler runs BEFORE Vite's handlers
534+
if (vite.httpServer) {
535+
vite.httpServer.prependListener('upgrade', (req, socket, head) => {
536+
const pathname = req.url?.split('?')[0] || req.url || '';
537+
538+
// ONLY handle /ws - everything else passes through unchanged to Vite
539+
if (pathname === '/ws') {
540+
if (!socket.destroyed && !socket.readableEnded) {
541+
wss.handleUpgrade(req, socket, head, (ws) => {
542+
wss.emit('connection', ws, req);
543+
});
544+
}
545+
// Stop here - we handled it, socket is consumed
546+
// Don't call other listeners
547+
return;
548+
}
549+
550+
// For non-/ws paths, explicitly do nothing and let the event propagate
551+
// The key is: don't return, don't touch the socket, just let it pass through
552+
// Vite's handlers (which were added before ours via prependListener) will process it
553+
});
554+
}
555+
529556
printBanner(`http://localhost:${HTTP_PORT}/demo/`);
530557
} else {
531558
// Production mode: static file server

0 commit comments

Comments
 (0)