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

Provide http server port directly from _server #1294

Merged
merged 10 commits into from
Feb 5, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ Emitted when the underlying server has been bound.
A set that stores all connected clients. Please note that this property is only
added when the `clientTracking` is truthy.

### server.address()

Returns an object with `port`, `family`, and `address` properties specifying
the bound address, the address family name, and port of the server as reported
by the operating system if listening on an IP socket.
If the server is listening on a pipe or UNIX domain socket, the name is
returned as a string.

### server.close([callback])

Close the server and terminate all clients, calls callback when done.
Expand Down
18 changes: 18 additions & 0 deletions lib/websocket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ class WebSocketServer extends EventEmitter {
this.options = options;
}

/**
* Returns the bound address, the address family name, and port of the server
* as reported by the operating system if listening on an IP socket.
* If the server is listening on a pipe or UNIX domain socket, the name is
* returned as a string.
*
* @return {(Object|String|null)} The address of the server
* @public
*/
address () {
if (this.options.noServer) {
throw new Error('The server is operating in "noServer" mode');
}

if (!this._server) return null;
return this._server.address();
}

/**
* Close the server.
*
Expand Down
28 changes: 28 additions & 0 deletions test/websocket-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ describe('WebSocketServer', function () {
});
});

describe('#address', function () {
it('returns the address of the server', function (done) {
const wss = new WebSocket.Server({ port: 0 }, () => {
const addr = wss.address();

assert.deepStrictEqual(addr, wss._server.address());
wss.close(done);
});
});

it('throws an error when operating in "noServer" mode', function () {
const wss = new WebSocket.Server({ noServer: true });

assert.throws(() => {
wss.address();
}, /^Error: The server is operating in "noServer" mode$/);
});

it('returns `null` if called after close', function (done) {
const wss = new WebSocket.Server({ port: 0 }, () => {
wss.close(() => {
assert.strictEqual(wss.address(), null);
done();
});
});
});
});

describe('#close', function () {
it('does not thrown when called twice', function (done) {
const wss = new WebSocket.Server({ port: 0 }, () => {
Expand Down