Skip to content

Commit

Permalink
feat(dev): add config for websocket connection
Browse files Browse the repository at this point in the history
close vitejs#674, close vitejs#676, close vitejs#652
  • Loading branch information
underfin committed Aug 4, 2020
1 parent 1e375a4 commit 544446c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// This file runs in the browser.

// injected by serverPluginHmr when served
declare const __HOSTNAME__: number
declare const __PORT__: number
declare const __PATH__: number

declare const __MODE__: string
declare const __DEFINES__: Record<string, any>
;(window as any).process = {
Expand Down Expand Up @@ -32,7 +35,12 @@ console.log('[vite] connecting...')
declare var __VUE_HMR_RUNTIME__: HMRRuntime

const socketProtocol = location.protocol === 'https:' ? 'wss' : 'ws'
const socketUrl = `${socketProtocol}://${location.hostname}:${__PORT__}`
const hostname = __HOSTNAME__ || location.hostname
const path = __PATH__
let socketUrl = `${socketProtocol}://${hostname}:${__PORT__}`
if (path) {
socketUrl = socketUrl + '/' + path
}
const socket = new WebSocket(socketUrl, 'vite-hmr')

function warnFailedFetch(err: Error, path: string | string[]) {
Expand Down
7 changes: 7 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ export interface SharedConfig {
}

export interface ServerConfig extends SharedConfig {
/**
* Configure websocket connection.
*/
socketHost?: string
socketPort?: number
socketPath?: string

hostname?: string
port?: number
open?: boolean
Expand Down
5 changes: 4 additions & 1 deletion src/node/server/serverPluginClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const clientPlugin: ServerPlugin = ({ app, config }) => {
if (ctx.path === clientPublicPath) {
ctx.type = 'js'
ctx.status = 200
ctx.body = clientCode.replace(`__PORT__`, ctx.port.toString())
ctx.body = clientCode
.replace(`__PORT__`, (config.socketPort || ctx.port).toString())
.replace(`__HOSTNAME__`, config.socketHost || 'false')
.replace(`__PATH__`, config.socketPath || 'false')
} else {
if (ctx.path === legacyPublicPath) {
console.error(
Expand Down

2 comments on commit 544446c

@sionzee
Copy link

@sionzee sionzee commented on 544446c Aug 6, 2020

Choose a reason for hiding this comment

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

Good idea!

@underfin
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sorry for overwrite your suggestion==.

Please sign in to comment.