Skip to content
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
18 changes: 14 additions & 4 deletions lib/WebSocketClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var url = require('url');
var crypto = require('crypto');
var WebSocketConnection = require('./WebSocketConnection');
var bufferAllocUnsafe = utils.bufferAllocUnsafe;
var Extensions = require('websocket-extensions');
var deflate = require('permessage-deflate');


var protocolSeparators = [
'(', ')', '<', '>', '@',
Expand All @@ -39,6 +42,7 @@ function WebSocketClient(config) {
EventEmitter.call(this);

// TODO: Implement extensions
this._exts = new Extensions();

this.config = {
// 1MiB max frame size.
Expand Down Expand Up @@ -103,21 +107,25 @@ function WebSocketClient(config) {
}

this._req = null;

switch (this.config.webSocketVersion) {
case 8:
case 13:
break;
default:
throw new Error('Requested webSocketVersion is not supported. Allowed values are 8 and 13.');
}

if (this.config.perMessageDeflate) {
this._exts.add(deflate);
}
}

util.inherits(WebSocketClient, EventEmitter);

WebSocketClient.prototype.connect = function(requestUrl, protocols, origin, headers, extraRequestOptions) {
var self = this;

if (typeof(protocols) === 'string') {
if (protocols.length > 0) {
protocols = [protocols];
Expand Down Expand Up @@ -193,7 +201,8 @@ WebSocketClient.prototype.connect = function(requestUrl, protocols, origin, head
'Connection': 'Upgrade',
'Sec-WebSocket-Version': this.config.webSocketVersion.toString(10),
'Sec-WebSocket-Key': this.base64nonce,
'Host': reqHeaders.Host || hostHeaderValue
'Host': reqHeaders.Host || hostHeaderValue,
'Sec-WebSocket-Extensions': this._exts.generateOffer(),
});

if (this.protocols.length > 0) {
Expand Down Expand Up @@ -257,6 +266,7 @@ WebSocketClient.prototype.connect = function(requestUrl, protocols, origin, head
req.removeListener('error', handleRequestError);
self.socket = socket;
self.response = response;
self._exts.activate(response.headers['sec-websocket-extensions']);
self.firstDataChunk = head;
self.validateHandshake();
});
Expand Down Expand Up @@ -340,7 +350,7 @@ WebSocketClient.prototype.failHandshake = function(errorDescription) {
};

WebSocketClient.prototype.succeedHandshake = function() {
var connection = new WebSocketConnection(this.socket, [], this.protocol, true, this.config);
var connection = new WebSocketConnection(this.socket, [], this.protocol, true, this.config, this._exts);

connection.webSocketVersion = this.config.webSocketVersion;
connection._addSocketEventListeners();
Expand Down
Loading