Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom websocket server #2338

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Update } from 'types/hmrPayload'
import { CLIENT_DIR } from '../constants'
import { RollupError } from 'rollup'
import match from 'minimatch'
import { Server } from 'http'

export const debugHmr = createDebugger('vite:hmr')

Expand All @@ -20,6 +21,7 @@ export interface HmrOptions {
path?: string
timeout?: number
overlay?: boolean
server?: Server
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be http.Server instead (which is then passed to createWebScoketServer as the first argument)? It feels weird to require the user to create a ws server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nvm..

}

export interface HmrContext {
Expand Down
11 changes: 6 additions & 5 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ export function createWebSocketServer(
): WebSocketServer {
let wss: WebSocket.Server

if (server) {
const hmr = typeof config.server.hmr === 'object' && config.server.hmr
const wsServer = (hmr && hmr.server) || server

if (wsServer) {
wss = new WebSocket.Server({ noServer: true })
server.on('upgrade', (req, socket, head) => {
wsServer.on('upgrade', (req, socket, head) => {
if (req.headers['sec-websocket-protocol'] === HMR_HEADER) {
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req)
Expand All @@ -29,9 +32,7 @@ export function createWebSocketServer(
} else {
// vite dev server in middleware mode
wss = new WebSocket.Server({
port:
(typeof config.server.hmr === 'object' && config.server.hmr.port) ||
24678
port: (hmr && hmr.port) || 24678
})
}

Expand Down