Skip to content

Commit 1d3101e

Browse files
Carlos GottbergPatrickJS
Carlos Gottberg
authored andcommitted
Allow ws options to be passed to constructors (#107)
This complies with the way ws behaves and is useful for specifying "low-level" ws options such as adding additional headers to the request (use case: authentication through jwt)
1 parent f0dec08 commit 1d3101e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Diff for: src/angular-websocket.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ if (!Array.prototype.indexOf) {
4343
// $WebSocketProvider.$inject = ['$rootScope', '$q', '$timeout', '$websocketBackend'];
4444
function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) {
4545

46-
function $WebSocket(url, protocols, options) {
46+
function $WebSocket(url, protocols, options, wsOptions = {}) {
4747
if (!options && isObject(protocols) && !isArray(protocols)) {
4848
options = protocols;
4949
protocols = undefined;
5050
}
51-
51+
this.wsOptions = wsOptions;
5252
this.protocols = protocols;
5353
this.url = url || 'Missing URL';
5454
this.ssl = /(wss)/i.test(this.url);
@@ -121,7 +121,7 @@ function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) {
121121

122122
$WebSocket.prototype._connect = function _connect(force) {
123123
if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) {
124-
this.socket = $websocketBackend.create(this.url, this.protocols);
124+
this.socket = $websocketBackend.create(this.url, this.protocols, this.wsOptions);
125125
this.socket.onmessage = angular.bind(this, this._onMessageHandler);
126126
this.socket.onopen = angular.bind(this, this._onOpenHandler);
127127
this.socket.onerror = angular.bind(this, this._onErrorHandler);
@@ -367,13 +367,17 @@ function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) {
367367

368368
// $WebSocketBackendProvider.$inject = ['$log'];
369369
function $WebSocketBackendProvider($log) {
370-
this.create = function create(url, protocols) {
370+
this.create = function create(url, protocols, options) {
371371
var match = /wss?:\/\//.exec(url);
372372

373373
if (!match) {
374374
throw new Error('Invalid url provided');
375375
}
376376

377+
if (options) {
378+
return new Socket(url, protocols, options);
379+
}
380+
377381
if (protocols) {
378382
return new Socket(url, protocols);
379383
}

0 commit comments

Comments
 (0)