Skip to content
This repository was archived by the owner on Jul 13, 2021. It is now read-only.
Open
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
6 changes: 3 additions & 3 deletions lib/client/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const maxRetries = 10;
let retry = maxRetries;

module.exports = function connect(options, handler) {
const { host } = options.webSocket;
const { host, port, secure } = options.webSocketClient;
const socketUrl = url.format({
protocol: options.https ? 'wss' : 'ws',
protocol: secure ? 'wss' : 'ws',
hostname: host === '*' ? window.location.hostname : host,
port: options.webSocket.port,
port: port,
slashes: true,
});

Expand Down
33 changes: 19 additions & 14 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const defaults = {
warnings: true,
},
server: null,
client: {},
stats: {
context: process.cwd(),
},
Expand Down Expand Up @@ -59,24 +60,28 @@ module.exports = (opts = {}) => {
);
}

const { server } = options;
const { server, client } = options;
const hasServer = server && server.listening;
options.webSocketServer = {};

if (server && !server.listening) {
throw new HotClientError(
'`options.server` must be a running/listening net.Server instance'
);
} else if (server) {
options.webSocket = {
host: server.address().address,
port: server.address().port,
};
if (hasServer && server instanceof HttpsServer && typeof options.https === 'undefined') {
options.https = true;
}

if (server instanceof HttpsServer && typeof options.https === 'undefined') {
options.https = true;
}
if (hasServer) {
options.webSocketServer = { server };
} else {
options.webSocket = { host: options.host.client, port: options.port };
options.webSocketServer = {
host: server && server.host || options.host.server,
port: server && server.port || options.port,
}
}

options.webSocketClient = {
host: client.host || options.host.client || (hasServer && server.address().address) || '*',
port: client.port || (hasServer && server.address().port) || options.port,
secure: typeof client.secure !== 'undefined' ? client.secure : options.https,
};

return options;
};
5 changes: 2 additions & 3 deletions lib/socket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const strip = require('strip-ansi');
const WebSocket = require('ws');

function getServer(options) {
const { host, log, port, server } = options;
const wssOptions = server ? { server } : { host: host.server, port };
const wss = new WebSocket.Server(wssOptions);
const { log, webSocketServer } = options;
const wss = new WebSocket.Server(webSocketServer);

onConnection(wss, options);
onError(wss, options);
Expand Down