|
@@ -1,28 +1,72 @@
|
|
|
-(function() {
|
|
|
+(function (global, factory) {
|
|
|
+ if (typeof define === "function" && define.amd) {
|
|
|
+ define(['module', 'exports', 'angular', 'ws'], factory);
|
|
|
+ } else if (typeof exports !== "undefined") {
|
|
|
+ factory(module, exports, require('angular'), require('ws'));
|
|
|
+ } else {
|
|
|
+ var mod = {
|
|
|
+ exports: {}
|
|
|
+ };
|
|
|
+ factory(mod, mod.exports, global.angular, global.ws);
|
|
|
+ global.angularWebsocket = mod.exports;
|
|
|
+ }
|
|
|
+})(this, function (module, exports, _angular, ws) {
|
|
|
'use strict';
|
|
|
|
|
|
- var noop = angular.noop;
|
|
|
- var objectFreeze = (Object.freeze) ? Object.freeze : noop;
|
|
|
+ Object.defineProperty(exports, "__esModule", {
|
|
|
+ value: true
|
|
|
+ });
|
|
|
+
|
|
|
+ var _angular2 = _interopRequireDefault(_angular);
|
|
|
+
|
|
|
+ function _interopRequireDefault(obj) {
|
|
|
+ return obj && obj.__esModule ? obj : {
|
|
|
+ default: obj
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
|
|
|
+ return typeof obj;
|
|
|
+ } : function (obj) {
|
|
|
+ return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
|
+ };
|
|
|
+
|
|
|
+ var Socket;
|
|
|
+
|
|
|
+ if (typeof window === 'undefined') {
|
|
|
+ try {
|
|
|
+
|
|
|
+ Socket = ws.Client || ws.client || ws;
|
|
|
+ } catch (e) {}
|
|
|
+ }
|
|
|
+
|
|
|
+ // Browser
|
|
|
+ Socket = Socket || window.WebSocket || window.MozWebSocket;
|
|
|
+
|
|
|
+ var noop = _angular2.default.noop;
|
|
|
+ var objectFreeze = Object.freeze ? Object.freeze : noop;
|
|
|
var objectDefineProperty = Object.defineProperty;
|
|
|
- var isString = angular.isString;
|
|
|
- var isFunction = angular.isFunction;
|
|
|
- var isDefined = angular.isDefined;
|
|
|
- var isObject = angular.isObject;
|
|
|
- var isArray = angular.isArray;
|
|
|
- var forEach = angular.forEach;
|
|
|
+ var isString = _angular2.default.isString;
|
|
|
+ var isFunction = _angular2.default.isFunction;
|
|
|
+ var isDefined = _angular2.default.isDefined;
|
|
|
+ var isObject = _angular2.default.isObject;
|
|
|
+ var isArray = _angular2.default.isArray;
|
|
|
+ var forEach = _angular2.default.forEach;
|
|
|
var arraySlice = Array.prototype.slice;
|
|
|
// ie8 wat
|
|
|
if (!Array.prototype.indexOf) {
|
|
|
- Array.prototype.indexOf = function(elt /*, from*/) {
|
|
|
+ Array.prototype.indexOf = function (elt /*, from*/) {
|
|
|
var len = this.length >>> 0;
|
|
|
var from = Number(arguments[1]) || 0;
|
|
|
- from = (from < 0) ? Math.ceil(from) : Math.floor(from);
|
|
|
+ from = from < 0 ? Math.ceil(from) : Math.floor(from);
|
|
|
if (from < 0) {
|
|
|
from += len;
|
|
|
}
|
|
|
|
|
|
for (; from < len; from++) {
|
|
|
- if (from in this && this[from] === elt) { return from; }
|
|
|
+ if (from in this && this[from] === elt) {
|
|
|
+ return from;
|
|
|
+ }
|
|
|
}
|
|
|
return -1;
|
|
|
};
|
|
@@ -48,20 +92,20 @@
|
|
|
// this.buffer = [];
|
|
|
|
|
|
// TODO: refactor options to use isDefined
|
|
|
- this.scope = options && options.scope || $rootScope;
|
|
|
- this.rootScopeFailover = options && options.rootScopeFailover && true;
|
|
|
- this.useApplyAsync = options && options.useApplyAsync || false;
|
|
|
- this.initialTimeout = options && options.initialTimeout || 500; // 500ms
|
|
|
- this.maxTimeout = options && options.maxTimeout || 5 * 60 * 1000; // 5 minutes
|
|
|
- this.reconnectIfNotNormalClose = options && options.reconnectIfNotNormalClose || false;
|
|
|
- this.binaryType = options && options.binaryType || 'blob';
|
|
|
+ this.scope = options && options.scope || $rootScope;
|
|
|
+ this.rootScopeFailover = options && options.rootScopeFailover && true;
|
|
|
+ this.useApplyAsync = options && options.useApplyAsync || false;
|
|
|
+ this.initialTimeout = options && options.initialTimeout || 500; // 500ms
|
|
|
+ this.maxTimeout = options && options.maxTimeout || 5 * 60 * 1000; // 5 minutes
|
|
|
+ this.reconnectIfNotNormalClose = options && options.reconnectIfNotNormalClose || false;
|
|
|
+ this.binaryType = options && options.binaryType || 'blob';
|
|
|
|
|
|
this._reconnectAttempts = 0;
|
|
|
- this.sendQueue = [];
|
|
|
- this.onOpenCallbacks = [];
|
|
|
+ this.sendQueue = [];
|
|
|
+ this.onOpenCallbacks = [];
|
|
|
this.onMessageCallbacks = [];
|
|
|
- this.onErrorCallbacks = [];
|
|
|
- this.onCloseCallbacks = [];
|
|
|
+ this.onErrorCallbacks = [];
|
|
|
+ this.onCloseCallbacks = [];
|
|
|
|
|
|
objectFreeze(this._readyStateConstants);
|
|
|
|
|
@@ -70,10 +114,8 @@
|
|
|
} else {
|
|
|
this._setInternalState(0);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
$WebSocket.prototype._readyStateConstants = {
|
|
|
'CONNECTING': 0,
|
|
|
'OPEN': 1,
|
|
@@ -84,9 +126,7 @@
|
|
|
|
|
|
$WebSocket.prototype._normalCloseCode = 1000;
|
|
|
|
|
|
- $WebSocket.prototype._reconnectableStatusCodes = [
|
|
|
- 4000
|
|
|
- ];
|
|
|
+ $WebSocket.prototype._reconnectableStatusCodes = [4000];
|
|
|
|
|
|
$WebSocket.prototype.safeDigest = function safeDigest(autoApply) {
|
|
|
if (autoApply && !this.scope.$$phase) {
|
|
@@ -99,7 +139,7 @@
|
|
|
if (scope) {
|
|
|
this.scope = scope;
|
|
|
if (this.rootScopeFailover) {
|
|
|
- this.scope.$on('$destroy', function() {
|
|
|
+ this.scope.$on('$destroy', function () {
|
|
|
self.scope = $rootScope;
|
|
|
});
|
|
|
}
|
|
@@ -110,10 +150,10 @@
|
|
|
$WebSocket.prototype._connect = function _connect(force) {
|
|
|
if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) {
|
|
|
this.socket = $websocketBackend.create(this.url, this.protocols);
|
|
|
- this.socket.onmessage = angular.bind(this, this._onMessageHandler);
|
|
|
- this.socket.onopen = angular.bind(this, this._onOpenHandler);
|
|
|
- this.socket.onerror = angular.bind(this, this._onErrorHandler);
|
|
|
- this.socket.onclose = angular.bind(this, this._onCloseHandler);
|
|
|
+ this.socket.onmessage = _angular2.default.bind(this, this._onMessageHandler);
|
|
|
+ this.socket.onopen = _angular2.default.bind(this, this._onOpenHandler);
|
|
|
+ this.socket.onerror = _angular2.default.bind(this, this._onErrorHandler);
|
|
|
+ this.socket.onclose = _angular2.default.bind(this, this._onCloseHandler);
|
|
|
this.socket.binaryType = this.binaryType;
|
|
|
}
|
|
|
};
|
|
@@ -122,9 +162,7 @@
|
|
|
while (this.sendQueue.length && this.socket.readyState === this._readyStateConstants.OPEN) {
|
|
|
var data = this.sendQueue.shift();
|
|
|
|
|
|
- this.socket.send(
|
|
|
- isString(data.message) || this.binaryType != "blob" ? data.message : JSON.stringify(data.message)
|
|
|
- );
|
|
|
+ this.socket.send(isString(data.message) || this.binaryType != 'blob' ? data.message : JSON.stringify(data.message));
|
|
|
data.deferred.resolve();
|
|
|
}
|
|
|
};
|
|
@@ -162,7 +200,6 @@
|
|
|
return this;
|
|
|
};
|
|
|
|
|
|
-
|
|
|
$WebSocket.prototype.onMessage = function onMessage(callback, options) {
|
|
|
if (!isFunction(callback)) {
|
|
|
throw new Error('Callback must be a function');
|
|
@@ -189,14 +226,14 @@
|
|
|
$WebSocket.prototype._onCloseHandler = function _onCloseHandler(event) {
|
|
|
var self = this;
|
|
|
if (self.useApplyAsync) {
|
|
|
- self.scope.$applyAsync(function() {
|
|
|
+ self.scope.$applyAsync(function () {
|
|
|
self.notifyCloseCallbacks(event);
|
|
|
});
|
|
|
} else {
|
|
|
self.notifyCloseCallbacks(event);
|
|
|
- self.safeDigest(autoApply);
|
|
|
+ self.safeDigest(true);
|
|
|
}
|
|
|
- if ((this.reconnectIfNotNormalClose && event.code !== this._normalCloseCode) || this._reconnectableStatusCodes.indexOf(event.code) > -1) {
|
|
|
+ if (this.reconnectIfNotNormalClose && event.code !== this._normalCloseCode || this._reconnectableStatusCodes.indexOf(event.code) > -1) {
|
|
|
this.reconnect();
|
|
|
}
|
|
|
};
|
|
@@ -204,12 +241,12 @@
|
|
|
$WebSocket.prototype._onErrorHandler = function _onErrorHandler(event) {
|
|
|
var self = this;
|
|
|
if (self.useApplyAsync) {
|
|
|
- self.scope.$applyAsync(function() {
|
|
|
+ self.scope.$applyAsync(function () {
|
|
|
self.notifyErrorCallbacks(event);
|
|
|
});
|
|
|
} else {
|
|
|
self.notifyErrorCallbacks(event);
|
|
|
- self.safeDigest(autoApply);
|
|
|
+ self.safeDigest(true);
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -223,12 +260,10 @@
|
|
|
if (pattern) {
|
|
|
if (isString(pattern) && message.data === pattern) {
|
|
|
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
|
|
|
- }
|
|
|
- else if (pattern instanceof RegExp && pattern.exec(message.data)) {
|
|
|
+ } else if (pattern instanceof RegExp && pattern.exec(message.data)) {
|
|
|
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
|
|
|
}
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
|
|
|
}
|
|
|
}
|
|
@@ -236,7 +271,7 @@
|
|
|
function applyAsyncOrDigest(callback, autoApply, args) {
|
|
|
args = arraySlice.call(arguments, 2);
|
|
|
if (self.useApplyAsync) {
|
|
|
- self.scope.$applyAsync(function() {
|
|
|
+ self.scope.$applyAsync(function () {
|
|
|
callback.apply(self, args);
|
|
|
});
|
|
|
} else {
|
|
@@ -244,7 +279,6 @@
|
|
|
self.safeDigest(autoApply);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
};
|
|
|
|
|
|
$WebSocket.prototype.close = function close(force) {
|
|
@@ -261,8 +295,7 @@
|
|
|
|
|
|
if (self.readyState === self._readyStateConstants.RECONNECT_ABORTED) {
|
|
|
deferred.reject('Socket connection has been closed');
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
self.sendQueue.push({
|
|
|
message: data,
|
|
|
deferred: deferred
|
|
@@ -274,7 +307,7 @@
|
|
|
function cancelableify(promise) {
|
|
|
promise.cancel = cancel;
|
|
|
var then = promise.then;
|
|
|
- promise.then = function() {
|
|
|
+ promise.then = function () {
|
|
|
var newPromise = then.apply(this, arguments);
|
|
|
return cancelableify(newPromise);
|
|
|
};
|
|
@@ -287,8 +320,7 @@
|
|
|
return self;
|
|
|
}
|
|
|
|
|
|
- if ($websocketBackend.isMocked && $websocketBackend.isMocked() &&
|
|
|
- $websocketBackend.isConnected(this.url)) {
|
|
|
+ if ($websocketBackend.isMocked && $websocketBackend.isMocked() && $websocketBackend.isConnected(this.url)) {
|
|
|
this._onMessageHandler($websocketBackend.mockSend());
|
|
|
}
|
|
|
|
|
@@ -303,7 +335,7 @@
|
|
|
var backoffDelaySeconds = backoffDelay / 1000;
|
|
|
console.log('Reconnecting in ' + backoffDelaySeconds + ' seconds');
|
|
|
|
|
|
- $timeout(angular.bind(this, this._connect), backoffDelay);
|
|
|
+ $timeout(_angular2.default.bind(this, this._connect), backoffDelay);
|
|
|
|
|
|
return this;
|
|
|
};
|
|
@@ -330,8 +362,7 @@
|
|
|
}
|
|
|
this._internalConnectionState = state;
|
|
|
|
|
|
-
|
|
|
- forEach(this.sendQueue, function(pending) {
|
|
|
+ forEach(this.sendQueue, function (pending) {
|
|
|
pending.deferred.reject('Message cancelled due to closed socket connection');
|
|
|
});
|
|
|
};
|
|
@@ -339,62 +370,46 @@
|
|
|
// Read only .readyState
|
|
|
if (objectDefineProperty) {
|
|
|
objectDefineProperty($WebSocket.prototype, 'readyState', {
|
|
|
- get: function() {
|
|
|
+ get: function get() {
|
|
|
return this._internalConnectionState || this.socket.readyState;
|
|
|
},
|
|
|
- set: function() {
|
|
|
+ set: function set() {
|
|
|
throw new Error('The readyState property is read-only');
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- return function(url, protocols, options) {
|
|
|
+ return function (url, protocols, options) {
|
|
|
return new $WebSocket(url, protocols, options);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- // $WebSocketBackendProvider.$inject = ['$window', '$log'];
|
|
|
- function $WebSocketBackendProvider($window, $log) {
|
|
|
+ // $WebSocketBackendProvider.$inject = ['$log'];
|
|
|
+ function $WebSocketBackendProvider($log) {
|
|
|
this.create = function create(url, protocols) {
|
|
|
var match = /wss?:\/\//.exec(url);
|
|
|
- var Socket, ws;
|
|
|
+
|
|
|
if (!match) {
|
|
|
throw new Error('Invalid url provided');
|
|
|
}
|
|
|
|
|
|
- // CommonJS
|
|
|
- if (typeof exports === 'object' && require) {
|
|
|
- try {
|
|
|
- ws = require('ws');
|
|
|
- Socket = (ws.Client || ws.client || ws);
|
|
|
- } catch(e) {}
|
|
|
- }
|
|
|
-
|
|
|
- // Browser
|
|
|
- Socket = Socket || $window.WebSocket || $window.MozWebSocket;
|
|
|
-
|
|
|
if (protocols) {
|
|
|
return new Socket(url, protocols);
|
|
|
}
|
|
|
|
|
|
return new Socket(url);
|
|
|
};
|
|
|
+
|
|
|
this.createWebSocketBackend = function createWebSocketBackend(url, protocols) {
|
|
|
$log.warn('Deprecated: Please use .create(url, protocols)');
|
|
|
return this.create(url, protocols);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- angular.module('ngWebSocket', [])
|
|
|
- .factory('$websocket', ['$rootScope', '$q', '$timeout', '$websocketBackend', $WebSocketProvider])
|
|
|
- .factory('WebSocket', ['$rootScope', '$q', '$timeout', 'WebsocketBackend', $WebSocketProvider])
|
|
|
- .service('$websocketBackend', ['$window', '$log', $WebSocketBackendProvider])
|
|
|
- .service('WebSocketBackend', ['$window', '$log', $WebSocketBackendProvider]);
|
|
|
-
|
|
|
+ _angular2.default.module('ngWebSocket', []).factory('$websocket', ['$rootScope', '$q', '$timeout', '$websocketBackend', $WebSocketProvider]).factory('WebSocket', ['$rootScope', '$q', '$timeout', 'WebsocketBackend', $WebSocketProvider]).service('$websocketBackend', ['$log', $WebSocketBackendProvider]).service('WebSocketBackend', ['$log', $WebSocketBackendProvider]);
|
|
|
|
|
|
- angular.module('angular-websocket', ['ngWebSocket']);
|
|
|
+ _angular2.default.module('angular-websocket', ['ngWebSocket']);
|
|
|
|
|
|
- if (typeof module === 'object' && typeof define !== 'function') {
|
|
|
- module.exports = angular.module('ngWebSocket');
|
|
|
- }
|
|
|
-}());
|
|
|
+ exports.default = _angular2.default.module('ngWebSocket');
|
|
|
+ module.exports = exports['default'];
|
|
|
+});
|