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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,21 @@ If true, instructs the internal logger to prepend log output with a timestamp.

##### port

Type: `Number`
Type: `Number|Object`
Default: `0`

The port the `WebSocket` server should listen on. By default, the socket server
will allocate a port. If a different port is chosen, the consumer of the module
must ensure that the port is free before hand.

If using the module in a specialized environment, you may choose to specify an
`object` to define `client` and `server` port separately. The `object` value
should match `{ client: <Number|false>, server: <Number> }`. Be aware that the `client`
port will be used _in the browser_ by `WebSockets`. You should not use this
option in this way unless _you know what you're doing._ Using a mismatched
`client` and `server` port will be **unsupported by the project** as the behavior
in the browser can be unpredictable and is specific to a particular environment.

##### reload

Type: `Boolean`
Expand Down
20 changes: 19 additions & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ module.exports = (opts = {}) => {
);
}

if (typeof options.port === 'number') {
options.port = {
server: options.port,
client: options.port,
};
} else if (!options.port.server) {
throw new HotClientError(
'`port.server` must be defined when setting port to an Object'
);
} else if (!options.port.client) {
throw new HotClientError(
'`port.client` must be defined when setting port to an Object'
);
}

const { server } = options;

if (server && !server.listening) {
Expand All @@ -68,7 +83,10 @@ module.exports = (opts = {}) => {
port: server.address().port,
};
} else {
options.webSocket = { host: options.host.client, port: options.port };
options.webSocket = {
host: options.host.client,
port: options.port.client !== false ? options.port.client : null,
};
}

return options;
Expand Down
4 changes: 3 additions & 1 deletion lib/socket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const WebSocket = require('ws');

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

onConnection(wss, options);
Expand Down
Loading