Skip to content
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 doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ This class represents a WebSocket server. It extends the `EventEmitter`.
- `verifyClient` {Function} A function which can be used to validate incoming
connections. See description below. (Usage is discouraged: see
[Issue #337](https://github.com/websockets/ws/issues/377#issuecomment-462152231))
- `WebSocket` {Function} Specifies the `WebSocket` class to be used. It must
be extended from the original `WebSocket`. Defaults to `WebSocket`.
- `callback` {Function}

Create a new server instance. One and only one of `port`, `server` or `noServer`
Expand Down
5 changes: 4 additions & 1 deletion lib/websocket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class WebSocketServer extends EventEmitter {
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
* not to skip UTF-8 validation for text and close messages
* @param {Function} [options.verifyClient] A hook to reject connections
* @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
* class to use. It must be the `WebSocket` class or class that extends it
* @param {Function} [callback] A listener for the `listening` event
*/
constructor(options, callback) {
Expand All @@ -67,6 +69,7 @@ class WebSocketServer extends EventEmitter {
host: null,
path: null,
port: null,
WebSocket,
...options
};

Expand Down Expand Up @@ -356,7 +359,7 @@ class WebSocketServer extends EventEmitter {
`Sec-WebSocket-Accept: ${digest}`
];

const ws = new WebSocket(null);
const ws = new this.options.WebSocket(null);

if (protocols.size) {
//
Expand Down
26 changes: 26 additions & 0 deletions test/websocket-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ describe('WebSocketServer', () => {
wss.close(done);
});
});

it('honors the `WebSocket` option', (done) => {
class CustomWebSocket extends WebSocket.WebSocket {
get foo() {
return 'foo';
}
}

const wss = new WebSocket.Server(
{
port: 0,
WebSocket: CustomWebSocket
},
() => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);

ws.on('open', ws.close);
}
);

wss.on('connection', (ws) => {
assert.ok(ws instanceof CustomWebSocket);
assert.strictEqual(ws.foo, 'foo');
wss.close(done);
});
});
});

it('emits an error if http server bind fails', (done) => {
Expand Down