Skip to content

Commit

Permalink
fix: error on wss listening but server not listening
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Oct 4, 2024
1 parent 6089d77 commit 91e7edf
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,27 @@ export function createWebSocketServer(
})
res.end(body)
}) as Parameters<typeof createHttpServer>[1]
// vite dev server in middleware mode
// need to call ws listen manually
if (httpsOptions) {
wsHttpServer = createHttpsServer(httpsOptions, route)
} else {
wsHttpServer = createHttpServer(route)
}
// vite dev server in middleware mode
// need to call ws listen manually
wss = new WebSocketServerRaw({ server: wsHttpServer })
wss = new WebSocketServerRaw({ noServer: true })
wsHttpServer.on('upgrade', (req, socket, head) => {
const protocol = req.headers['sec-websocket-protocol']!
if (protocol === 'vite-ping' && server && !server.listening) {
// reject connection to tell the vite/client that the server is not ready
// if the http server is not listening
// because the ws server listens before the http server listens
req.destroy()
return
}
wss.handleUpgrade(req, socket as Socket, head, (ws) => {
wss.emit('connection', ws, req)
})
})
}

wss.on('connection', (socket) => {
Expand Down

0 comments on commit 91e7edf

Please sign in to comment.