Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow pass additional request headers as option for web socket client #106

Closed
wants to merge 1 commit into from

6 participants

@martynovs

In my case this gives support for OAuth authorization

@einaros
Owner

Thanks, I'll test this and include in the next version!

@damianb

Heartbeat check, what's the status of this? Has this been implemented separately, or is it still to be handled?

@jjp91

For a server, could it be possible to verify origin of the client connecting and eventually close only one connection that is open.

@nbrownus

Ping, would be nice to have this.

@3rd-Eden 3rd-Eden referenced this pull request from a commit
@3rd-Eden 3rd-Eden [fix] Allow custom headers. 43a0b2b
@3rd-Eden
Owner

Fixed with the commit issued above.

@3rd-Eden 3rd-Eden closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 10 deletions.
  1. +12 −10 lib/WebSocket.js
View
22 lib/WebSocket.js
@@ -392,7 +392,8 @@ function initAsClient(address, options) {
options = new Options({
origin: null,
protocolVersion: protocolVersion,
- protocol: null
+ protocol: null,
+ headers: null
}).merge(options);
if (options.value.protocolVersion != 8 && options.value.protocolVersion != 13) {
throw new Error('unsupported protocol version');
@@ -427,18 +428,19 @@ function initAsClient(address, options) {
});
}
+ var requestHeaders = options.value.headers || {};
+ requestHeaders['Connection'] = 'Upgrade';
+ requestHeaders['Upgrade'] = 'websocket';
+ requestHeaders['Sec-WebSocket-Version'] = options.value.protocolVersion;
+ requestHeaders['Sec-WebSocket-Key'] = key;
+
var requestOptions = {
port: serverUrl.port || (isSecure ? 443 : 80),
host: serverUrl.hostname,
- headers: {
- 'Connection': 'Upgrade',
- 'Upgrade': 'websocket',
- 'Sec-WebSocket-Version': options.value.protocolVersion,
- 'Sec-WebSocket-Key': key
- }
+ headers: requestHeaders
};
if (options.value.protocol) {
- requestOptions.headers['Sec-WebSocket-Protocol'] = options.value.protocol;
+ requestHeaders['Sec-WebSocket-Protocol'] = options.value.protocol;
}
if (isNodeV4) {
requestOptions.path = (serverUrl.pathname || '/') + (serverUrl.search || '');
@@ -449,8 +451,8 @@ function initAsClient(address, options) {
requestOptions.socketPath = serverUrl.pathname;
}
if (options.value.origin) {
- if (options.value.protocolVersion < 13) requestOptions.headers['Sec-WebSocket-Origin'] = options.value.origin;
- else requestOptions.headers['Origin'] = options.value.origin;
+ var header = options.value.protocolVersion < 13 ? 'Sec-WebSocket-Origin' : 'Origin';
+ requestHeaders[header] = options.value.origin;
}
var self = this;
Something went wrong with that request. Please try again.