File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed
Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments