angular-websocket
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -14,3 +14,3 @@ (function() { | ||
function $MockWebSocket(url, protocols) { | ||
this.protocols = protocols || 'Sec-WebSocket-Protocol'; | ||
this.protocols = protocols; | ||
this.ssl = /(wss)/i.test(this.url); | ||
@@ -17,0 +17,0 @@ |
@@ -56,4 +56,2 @@ (function() { | ||
function $WebSocket(url, protocols, options) { | ||
// var bits = url.split('/'); | ||
if (!options && isObject(protocols) && !isArray(protocols)) { | ||
@@ -77,3 +75,3 @@ options = protocols; | ||
this.rootScopeFailover = options && options.rootScopeFailover && true; | ||
// this.useApplyAsync = options && options.useApplyAsync || false; | ||
this.useApplyAsync = options && options.useApplyAsync || false; | ||
this._reconnectAttempts = options && options.reconnectAttempts || 0; | ||
@@ -118,2 +116,3 @@ this.initialTimeout = options && options.initialTimeout || 500; // 500ms | ||
$WebSocket.prototype.bindToScope = function bindToScope(scope) { | ||
var self = this; | ||
if (scope) { | ||
@@ -123,7 +122,7 @@ this.scope = scope; | ||
this.scope.$on('$destroy', function() { | ||
this.scope = $rootScope; | ||
self.scope = $rootScope; | ||
}); | ||
} | ||
} | ||
return this; | ||
return self; | ||
}; | ||
@@ -134,4 +133,4 @@ | ||
this.socket = $websocketBackend.create(this.url, this.protocols); | ||
this.socket.onopen = this._onOpenHandler.bind(this); | ||
this.socket.onmessage = this._onMessageHandler.bind(this); | ||
this.socket.onopen = this._onOpenHandler.bind(this); | ||
this.socket.onerror = this._onErrorHandler.bind(this); | ||
@@ -153,5 +152,5 @@ this.socket.onclose = this._onCloseHandler.bind(this); | ||
$WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks() { | ||
$WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks(event) { | ||
for (var i = 0; i < this.onOpenCallbacks.length; i++) { | ||
this.onOpenCallbacks[i].call(this); | ||
this.onOpenCallbacks[i].call(this, event); | ||
} | ||
@@ -205,5 +204,5 @@ }; | ||
$WebSocket.prototype._onOpenHandler = function _onOpenHandler() { | ||
$WebSocket.prototype._onOpenHandler = function _onOpenHandler(event) { | ||
this._reconnectAttempts = 0; | ||
this.notifyOpenCallbacks(); | ||
this.notifyOpenCallbacks(event); | ||
this.fireQueue(); | ||
@@ -225,22 +224,32 @@ }; | ||
var pattern; | ||
var socketInstance = this; | ||
var self = this; | ||
var currentCallback; | ||
for (var i = 0; i < socketInstance.onMessageCallbacks.length; i++) { | ||
currentCallback = socketInstance.onMessageCallbacks[i]; | ||
for (var i = 0; i < self.onMessageCallbacks.length; i++) { | ||
currentCallback = self.onMessageCallbacks[i]; | ||
pattern = currentCallback.pattern; | ||
if (pattern) { | ||
if (isString(pattern) && message.data === pattern) { | ||
currentCallback.fn.call(socketInstance, message); | ||
socketInstance.safeDigest(currentCallback.autoApply); | ||
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); | ||
} | ||
else if (pattern instanceof RegExp && pattern.exec(message.data)) { | ||
currentCallback.fn.call(socketInstance, message); | ||
socketInstance.safeDigest(currentCallback.autoApply); | ||
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); | ||
} | ||
} | ||
else { | ||
currentCallback.fn.call(socketInstance, message); | ||
socketInstance.safeDigest(currentCallback.autoApply); | ||
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); | ||
} | ||
} | ||
function applyAsyncOrDigest(callback, autoApply, args) { | ||
args = arraySlice.call(arguments, 2); | ||
if (self.useApplyAsync) { | ||
self.scope.$applyAsync(function() { | ||
callback.apply(self, args); | ||
}); | ||
} else { | ||
callback.apply(self, args); | ||
self.safeDigest(autoApply); | ||
} | ||
} | ||
}; | ||
@@ -257,14 +266,14 @@ | ||
var deferred = $q.defer(); | ||
var socketInstance = this; | ||
var self = this; | ||
var promise = cancelableify(deferred.promise); | ||
if (socketInstance.readyState === socketInstance._readyStateConstants.RECONNECT_ABORTED) { | ||
if (self.readyState === self._readyStateConstants.RECONNECT_ABORTED) { | ||
deferred.reject('Socket connection has been closed'); | ||
} | ||
else { | ||
socketInstance.sendQueue.push({ | ||
self.sendQueue.push({ | ||
message: data, | ||
deferred: deferred | ||
}); | ||
socketInstance.fireQueue(); | ||
self.fireQueue(); | ||
} | ||
@@ -284,5 +293,5 @@ | ||
function cancel(reason) { | ||
socketInstance.sendQueue.splice(socketInstance.sendQueue.indexOf(data), 1); | ||
self.sendQueue.splice(self.sendQueue.indexOf(data), 1); | ||
deferred.reject(reason); | ||
return socketInstance; | ||
return self; | ||
} | ||
@@ -294,8 +303,7 @@ | ||
$WebSocket.prototype.reconnect = function reconnect() { | ||
var socketInstance = this; | ||
socketInstance.close(); | ||
this.close(); | ||
$timeout(socketInstance._connect, socketInstance._getBackoffDelay(++socketInstance._reconnectAttempts)); | ||
$timeout(this._connect.bind(this), this._getBackoffDelay(++this._reconnectAttempts)); | ||
return socketInstance; | ||
return this; | ||
}; | ||
@@ -348,3 +356,3 @@ // Exponential Backoff Formula by Prof. Douglas Thain | ||
// $WebSocketBackendProvider.$inject = ['$window']; | ||
// $WebSocketBackendProvider.$inject = ['$window', '$log']; | ||
function $WebSocketBackendProvider($window, $log) { | ||
@@ -351,0 +359,0 @@ this.create = function create(url, protocols) { |
@@ -1,2 +0,2 @@ | ||
!function(){"use strict";function t(t,e,o,u){function p(e,o,r){r||!c(o)||l(o)||(r=o,o=void 0),this.protocols=o,this.url=e||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=r&&r.scope||t,this.rootScopeFailover=r&&r.rootScopeFailover&&!0,this._reconnectAttempts=r&&r.reconnectAttempts||0,this.initialTimeout=r&&r.initialTimeout||500,this.maxTimeout=r&&r.maxTimeout||3e5,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],n(this._readyStateConstants),e?this._connect():this._setInternalState(0)}return p.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},p.prototype._reconnectableStatusCodes=[4e3],p.prototype.safeDigest=function(t){t&&!this.scope.$$phase&&this.scope.$digest()},p.prototype.bindToScope=function(e){return e&&(this.scope=e,this.rootScopeFailover&&this.scope.$on("$destroy",function(){this.scope=t})),this},p.prototype._connect=function(t){(t||!this.socket||this.socket.readyState!==this._readyStateConstants.OPEN)&&(this.socket=u.create(this.url,this.protocols),this.socket.onopen=this._onOpenHandler.bind(this),this.socket.onmessage=this._onMessageHandler.bind(this),this.socket.onerror=this._onErrorHandler.bind(this),this.socket.onclose=this._onCloseHandler.bind(this))},p.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var t=this.sendQueue.shift();this.socket.send(s(t.message)?t.message:JSON.stringify(t.message)),t.deferred.resolve()}},p.prototype.notifyOpenCallbacks=function(){for(var t=0;t<this.onOpenCallbacks.length;t++)this.onOpenCallbacks[t].call(this)},p.prototype.notifyCloseCallbacks=function(t){for(var e=0;e<this.onCloseCallbacks.length;e++)this.onCloseCallbacks[e].call(this,t)},p.prototype.notifyErrorCallbacks=function(t){for(var e=0;e<this.onErrorCallbacks.length;e++)this.onErrorCallbacks[e].call(this,t)},p.prototype.onOpen=function(t){return this.onOpenCallbacks.push(t),this},p.prototype.onClose=function(t){return this.onCloseCallbacks.push(t),this},p.prototype.onError=function(t){return this.onErrorCallbacks.push(t),this},p.prototype.onMessage=function(t,e){if(!i(t))throw new Error("Callback must be a function");if(e&&a(e.filter)&&!s(e.filter)&&!(e.filter instanceof RegExp))throw new Error("Pattern must be a string or regular expression");return this.onMessageCallbacks.push({fn:t,pattern:e?e.filter:void 0,autoApply:e?e.autoApply:!0}),this},p.prototype._onOpenHandler=function(){this._reconnectAttempts=0,this.notifyOpenCallbacks(),this.fireQueue()},p.prototype._onCloseHandler=function(t){this.notifyCloseCallbacks(t),this._reconnectableStatusCodes.indexOf(t.code)>-1&&this.reconnect()},p.prototype._onErrorHandler=function(t){this.notifyErrorCallbacks(t)},p.prototype._onMessageHandler=function(t){for(var e,o,n=this,r=0;r<n.onMessageCallbacks.length;r++)o=n.onMessageCallbacks[r],e=o.pattern,e?s(e)&&t.data===e?(o.fn.call(n,t),n.safeDigest(o.autoApply)):e instanceof RegExp&&e.exec(t.data)&&(o.fn.call(n,t),n.safeDigest(o.autoApply)):(o.fn.call(n,t),n.safeDigest(o.autoApply))},p.prototype.close=function(t){return(t||!this.socket.bufferedAmount)&&this.socket.close(),this},p.prototype.send=function(t){function o(t){t.cancel=n;var e=t.then;return t.then=function(){var t=e.apply(this,arguments);return o(t)},t}function n(e){return s.sendQueue.splice(s.sendQueue.indexOf(t),1),r.reject(e),s}var r=e.defer(),s=this,i=o(r.promise);return s.readyState===s._readyStateConstants.RECONNECT_ABORTED?r.reject("Socket connection has been closed"):(s.sendQueue.push({message:t,deferred:r}),s.fireQueue()),i},p.prototype.reconnect=function(){var t=this;return t.close(),o(t._connect,t._getBackoffDelay(++t._reconnectAttempts)),t},p.prototype._getBackoffDelay=function(t){var e=Math.random()+1,o=this.initialTimeout,n=2,r=t,s=this.maxTimeout;return Math.floor(Math.min(e*o*Math.pow(n,r),s))},p.prototype._setInternalState=function(t){if(Math.floor(t)!==t||0>t||t>4)throw new Error("state must be an integer between 0 and 4, got: "+t);r||(this.readyState=t||this.socket.readyState),this._internalConnectionState=t,angular.forEach(this.sendQueue,function(t){t.deferred.reject("Message cancelled due to closed socket connection")})},r&&r(p.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(t,e){return new p(t,e)}}function e(t,e){this.create=function(e,o){var n,r,s=/wss?:\/\//.exec(e);if(!s)throw new Error("Invalid url provided");if("object"==typeof exports&&require)try{r=require("ws"),n=r.Client||r.client||r}catch(i){}return n=n||t.WebSocket||t.MozWebSocket,o?new n(e,o):new n(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}var o=angular.noop,n=Object.freeze?Object.freeze:o,r=Object.defineProperty,s=angular.isString,i=angular.isFunction,a=angular.isDefined,c=angular.isObject,l=angular.isArray,u=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(t){var e=this.length>>>0,o=Number(arguments[1])||0;for(o=0>o?Math.ceil(o):Math.floor(o),0>o&&(o+=e);e>o;o++)if(o in this&&this[o]===t)return o;return-1}),Function.prototype.bind||(Function.prototype.bind=function(t){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var e=u.call(arguments,1),o=this,n=function(){},r=function(){return o.apply(this instanceof n&&t?this:t,e.concat(u.call(arguments)))};return n.prototype=this.prototype,r.prototype=new n,r}),angular.module("ngWebSocket",[]).factory("$websocket",["$rootScope","$q","$timeout","$websocketBackend",t]).factory("WebSocket",["$rootScope","$q","$timeout","WebsocketBackend",t]).service("$websocketBackend",["$window","$log",e]).service("WebSocketBackend",["$window","$log",e]),angular.module("angular-websocket",["ngWebSocket"])}(); | ||
!function(){"use strict";function t(t,e,o,u){function h(e,o,s){s||!c(o)||l(o)||(s=o,o=void 0),this.protocols=o,this.url=e||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=s&&s.scope||t,this.rootScopeFailover=s&&s.rootScopeFailover&&!0,this.useApplyAsync=s&&s.useApplyAsync||!1,this._reconnectAttempts=s&&s.reconnectAttempts||0,this.initialTimeout=s&&s.initialTimeout||500,this.maxTimeout=s&&s.maxTimeout||3e5,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],n(this._readyStateConstants),e?this._connect():this._setInternalState(0)}return h.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},h.prototype._reconnectableStatusCodes=[4e3],h.prototype.safeDigest=function(t){t&&!this.scope.$$phase&&this.scope.$digest()},h.prototype.bindToScope=function(e){var o=this;return e&&(this.scope=e,this.rootScopeFailover&&this.scope.$on("$destroy",function(){o.scope=t})),o},h.prototype._connect=function(t){(t||!this.socket||this.socket.readyState!==this._readyStateConstants.OPEN)&&(this.socket=u.create(this.url,this.protocols),this.socket.onmessage=this._onMessageHandler.bind(this),this.socket.onopen=this._onOpenHandler.bind(this),this.socket.onerror=this._onErrorHandler.bind(this),this.socket.onclose=this._onCloseHandler.bind(this))},h.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var t=this.sendQueue.shift();this.socket.send(r(t.message)?t.message:JSON.stringify(t.message)),t.deferred.resolve()}},h.prototype.notifyOpenCallbacks=function(t){for(var e=0;e<this.onOpenCallbacks.length;e++)this.onOpenCallbacks[e].call(this,t)},h.prototype.notifyCloseCallbacks=function(t){for(var e=0;e<this.onCloseCallbacks.length;e++)this.onCloseCallbacks[e].call(this,t)},h.prototype.notifyErrorCallbacks=function(t){for(var e=0;e<this.onErrorCallbacks.length;e++)this.onErrorCallbacks[e].call(this,t)},h.prototype.onOpen=function(t){return this.onOpenCallbacks.push(t),this},h.prototype.onClose=function(t){return this.onCloseCallbacks.push(t),this},h.prototype.onError=function(t){return this.onErrorCallbacks.push(t),this},h.prototype.onMessage=function(t,e){if(!i(t))throw new Error("Callback must be a function");if(e&&a(e.filter)&&!r(e.filter)&&!(e.filter instanceof RegExp))throw new Error("Pattern must be a string or regular expression");return this.onMessageCallbacks.push({fn:t,pattern:e?e.filter:void 0,autoApply:e?e.autoApply:!0}),this},h.prototype._onOpenHandler=function(t){this._reconnectAttempts=0,this.notifyOpenCallbacks(t),this.fireQueue()},h.prototype._onCloseHandler=function(t){this.notifyCloseCallbacks(t),this._reconnectableStatusCodes.indexOf(t.code)>-1&&this.reconnect()},h.prototype._onErrorHandler=function(t){this.notifyErrorCallbacks(t)},h.prototype._onMessageHandler=function(t){function e(t,e,o){o=p.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){t.apply(s,o)}):(t.apply(s,o),s.safeDigest(e))}for(var o,n,s=this,i=0;i<s.onMessageCallbacks.length;i++)n=s.onMessageCallbacks[i],o=n.pattern,o?r(o)&&t.data===o?e(n.fn,n.autoApply,t):o instanceof RegExp&&o.exec(t.data)&&e(n.fn,n.autoApply,t):e(n.fn,n.autoApply,t)},h.prototype.close=function(t){return(t||!this.socket.bufferedAmount)&&this.socket.close(),this},h.prototype.send=function(t){function o(t){t.cancel=n;var e=t.then;return t.then=function(){var t=e.apply(this,arguments);return o(t)},t}function n(e){return r.sendQueue.splice(r.sendQueue.indexOf(t),1),s.reject(e),r}var s=e.defer(),r=this,i=o(s.promise);return r.readyState===r._readyStateConstants.RECONNECT_ABORTED?s.reject("Socket connection has been closed"):(r.sendQueue.push({message:t,deferred:s}),r.fireQueue()),i},h.prototype.reconnect=function(){return this.close(),o(this._connect.bind(this),this._getBackoffDelay(++this._reconnectAttempts)),this},h.prototype._getBackoffDelay=function(t){var e=Math.random()+1,o=this.initialTimeout,n=2,s=t,r=this.maxTimeout;return Math.floor(Math.min(e*o*Math.pow(n,s),r))},h.prototype._setInternalState=function(t){if(Math.floor(t)!==t||0>t||t>4)throw new Error("state must be an integer between 0 and 4, got: "+t);s||(this.readyState=t||this.socket.readyState),this._internalConnectionState=t,angular.forEach(this.sendQueue,function(t){t.deferred.reject("Message cancelled due to closed socket connection")})},s&&s(h.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(t,e){return new h(t,e)}}function e(t,e){this.create=function(e,o){var n,s,r=/wss?:\/\//.exec(e);if(!r)throw new Error("Invalid url provided");if("object"==typeof exports&&require)try{s=require("ws"),n=s.Client||s.client||s}catch(i){}return n=n||t.WebSocket||t.MozWebSocket,o?new n(e,o):new n(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}var o=angular.noop,n=Object.freeze?Object.freeze:o,s=Object.defineProperty,r=angular.isString,i=angular.isFunction,a=angular.isDefined,c=angular.isObject,l=angular.isArray,p=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(t){var e=this.length>>>0,o=Number(arguments[1])||0;for(o=0>o?Math.ceil(o):Math.floor(o),0>o&&(o+=e);e>o;o++)if(o in this&&this[o]===t)return o;return-1}),Function.prototype.bind||(Function.prototype.bind=function(t){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var e=p.call(arguments,1),o=this,n=function(){},s=function(){return o.apply(this instanceof n&&t?this:t,e.concat(p.call(arguments)))};return n.prototype=this.prototype,s.prototype=new n,s}),angular.module("ngWebSocket",[]).factory("$websocket",["$rootScope","$q","$timeout","$websocketBackend",t]).factory("WebSocket",["$rootScope","$q","$timeout","WebsocketBackend",t]).service("$websocketBackend",["$window","$log",e]).service("WebSocketBackend",["$window","$log",e]),angular.module("angular-websocket",["ngWebSocket"])}(); | ||
//# sourceMappingURL=angular-websocket.min.js.map |
@@ -0,1 +1,12 @@ | ||
## 1.0.8 (2015-1-29) | ||
Features: | ||
- Allow .asyncApply() if useApplyAsync is true in config | ||
- Pass event to .notifyOpenCallbacks() callbacks | ||
- Tests for .bindToScope and .safeDigest | ||
Bugfixes: | ||
- Bind ._connect() correctly for .reconnect() | ||
- Correct rootScopeFailover of $rootScope with bindToScope | ||
## 1.0.7 (2015-1-20) | ||
@@ -2,0 +13,0 @@ |
@@ -14,3 +14,3 @@ (function() { | ||
function $MockWebSocket(url, protocols) { | ||
this.protocols = protocols || 'Sec-WebSocket-Protocol'; | ||
this.protocols = protocols; | ||
this.ssl = /(wss)/i.test(this.url); | ||
@@ -17,0 +17,0 @@ |
@@ -56,4 +56,2 @@ (function() { | ||
function $WebSocket(url, protocols, options) { | ||
// var bits = url.split('/'); | ||
if (!options && isObject(protocols) && !isArray(protocols)) { | ||
@@ -77,3 +75,3 @@ options = protocols; | ||
this.rootScopeFailover = options && options.rootScopeFailover && true; | ||
// this.useApplyAsync = options && options.useApplyAsync || false; | ||
this.useApplyAsync = options && options.useApplyAsync || false; | ||
this._reconnectAttempts = options && options.reconnectAttempts || 0; | ||
@@ -118,2 +116,3 @@ this.initialTimeout = options && options.initialTimeout || 500; // 500ms | ||
$WebSocket.prototype.bindToScope = function bindToScope(scope) { | ||
var self = this; | ||
if (scope) { | ||
@@ -123,7 +122,7 @@ this.scope = scope; | ||
this.scope.$on('$destroy', function() { | ||
this.scope = $rootScope; | ||
self.scope = $rootScope; | ||
}); | ||
} | ||
} | ||
return this; | ||
return self; | ||
}; | ||
@@ -134,4 +133,4 @@ | ||
this.socket = $websocketBackend.create(this.url, this.protocols); | ||
this.socket.onopen = this._onOpenHandler.bind(this); | ||
this.socket.onmessage = this._onMessageHandler.bind(this); | ||
this.socket.onopen = this._onOpenHandler.bind(this); | ||
this.socket.onerror = this._onErrorHandler.bind(this); | ||
@@ -153,5 +152,5 @@ this.socket.onclose = this._onCloseHandler.bind(this); | ||
$WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks() { | ||
$WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks(event) { | ||
for (var i = 0; i < this.onOpenCallbacks.length; i++) { | ||
this.onOpenCallbacks[i].call(this); | ||
this.onOpenCallbacks[i].call(this, event); | ||
} | ||
@@ -205,5 +204,5 @@ }; | ||
$WebSocket.prototype._onOpenHandler = function _onOpenHandler() { | ||
$WebSocket.prototype._onOpenHandler = function _onOpenHandler(event) { | ||
this._reconnectAttempts = 0; | ||
this.notifyOpenCallbacks(); | ||
this.notifyOpenCallbacks(event); | ||
this.fireQueue(); | ||
@@ -225,22 +224,32 @@ }; | ||
var pattern; | ||
var socketInstance = this; | ||
var self = this; | ||
var currentCallback; | ||
for (var i = 0; i < socketInstance.onMessageCallbacks.length; i++) { | ||
currentCallback = socketInstance.onMessageCallbacks[i]; | ||
for (var i = 0; i < self.onMessageCallbacks.length; i++) { | ||
currentCallback = self.onMessageCallbacks[i]; | ||
pattern = currentCallback.pattern; | ||
if (pattern) { | ||
if (isString(pattern) && message.data === pattern) { | ||
currentCallback.fn.call(socketInstance, message); | ||
socketInstance.safeDigest(currentCallback.autoApply); | ||
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); | ||
} | ||
else if (pattern instanceof RegExp && pattern.exec(message.data)) { | ||
currentCallback.fn.call(socketInstance, message); | ||
socketInstance.safeDigest(currentCallback.autoApply); | ||
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); | ||
} | ||
} | ||
else { | ||
currentCallback.fn.call(socketInstance, message); | ||
socketInstance.safeDigest(currentCallback.autoApply); | ||
applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message); | ||
} | ||
} | ||
function applyAsyncOrDigest(callback, autoApply, args) { | ||
args = arraySlice.call(arguments, 2); | ||
if (self.useApplyAsync) { | ||
self.scope.$applyAsync(function() { | ||
callback.apply(self, args); | ||
}); | ||
} else { | ||
callback.apply(self, args); | ||
self.safeDigest(autoApply); | ||
} | ||
} | ||
}; | ||
@@ -257,14 +266,14 @@ | ||
var deferred = $q.defer(); | ||
var socketInstance = this; | ||
var self = this; | ||
var promise = cancelableify(deferred.promise); | ||
if (socketInstance.readyState === socketInstance._readyStateConstants.RECONNECT_ABORTED) { | ||
if (self.readyState === self._readyStateConstants.RECONNECT_ABORTED) { | ||
deferred.reject('Socket connection has been closed'); | ||
} | ||
else { | ||
socketInstance.sendQueue.push({ | ||
self.sendQueue.push({ | ||
message: data, | ||
deferred: deferred | ||
}); | ||
socketInstance.fireQueue(); | ||
self.fireQueue(); | ||
} | ||
@@ -284,5 +293,5 @@ | ||
function cancel(reason) { | ||
socketInstance.sendQueue.splice(socketInstance.sendQueue.indexOf(data), 1); | ||
self.sendQueue.splice(self.sendQueue.indexOf(data), 1); | ||
deferred.reject(reason); | ||
return socketInstance; | ||
return self; | ||
} | ||
@@ -294,8 +303,7 @@ | ||
$WebSocket.prototype.reconnect = function reconnect() { | ||
var socketInstance = this; | ||
socketInstance.close(); | ||
this.close(); | ||
$timeout(socketInstance._connect, socketInstance._getBackoffDelay(++socketInstance._reconnectAttempts)); | ||
$timeout(this._connect.bind(this), this._getBackoffDelay(++this._reconnectAttempts)); | ||
return socketInstance; | ||
return this; | ||
}; | ||
@@ -348,3 +356,3 @@ // Exponential Backoff Formula by Prof. Douglas Thain | ||
// $WebSocketBackendProvider.$inject = ['$window']; | ||
// $WebSocketBackendProvider.$inject = ['$window', '$log']; | ||
function $WebSocketBackendProvider($window, $log) { | ||
@@ -351,0 +359,0 @@ this.create = function create(url, protocols) { |
@@ -1,2 +0,2 @@ | ||
!function(){"use strict";function t(t,e,o,u){function p(e,o,r){r||!c(o)||l(o)||(r=o,o=void 0),this.protocols=o,this.url=e||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=r&&r.scope||t,this.rootScopeFailover=r&&r.rootScopeFailover&&!0,this._reconnectAttempts=r&&r.reconnectAttempts||0,this.initialTimeout=r&&r.initialTimeout||500,this.maxTimeout=r&&r.maxTimeout||3e5,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],n(this._readyStateConstants),e?this._connect():this._setInternalState(0)}return p.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},p.prototype._reconnectableStatusCodes=[4e3],p.prototype.safeDigest=function(t){t&&!this.scope.$$phase&&this.scope.$digest()},p.prototype.bindToScope=function(e){return e&&(this.scope=e,this.rootScopeFailover&&this.scope.$on("$destroy",function(){this.scope=t})),this},p.prototype._connect=function(t){(t||!this.socket||this.socket.readyState!==this._readyStateConstants.OPEN)&&(this.socket=u.create(this.url,this.protocols),this.socket.onopen=this._onOpenHandler.bind(this),this.socket.onmessage=this._onMessageHandler.bind(this),this.socket.onerror=this._onErrorHandler.bind(this),this.socket.onclose=this._onCloseHandler.bind(this))},p.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var t=this.sendQueue.shift();this.socket.send(s(t.message)?t.message:JSON.stringify(t.message)),t.deferred.resolve()}},p.prototype.notifyOpenCallbacks=function(){for(var t=0;t<this.onOpenCallbacks.length;t++)this.onOpenCallbacks[t].call(this)},p.prototype.notifyCloseCallbacks=function(t){for(var e=0;e<this.onCloseCallbacks.length;e++)this.onCloseCallbacks[e].call(this,t)},p.prototype.notifyErrorCallbacks=function(t){for(var e=0;e<this.onErrorCallbacks.length;e++)this.onErrorCallbacks[e].call(this,t)},p.prototype.onOpen=function(t){return this.onOpenCallbacks.push(t),this},p.prototype.onClose=function(t){return this.onCloseCallbacks.push(t),this},p.prototype.onError=function(t){return this.onErrorCallbacks.push(t),this},p.prototype.onMessage=function(t,e){if(!i(t))throw new Error("Callback must be a function");if(e&&a(e.filter)&&!s(e.filter)&&!(e.filter instanceof RegExp))throw new Error("Pattern must be a string or regular expression");return this.onMessageCallbacks.push({fn:t,pattern:e?e.filter:void 0,autoApply:e?e.autoApply:!0}),this},p.prototype._onOpenHandler=function(){this._reconnectAttempts=0,this.notifyOpenCallbacks(),this.fireQueue()},p.prototype._onCloseHandler=function(t){this.notifyCloseCallbacks(t),this._reconnectableStatusCodes.indexOf(t.code)>-1&&this.reconnect()},p.prototype._onErrorHandler=function(t){this.notifyErrorCallbacks(t)},p.prototype._onMessageHandler=function(t){for(var e,o,n=this,r=0;r<n.onMessageCallbacks.length;r++)o=n.onMessageCallbacks[r],e=o.pattern,e?s(e)&&t.data===e?(o.fn.call(n,t),n.safeDigest(o.autoApply)):e instanceof RegExp&&e.exec(t.data)&&(o.fn.call(n,t),n.safeDigest(o.autoApply)):(o.fn.call(n,t),n.safeDigest(o.autoApply))},p.prototype.close=function(t){return(t||!this.socket.bufferedAmount)&&this.socket.close(),this},p.prototype.send=function(t){function o(t){t.cancel=n;var e=t.then;return t.then=function(){var t=e.apply(this,arguments);return o(t)},t}function n(e){return s.sendQueue.splice(s.sendQueue.indexOf(t),1),r.reject(e),s}var r=e.defer(),s=this,i=o(r.promise);return s.readyState===s._readyStateConstants.RECONNECT_ABORTED?r.reject("Socket connection has been closed"):(s.sendQueue.push({message:t,deferred:r}),s.fireQueue()),i},p.prototype.reconnect=function(){var t=this;return t.close(),o(t._connect,t._getBackoffDelay(++t._reconnectAttempts)),t},p.prototype._getBackoffDelay=function(t){var e=Math.random()+1,o=this.initialTimeout,n=2,r=t,s=this.maxTimeout;return Math.floor(Math.min(e*o*Math.pow(n,r),s))},p.prototype._setInternalState=function(t){if(Math.floor(t)!==t||0>t||t>4)throw new Error("state must be an integer between 0 and 4, got: "+t);r||(this.readyState=t||this.socket.readyState),this._internalConnectionState=t,angular.forEach(this.sendQueue,function(t){t.deferred.reject("Message cancelled due to closed socket connection")})},r&&r(p.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(t,e){return new p(t,e)}}function e(t,e){this.create=function(e,o){var n,r,s=/wss?:\/\//.exec(e);if(!s)throw new Error("Invalid url provided");if("object"==typeof exports&&require)try{r=require("ws"),n=r.Client||r.client||r}catch(i){}return n=n||t.WebSocket||t.MozWebSocket,o?new n(e,o):new n(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}var o=angular.noop,n=Object.freeze?Object.freeze:o,r=Object.defineProperty,s=angular.isString,i=angular.isFunction,a=angular.isDefined,c=angular.isObject,l=angular.isArray,u=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(t){var e=this.length>>>0,o=Number(arguments[1])||0;for(o=0>o?Math.ceil(o):Math.floor(o),0>o&&(o+=e);e>o;o++)if(o in this&&this[o]===t)return o;return-1}),Function.prototype.bind||(Function.prototype.bind=function(t){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var e=u.call(arguments,1),o=this,n=function(){},r=function(){return o.apply(this instanceof n&&t?this:t,e.concat(u.call(arguments)))};return n.prototype=this.prototype,r.prototype=new n,r}),angular.module("ngWebSocket",[]).factory("$websocket",["$rootScope","$q","$timeout","$websocketBackend",t]).factory("WebSocket",["$rootScope","$q","$timeout","WebsocketBackend",t]).service("$websocketBackend",["$window","$log",e]).service("WebSocketBackend",["$window","$log",e]),angular.module("angular-websocket",["ngWebSocket"])}(); | ||
!function(){"use strict";function t(t,e,o,u){function h(e,o,s){s||!c(o)||l(o)||(s=o,o=void 0),this.protocols=o,this.url=e||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=s&&s.scope||t,this.rootScopeFailover=s&&s.rootScopeFailover&&!0,this.useApplyAsync=s&&s.useApplyAsync||!1,this._reconnectAttempts=s&&s.reconnectAttempts||0,this.initialTimeout=s&&s.initialTimeout||500,this.maxTimeout=s&&s.maxTimeout||3e5,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],n(this._readyStateConstants),e?this._connect():this._setInternalState(0)}return h.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},h.prototype._reconnectableStatusCodes=[4e3],h.prototype.safeDigest=function(t){t&&!this.scope.$$phase&&this.scope.$digest()},h.prototype.bindToScope=function(e){var o=this;return e&&(this.scope=e,this.rootScopeFailover&&this.scope.$on("$destroy",function(){o.scope=t})),o},h.prototype._connect=function(t){(t||!this.socket||this.socket.readyState!==this._readyStateConstants.OPEN)&&(this.socket=u.create(this.url,this.protocols),this.socket.onmessage=this._onMessageHandler.bind(this),this.socket.onopen=this._onOpenHandler.bind(this),this.socket.onerror=this._onErrorHandler.bind(this),this.socket.onclose=this._onCloseHandler.bind(this))},h.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var t=this.sendQueue.shift();this.socket.send(r(t.message)?t.message:JSON.stringify(t.message)),t.deferred.resolve()}},h.prototype.notifyOpenCallbacks=function(t){for(var e=0;e<this.onOpenCallbacks.length;e++)this.onOpenCallbacks[e].call(this,t)},h.prototype.notifyCloseCallbacks=function(t){for(var e=0;e<this.onCloseCallbacks.length;e++)this.onCloseCallbacks[e].call(this,t)},h.prototype.notifyErrorCallbacks=function(t){for(var e=0;e<this.onErrorCallbacks.length;e++)this.onErrorCallbacks[e].call(this,t)},h.prototype.onOpen=function(t){return this.onOpenCallbacks.push(t),this},h.prototype.onClose=function(t){return this.onCloseCallbacks.push(t),this},h.prototype.onError=function(t){return this.onErrorCallbacks.push(t),this},h.prototype.onMessage=function(t,e){if(!i(t))throw new Error("Callback must be a function");if(e&&a(e.filter)&&!r(e.filter)&&!(e.filter instanceof RegExp))throw new Error("Pattern must be a string or regular expression");return this.onMessageCallbacks.push({fn:t,pattern:e?e.filter:void 0,autoApply:e?e.autoApply:!0}),this},h.prototype._onOpenHandler=function(t){this._reconnectAttempts=0,this.notifyOpenCallbacks(t),this.fireQueue()},h.prototype._onCloseHandler=function(t){this.notifyCloseCallbacks(t),this._reconnectableStatusCodes.indexOf(t.code)>-1&&this.reconnect()},h.prototype._onErrorHandler=function(t){this.notifyErrorCallbacks(t)},h.prototype._onMessageHandler=function(t){function e(t,e,o){o=p.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){t.apply(s,o)}):(t.apply(s,o),s.safeDigest(e))}for(var o,n,s=this,i=0;i<s.onMessageCallbacks.length;i++)n=s.onMessageCallbacks[i],o=n.pattern,o?r(o)&&t.data===o?e(n.fn,n.autoApply,t):o instanceof RegExp&&o.exec(t.data)&&e(n.fn,n.autoApply,t):e(n.fn,n.autoApply,t)},h.prototype.close=function(t){return(t||!this.socket.bufferedAmount)&&this.socket.close(),this},h.prototype.send=function(t){function o(t){t.cancel=n;var e=t.then;return t.then=function(){var t=e.apply(this,arguments);return o(t)},t}function n(e){return r.sendQueue.splice(r.sendQueue.indexOf(t),1),s.reject(e),r}var s=e.defer(),r=this,i=o(s.promise);return r.readyState===r._readyStateConstants.RECONNECT_ABORTED?s.reject("Socket connection has been closed"):(r.sendQueue.push({message:t,deferred:s}),r.fireQueue()),i},h.prototype.reconnect=function(){return this.close(),o(this._connect.bind(this),this._getBackoffDelay(++this._reconnectAttempts)),this},h.prototype._getBackoffDelay=function(t){var e=Math.random()+1,o=this.initialTimeout,n=2,s=t,r=this.maxTimeout;return Math.floor(Math.min(e*o*Math.pow(n,s),r))},h.prototype._setInternalState=function(t){if(Math.floor(t)!==t||0>t||t>4)throw new Error("state must be an integer between 0 and 4, got: "+t);s||(this.readyState=t||this.socket.readyState),this._internalConnectionState=t,angular.forEach(this.sendQueue,function(t){t.deferred.reject("Message cancelled due to closed socket connection")})},s&&s(h.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(t,e){return new h(t,e)}}function e(t,e){this.create=function(e,o){var n,s,r=/wss?:\/\//.exec(e);if(!r)throw new Error("Invalid url provided");if("object"==typeof exports&&require)try{s=require("ws"),n=s.Client||s.client||s}catch(i){}return n=n||t.WebSocket||t.MozWebSocket,o?new n(e,o):new n(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}var o=angular.noop,n=Object.freeze?Object.freeze:o,s=Object.defineProperty,r=angular.isString,i=angular.isFunction,a=angular.isDefined,c=angular.isObject,l=angular.isArray,p=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(t){var e=this.length>>>0,o=Number(arguments[1])||0;for(o=0>o?Math.ceil(o):Math.floor(o),0>o&&(o+=e);e>o;o++)if(o in this&&this[o]===t)return o;return-1}),Function.prototype.bind||(Function.prototype.bind=function(t){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var e=p.call(arguments,1),o=this,n=function(){},s=function(){return o.apply(this instanceof n&&t?this:t,e.concat(p.call(arguments)))};return n.prototype=this.prototype,s.prototype=new n,s}),angular.module("ngWebSocket",[]).factory("$websocket",["$rootScope","$q","$timeout","$websocketBackend",t]).factory("WebSocket",["$rootScope","$q","$timeout","WebsocketBackend",t]).service("$websocketBackend",["$window","$log",e]).service("WebSocketBackend",["$window","$log",e]),angular.module("angular-websocket",["ngWebSocket"])}(); | ||
//# sourceMappingURL=angular-websocket.min.js.map |
{ | ||
"name": "angular-websocket", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"main": "angular-websocket.min.js", | ||
@@ -5,0 +5,0 @@ "description": "An AngularJS 1.x WebSocket service for connecting client applications to servers.", |
@@ -44,3 +44,3 @@ # angular-websocket | ||
dataStream.onMessage(function(message.data) { | ||
dataStream.onMessage(function(message) { | ||
collection.push(JSON.parse(message.data)); | ||
@@ -152,2 +152,3 @@ }); | ||
* Allow more control over $digest cycle per WebSocket instance | ||
* Add Angular interceptors | ||
* Add .on(event) | ||
@@ -154,0 +155,0 @@ * Include more examples of patterns for realtime Angular apps |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71088
862
163