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

Make WebSocketServer#handleUpgrade() callback optional #1968

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ tracking is disabled. In this case the `'close'` event is emitted in the next
tick. The optional callback is called when the `'close'` event occurs and
receives an `Error` if the server is already closed.

### server.handleUpgrade(request, socket, head, callback)
### server.handleUpgrade(request, socket, head[, callback])

- `request` {http.IncomingMessage} The client HTTP GET request.
- `socket` {net.Socket} The network socket between the server and client.
Expand All @@ -236,6 +236,9 @@ If the upgrade is successful, the `callback` is called with two arguments:
- `websocket` {WebSocket} A `WebSocket` object.
- `request` {http.IncomingMessage} The client HTTP GET request.

If the `callback` is not provided the [`'connection'`](#event-connection) event
is emitted instead.

### server.shouldHandle(request)

- `request` {http.IncomingMessage} The client HTTP GET request.
Expand Down
16 changes: 8 additions & 8 deletions lib/websocket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,10 @@ class WebSocketServer extends EventEmitter {
}

if (this._server) {
const emitConnection = this.emit.bind(this, 'connection');

this._removeListeners = addListeners(this._server, {
listening: this.emit.bind(this, 'listening'),
error: this.emit.bind(this, 'error'),
upgrade: (req, socket, head) => {
this.handleUpgrade(req, socket, head, emitConnection);
}
upgrade: this.handleUpgrade.bind(this)
});
}

Expand Down Expand Up @@ -221,7 +217,7 @@ class WebSocketServer extends EventEmitter {
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Function} cb Callback
* @param {Function} [cb] Callback
* @public
*/
handleUpgrade(req, socket, head, cb) {
Expand Down Expand Up @@ -326,7 +322,7 @@ class WebSocketServer extends EventEmitter {
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Function} cb Callback
* @param {Function} [cb] Callback
* @throws {Error} If called more than once with the same socket
* @private
*/
Expand Down Expand Up @@ -405,7 +401,11 @@ class WebSocketServer extends EventEmitter {
});
}

cb(ws, req);
if (typeof cb === 'function') {
cb(ws, req);
} else {
this.emit('connection', ws, req);
}
}
}

Expand Down