11import type { Server } from 'node:http'
2- import { STATUS_CODES } from 'node:http'
2+ import { STATUS_CODES , createServer as createHttpServer } from 'node:http'
33import type { ServerOptions as HttpsServerOptions } from 'node:https'
44import { createServer as createHttpsServer } from 'node:https'
55import type { Socket } from 'node:net'
66import colors from 'picocolors'
7- import type { ServerOptions , WebSocket as WebSocketRaw } from 'ws'
7+ import type { WebSocket as WebSocketRaw } from 'ws'
88import { WebSocketServer as WebSocketServerRaw } from 'ws'
99import type { WebSocket as WebSocketTypes } from 'dep-types/ws'
1010import type { CustomPayload , ErrorPayload , HMRPayload } from 'types/hmrPayload'
@@ -20,6 +20,10 @@ export type WebSocketCustomListener<T> = (
2020) => void
2121
2222export interface WebSocketServer {
23+ /**
24+ * Listen on port and host
25+ */
26+ listen ( ) : void
2327 /**
2428 * Get all connected clients.
2529 */
@@ -83,7 +87,7 @@ export function createWebSocketServer(
8387 httpsOptions ?: HttpsServerOptions ,
8488) : WebSocketServer {
8589 let wss : WebSocketServerRaw
86- let httpsServer : Server | undefined = undefined
90+ let wsHttpServer : Server | undefined = undefined
8791
8892 const hmr = isObject ( config . server . hmr ) && config . server . hmr
8993 const hmrServer = hmr && hmr . server
@@ -93,6 +97,8 @@ export function createWebSocketServer(
9397 const wsServer = hmrServer || ( portsAreCompatible && server )
9498 const customListeners = new Map < string , Set < WebSocketCustomListener < any > > > ( )
9599 const clientsMap = new WeakMap < WebSocketRaw , WebSocketClient > ( )
100+ const port = hmrPort || 24678
101+ const host = ( hmr && hmr . host ) || undefined
96102
97103 if ( wsServer ) {
98104 wss = new WebSocketServerRaw ( { noServer : true } )
@@ -104,39 +110,28 @@ export function createWebSocketServer(
104110 }
105111 } )
106112 } else {
107- const websocketServerOptions : ServerOptions = { }
108- const port = hmrPort || 24678
109- const host = ( hmr && hmr . host ) || undefined
110- if ( httpsOptions ) {
111- // if we're serving the middlewares over https, the ws library doesn't support automatically creating an https server, so we need to do it ourselves
112- // create an inline https server and mount the websocket server to it
113- httpsServer = createHttpsServer ( httpsOptions , ( req , res ) => {
114- const statusCode = 426
115- const body = STATUS_CODES [ statusCode ]
116- if ( ! body )
117- throw new Error (
118- `No body text found for the ${ statusCode } status code` ,
119- )
113+ // http server request handler keeps the same with
114+ // https://github.com/websockets/ws/blob/45e17acea791d865df6b255a55182e9c42e5877a/lib/websocket-server.js#L88-L96
115+ const route = ( ( _ , res ) => {
116+ const statusCode = 426
117+ const body = STATUS_CODES [ statusCode ]
118+ if ( ! body )
119+ throw new Error ( `No body text found for the ${ statusCode } status code` )
120120
121- res . writeHead ( statusCode , {
122- 'Content-Length' : body . length ,
123- 'Content-Type' : 'text/plain' ,
124- } )
125- res . end ( body )
121+ res . writeHead ( statusCode , {
122+ 'Content-Length' : body . length ,
123+ 'Content-Type' : 'text/plain' ,
126124 } )
127-
128- httpsServer . listen ( port , host )
129- websocketServerOptions . server = httpsServer
125+ res . end ( body )
126+ } ) as Parameters < typeof createHttpServer > [ 1 ]
127+ if ( httpsOptions ) {
128+ wsHttpServer = createHttpsServer ( httpsOptions , route )
130129 } else {
131- // we don't need to serve over https, just let ws handle its own server
132- websocketServerOptions . port = port
133- if ( host ) {
134- websocketServerOptions . host = host
135- }
130+ wsHttpServer = createHttpServer ( route )
136131 }
137-
138132 // vite dev server in middleware mode
139- wss = new WebSocketServerRaw ( websocketServerOptions )
133+ // need to call ws listen manually
134+ wss = new WebSocketServerRaw ( { server : wsHttpServer } )
140135 }
141136
142137 wss . on ( 'connection' , ( socket ) => {
@@ -210,6 +205,9 @@ export function createWebSocketServer(
210205 let bufferedError : ErrorPayload | null = null
211206
212207 return {
208+ listen : ( ) => {
209+ wsHttpServer ?. listen ( port , host )
210+ } ,
213211 on : ( ( event : string , fn : ( ) => void ) => {
214212 if ( wsServerEvents . includes ( event ) ) wss . on ( event , fn )
215213 else {
@@ -266,8 +264,8 @@ export function createWebSocketServer(
266264 if ( err ) {
267265 reject ( err )
268266 } else {
269- if ( httpsServer ) {
270- httpsServer . close ( ( err ) => {
267+ if ( wsHttpServer ) {
268+ wsHttpServer . close ( ( err ) => {
271269 if ( err ) {
272270 reject ( err )
273271 } else {
0 commit comments