From e97847efdcc8ed0c3c2f442bcf9ccf516d3941ce Mon Sep 17 00:00:00 2001 From: Christian Jeschke Date: Tue, 5 Jul 2016 11:22:29 +0200 Subject: [PATCH 1/7] implemented mockClose and automatic listener notification for better testing --- dist/angular-websocket-mock.js | 55 ++++++++++++++++++++++++++----- dist/angular-websocket.js | 8 +---- dist/angular-websocket.min.js | 2 +- dist/angular-websocket.min.js.map | 2 +- src/angular-websocket-mock.js | 50 ++++++++++++++++++++++++---- test/angular-websocket.spec.js | 41 +++++++++++++++++++++-- 6 files changed, 130 insertions(+), 28 deletions(-) diff --git a/dist/angular-websocket-mock.js b/dist/angular-websocket-mock.js index 21bb08a..429a958 100644 --- a/dist/angular-websocket-mock.js +++ b/dist/angular-websocket-mock.js @@ -1,8 +1,8 @@ (function (global, factory) { if (typeof define === "function" && define.amd) { - define(['module', 'angular'], factory); + define(["module", "angular"], factory); } else if (typeof exports !== "undefined") { - factory(module, require('angular')); + factory(module, require("angular")); } else { var mod = { exports: {} @@ -11,7 +11,7 @@ global.angularWebsocketMock = mod.exports; } })(this, function (module, _angular) { - 'use strict'; + "use strict"; var _angular2 = _interopRequireDefault(_angular); @@ -29,10 +29,17 @@ var sendQueue = []; var pendingSends = []; var mock = false; + var existingMocks = {}; function $MockWebSocket(url, protocols) { + this.url = url; this.protocols = protocols; this.ssl = /(wss)/i.test(this.url); + if (!existingMocks[url]) { + existingMocks[url] = [this]; + } else { + existingMocks[url].push(this); + } } $MockWebSocket.prototype.send = function (msg) { @@ -45,6 +52,14 @@ } }; + this.mockClose = function (url, code) { + if (existingMocks[url]) { + existingMocks[url].map(function (mockSocket) { + mockSocket.close(code); + }); + } + }; + this.mock = function () { mock = true; }; @@ -57,8 +72,8 @@ return connectQueue.indexOf(url) > -1; }; - $MockWebSocket.prototype.close = function () { - pendingCloses.push(true); + $MockWebSocket.prototype.close = function (code) { + pendingCloses.push({ url: this.url, code: code ? code : 1000 }); }; function createWebSocketBackend(url, protocols) { @@ -76,19 +91,41 @@ this.create = createWebSocketBackend; this.createWebSocketBackend = createWebSocketBackend; + function callOpenCallbacks(url) { + existingMocks[url].map(function (socketMock) { + if (socketMock.onopen && typeof socketMock.onopen === "function") { + socketMock.onopen(); + } + }); + } + + function callCloseCallbacks(url, code) { + existingMocks[url].map(function (socketMock) { + if (socketMock.onclose && typeof socketMock.onclose === "function") { + socketMock.onclose({ code: code }); + } + }); + } + this.flush = function () { var url, msg, config; while (url = pendingConnects.shift()) { var i = connectQueue.indexOf(url); if (i > -1) { connectQueue.splice(i, 1); + callOpenCallbacks(url); } // if (config && config.url) { // } } - while (pendingCloses.shift()) { - closeQueue.shift(); + var pendingClose; + while (pendingClose = pendingCloses.shift()) { + var i = closeQueue.indexOf(pendingClose.url); + if (i > -1) { + closeQueue.splice(i, 1); + callCloseCallbacks(pendingClose.url, pendingClose.code); + } } while (msg = pendingSends.shift()) { @@ -110,8 +147,8 @@ // connectQueue.push({url: url, protocols: protocols}); }; - this.expectClose = function () { - closeQueue.push(true); + this.expectClose = function (url) { + closeQueue.push(url); }; this.expectSend = function (msg) { diff --git a/dist/angular-websocket.js b/dist/angular-websocket.js index db99c8e..f1a9afb 100644 --- a/dist/angular-websocket.js +++ b/dist/angular-websocket.js @@ -25,12 +25,6 @@ }; } - 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') { @@ -412,4 +406,4 @@ exports.default = _angular2.default.module('ngWebSocket'); module.exports = exports['default']; -}); +}); \ No newline at end of file diff --git a/dist/angular-websocket.min.js b/dist/angular-websocket.min.js index 0d82580..4f7ee88 100644 --- a/dist/angular-websocket.min.js +++ b/dist/angular-websocket.min.js @@ -1,2 +1,2 @@ -!function(e,t){if("function"==typeof define&&define.amd)define(["module","exports","angular","ws"],t);else if("undefined"!=typeof exports)t(module,exports,require("angular"),require("ws"));else{var o={exports:{}};t(o,o.exports,e.angular,e.ws),e.angularWebsocket=o.exports}}(this,function(e,t,o,n){"use strict";function s(e){return e&&e.__esModule?e:{"default":e}}function r(e,t,o,n){function s(t,o,n){n||!k(o)||C(o)||(n=o,o=void 0),this.protocols=o,this.url=t||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=n&&n.scope||e,this.rootScopeFailover=n&&n.rootScopeFailover&&!0,this.useApplyAsync=n&&n.useApplyAsync||!1,this.initialTimeout=n&&n.initialTimeout||500,this.maxTimeout=n&&n.maxTimeout||3e5,this.reconnectIfNotNormalClose=n&&n.reconnectIfNotNormalClose||!1,this.binaryType=n&&n.binaryType||"blob",this._reconnectAttempts=0,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],f(this._readyStateConstants),t?this._connect():this._setInternalState(0)}return s.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},s.prototype._normalCloseCode=1e3,s.prototype._reconnectableStatusCodes=[4e3],s.prototype.safeDigest=function(e){e&&!this.scope.$$phase&&this.scope.$digest()},s.prototype.bindToScope=function(t){var o=this;return t&&(this.scope=t,this.rootScopeFailover&&this.scope.$on("$destroy",function(){o.scope=e})),o},s.prototype._connect=function(e){!e&&this.socket&&this.socket.readyState===this._readyStateConstants.OPEN||(this.socket=n.create(this.url,this.protocols),this.socket.onmessage=c["default"].bind(this,this._onMessageHandler),this.socket.onopen=c["default"].bind(this,this._onOpenHandler),this.socket.onerror=c["default"].bind(this,this._onErrorHandler),this.socket.onclose=c["default"].bind(this,this._onCloseHandler),this.socket.binaryType=this.binaryType)},s.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var e=this.sendQueue.shift();this.socket.send(d(e.message)||"blob"!=this.binaryType?e.message:JSON.stringify(e.message)),e.deferred.resolve()}},s.prototype.notifyOpenCallbacks=function(e){for(var t=0;t-1)&&this.reconnect()},s.prototype._onErrorHandler=function(e){var t=this;t.useApplyAsync?t.scope.$applyAsync(function(){t.notifyErrorCallbacks(e)}):(t.notifyErrorCallbacks(e),t.safeDigest(!0))},s.prototype._onMessageHandler=function(e){function t(e,t,o){o=m.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){e.apply(s,o)}):(e.apply(s,o),s.safeDigest(t))}for(var o,n,s=this,r=0;re||e>4)throw new Error("state must be an integer between 0 and 4, got: "+e);h||(this.readyState=e||this.socket.readyState),this._internalConnectionState=e,g(this.sendQueue,function(e){e.deferred.reject("Message cancelled due to closed socket connection")})},h&&h(s.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(e,t,o){return new s(e,t,o)}}function i(e){this.create=function(e,t){var o=/wss?:\/\//.exec(e);if(!o)throw new Error("Invalid url provided");return t?new a(e,t):new a(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}Object.defineProperty(t,"__esModule",{value:!0});var a,c=s(o),l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e};if("object"===("undefined"==typeof t?"undefined":l(t))&&"function"==typeof require)try{a=n.Client||n.client||n}catch(u){}a=a||window.WebSocket||window.MozWebSocket;var p=c["default"].noop,f=Object.freeze?Object.freeze:p,h=Object.defineProperty,d=c["default"].isString,y=c["default"].isFunction,b=c["default"].isDefined,k=c["default"].isObject,C=c["default"].isArray,g=c["default"].forEach,m=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(e){var t=this.length>>>0,o=Number(arguments[1])||0;for(o=0>o?Math.ceil(o):Math.floor(o),0>o&&(o+=t);t>o;o++)if(o in this&&this[o]===e)return o;return-1}),c["default"].module("ngWebSocket",[]).factory("$websocket",["$rootScope","$q","$timeout","$websocketBackend",r]).factory("WebSocket",["$rootScope","$q","$timeout","WebsocketBackend",r]).service("$websocketBackend",["$log",i]).service("WebSocketBackend",["$log",i]),c["default"].module("angular-websocket",["ngWebSocket"]),t["default"]=c["default"].module("ngWebSocket"),e.exports=t["default"]}); +!function(e,t){if("function"==typeof define&&define.amd)define(["module","exports","angular","ws"],t);else if("undefined"!=typeof exports)t(module,exports,require("angular"),require("ws"));else{var o={exports:{}};t(o,o.exports,e.angular,e.ws),e.angularWebsocket=o.exports}}(this,function(e,t,o,n){"use strict";function s(e){return e&&e.__esModule?e:{default:e}}function r(e,t,o,n){function s(t,o,n){n||!y(o)||b(o)||(n=o,o=void 0),this.protocols=o,this.url=t||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=n&&n.scope||e,this.rootScopeFailover=n&&n.rootScopeFailover&&!0,this.useApplyAsync=n&&n.useApplyAsync||!1,this.initialTimeout=n&&n.initialTimeout||500,this.maxTimeout=n&&n.maxTimeout||3e5,this.reconnectIfNotNormalClose=n&&n.reconnectIfNotNormalClose||!1,this.binaryType=n&&n.binaryType||"blob",this._reconnectAttempts=0,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],u(this._readyStateConstants),t?this._connect():this._setInternalState(0)}return s.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},s.prototype._normalCloseCode=1e3,s.prototype._reconnectableStatusCodes=[4e3],s.prototype.safeDigest=function(e){e&&!this.scope.$$phase&&this.scope.$digest()},s.prototype.bindToScope=function(t){var o=this;return t&&(this.scope=t,this.rootScopeFailover&&this.scope.$on("$destroy",function(){o.scope=e})),o},s.prototype._connect=function(e){!e&&this.socket&&this.socket.readyState===this._readyStateConstants.OPEN||(this.socket=n.create(this.url,this.protocols),this.socket.onmessage=c.default.bind(this,this._onMessageHandler),this.socket.onopen=c.default.bind(this,this._onOpenHandler),this.socket.onerror=c.default.bind(this,this._onErrorHandler),this.socket.onclose=c.default.bind(this,this._onCloseHandler),this.socket.binaryType=this.binaryType)},s.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var e=this.sendQueue.shift();this.socket.send(f(e.message)||"blob"!=this.binaryType?e.message:JSON.stringify(e.message)),e.deferred.resolve()}},s.prototype.notifyOpenCallbacks=function(e){for(var t=0;t-1)&&this.reconnect()},s.prototype._onErrorHandler=function(e){var t=this;t.useApplyAsync?t.scope.$applyAsync(function(){t.notifyErrorCallbacks(e)}):(t.notifyErrorCallbacks(e),t.safeDigest(!0))},s.prototype._onMessageHandler=function(e){function t(e,t,o){o=C.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){e.apply(s,o)}):(e.apply(s,o),s.safeDigest(t))}for(var o,n,s=this,r=0;r4)throw new Error("state must be an integer between 0 and 4, got: "+e);p||(this.readyState=e||this.socket.readyState),this._internalConnectionState=e,k(this.sendQueue,function(e){e.deferred.reject("Message cancelled due to closed socket connection")})},p&&p(s.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(e,t,o){return new s(e,t,o)}}function i(e){this.create=function(e,t){var o=/wss?:\/\//.exec(e);if(!o)throw new Error("Invalid url provided");return t?new a(e,t):new a(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}Object.defineProperty(t,"__esModule",{value:!0});var a,c=s(o);if("undefined"==typeof window)try{a=n.Client||n.client||n}catch(e){}a=a||window.WebSocket||window.MozWebSocket;var l=c.default.noop,u=Object.freeze?Object.freeze:l,p=Object.defineProperty,f=c.default.isString,h=c.default.isFunction,d=c.default.isDefined,y=c.default.isObject,b=c.default.isArray,k=c.default.forEach,C=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(e){var t=this.length>>>0,o=Number(arguments[1])||0;for(o=o<0?Math.ceil(o):Math.floor(o),o<0&&(o+=t);o -1; }; - $MockWebSocket.prototype.close = function () { - pendingCloses.push(true); + $MockWebSocket.prototype.close = function (code) { + pendingCloses.push({url: this.url, code: code ? code : 1000}); }; function createWebSocketBackend(url, protocols) { @@ -57,19 +71,41 @@ function $WebSocketBackend() { this.create = createWebSocketBackend; this.createWebSocketBackend = createWebSocketBackend; + function callOpenCallbacks(url) { + existingMocks[url].map(function(socketMock) { + if(socketMock.onopen && typeof socketMock.onopen === "function") { + socketMock.onopen(); + } + }); + } + + function callCloseCallbacks(url, code) { + existingMocks[url].map(function(socketMock) { + if(socketMock.onclose && typeof socketMock.onclose === "function") { + socketMock.onclose({code: code}); + } + }); + } + this.flush = function () { var url, msg, config; while (url = pendingConnects.shift()) { var i = connectQueue.indexOf(url); if (i > -1) { connectQueue.splice(i, 1); + callOpenCallbacks(url); } // if (config && config.url) { // } } - while (pendingCloses.shift()) { - closeQueue.shift(); + var pendingClose; + while (pendingClose = pendingCloses.shift()) { + var i = closeQueue.indexOf(pendingClose.url); + if(i > -1) { + closeQueue.splice(i, 1); + callCloseCallbacks(pendingClose.url, pendingClose.code); + } } while (msg = pendingSends.shift()) { @@ -91,8 +127,8 @@ function $WebSocketBackend() { // connectQueue.push({url: url, protocols: protocols}); }; - this.expectClose = function () { - closeQueue.push(true); + this.expectClose = function (url) { + closeQueue.push(url); }; this.expectSend = function (msg) { diff --git a/test/angular-websocket.spec.js b/test/angular-websocket.spec.js index a5e4f4e..32c6b21 100644 --- a/test/angular-websocket.spec.js +++ b/test/angular-websocket.spec.js @@ -174,7 +174,7 @@ describe('angular-websocket', function() { }); it('should call close on the underlying socket', function() { - $websocketBackend.expectClose(); + $websocketBackend.expectClose(url); ws.close(); }); @@ -186,7 +186,7 @@ describe('angular-websocket', function() { it('should accept a force param to close the socket even if bufferedAmount is greater than 0', function() { - $websocketBackend.expectClose(); + $websocketBackend.expectClose(url); ws.socket.bufferedAmount = 5; ws.close(true); }); @@ -199,7 +199,7 @@ describe('angular-websocket', function() { $websocketBackend.expectConnect(url); var ws = $websocket(url); - $websocketBackend.expectClose(); + $websocketBackend.expectClose(url); ws.reconnect(); $websocketBackend.flush(); @@ -918,6 +918,41 @@ describe('angular-websocket', function() { }); // end ._onErrorHandler() + describe('eventHandlers', function() { + var url, ws; + beforeEach(function() { + url = 'ws://foo'; + $websocketBackend.expectConnect(url); + ws = $websocket(url); + }); + + it('should be automatically called on open', function() { + var spy = jasmine.createSpy('callback'); + ws.onOpenCallbacks.push(spy); + $websocketBackend.flush(); + expect(spy).toHaveBeenCalled(); + }); + + it('should be automatically called on close', function() { + var spy = jasmine.createSpy('callback'); + ws.onCloseCallbacks.push(spy); + $websocketBackend.expectClose(url); + ws.close(); + $websocketBackend.flush(); + expect(spy).toHaveBeenCalled(); + }); + + it("should be automatically called on mock close", function() { + var spy = jasmine.createSpy('callback'); + ws.onCloseCallbacks.push(spy); + $websocketBackend.expectClose(url); + $websocketBackend.mockClose(url); + $websocketBackend.flush(); + expect(spy).toHaveBeenCalled(); + }); + + //it(should be automatically called on error) + }); }); // end $websocketBackend }); // end $websocket From 43d5ed3980b07173160c2f0714e54ffce629a1a8 Mon Sep 17 00:00:00 2001 From: VonRehberg Date: Tue, 5 Jul 2016 11:27:18 +0200 Subject: [PATCH 2/7] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 73586d9..559c8be 100644 --- a/README.md +++ b/README.md @@ -137,8 +137,9 @@ name | arguments | description -------------------------------|------------|----------------------------------- flush | | Executes all pending requests expectConnect | url:String | Specify the url of an expected WebSocket connection -expectClose | | Expect "close" to be called on the WebSocket +expectClose | url:String | Expect "close" to be called on the WebSocket expectSend | msg:String | Expectation of send to be called, with required message +mockClose | url:String | Used to fake close a socket and test corresponding close handlers verifyNoOutstandingExpectation | | Makes sure all expectations have been satisfied, should be called in afterEach verifyNoOutstandingRequest | | Makes sure no requests are pending, should be called in afterEach From f432ac253d2b1eed9ef0b51d3de3967608dee1f1 Mon Sep 17 00:00:00 2001 From: Christian Jeschke Date: Tue, 5 Jul 2016 14:56:30 +0200 Subject: [PATCH 3/7] added fakeMessage method and rename mockClose to fakeClose to make intention more clear --- README.md | 3 ++- dist/angular-websocket-mock.js | 21 ++++++++++++++++++++- src/angular-websocket-mock.js | 21 ++++++++++++++++++++- test/angular-websocket.spec.js | 12 ++++++++++-- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 559c8be..4ea8bbe 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,8 @@ flush | | Executes all pending requests expectConnect | url:String | Specify the url of an expected WebSocket connection expectClose | url:String | Expect "close" to be called on the WebSocket expectSend | msg:String | Expectation of send to be called, with required message -mockClose | url:String | Used to fake close a socket and test corresponding close handlers +fakeClose | url:String | Used to fake close a socket and test corresponding close handlers +fakeMessage | url:String, data:object | Used to fake a backend initialized send action, which can be handled in onMessage verifyNoOutstandingExpectation | | Makes sure all expectations have been satisfied, should be called in afterEach verifyNoOutstandingRequest | | Makes sure no requests are pending, should be called in afterEach diff --git a/dist/angular-websocket-mock.js b/dist/angular-websocket-mock.js index 429a958..e025163 100644 --- a/dist/angular-websocket-mock.js +++ b/dist/angular-websocket-mock.js @@ -28,6 +28,7 @@ var pendingCloses = []; var sendQueue = []; var pendingSends = []; + var pendingMessages = []; var mock = false; var existingMocks = {}; @@ -52,7 +53,7 @@ } }; - this.mockClose = function (url, code) { + this.fakeClose = function (url, code) { if (existingMocks[url]) { existingMocks[url].map(function (mockSocket) { mockSocket.close(code); @@ -60,6 +61,10 @@ } }; + this.fakeMessage = function (url, data) { + pendingMessages.push({ url: url, data: data }); + }; + this.mock = function () { mock = true; }; @@ -107,6 +112,16 @@ }); } + function callMessageCallbacks(url, data) { + if (existingMocks[url]) { + existingMocks[url].map(function (socketMock) { + if (socketMock.onmessage && typeof socketMock.onmessage === "function") { + socketMock.onmessage(data); + } + }); + } + } + this.flush = function () { var url, msg, config; while (url = pendingConnects.shift()) { @@ -140,6 +155,10 @@ sendQueue.splice(j, 1); } } + + while (msg = pendingMessages.shift()) { + callMessageCallbacks(msg.url, msg.data); + } }; this.expectConnect = function (url, protocols) { diff --git a/src/angular-websocket-mock.js b/src/angular-websocket-mock.js index a715adb..f686de2 100644 --- a/src/angular-websocket-mock.js +++ b/src/angular-websocket-mock.js @@ -7,6 +7,7 @@ function $WebSocketBackend() { var pendingCloses = []; var sendQueue = []; var pendingSends = []; + var pendingMessages = []; var mock = false; var existingMocks = {}; @@ -32,7 +33,7 @@ function $WebSocketBackend() { } }; - this.mockClose = function(url, code) { + this.fakeClose = function(url, code) { if(existingMocks[url]) { existingMocks[url].map(function(mockSocket) { mockSocket.close(code); @@ -40,6 +41,10 @@ function $WebSocketBackend() { } }; + this.fakeMessage = function(url, data) { + pendingMessages.push({url: url, data: data}); + }; + this.mock = function() { mock = true; }; @@ -87,6 +92,16 @@ function $WebSocketBackend() { }); } + function callMessageCallbacks(url, data) { + if(existingMocks[url]) { + existingMocks[url].map(function(socketMock) { + if(socketMock.onmessage && typeof socketMock.onmessage === "function") { + socketMock.onmessage(data); + } + }); + } + } + this.flush = function () { var url, msg, config; while (url = pendingConnects.shift()) { @@ -120,6 +135,10 @@ function $WebSocketBackend() { sendQueue.splice(j, 1); } } + + while (msg = pendingMessages.shift()) { + callMessageCallbacks(msg.url, msg.data); + } }; this.expectConnect = function (url, protocols) { diff --git a/test/angular-websocket.spec.js b/test/angular-websocket.spec.js index 32c6b21..713e65b 100644 --- a/test/angular-websocket.spec.js +++ b/test/angular-websocket.spec.js @@ -943,11 +943,19 @@ describe('angular-websocket', function() { expect(spy).toHaveBeenCalled(); }); - it("should be automatically called on mock close", function() { + it("should be automatically called on fake close", function() { var spy = jasmine.createSpy('callback'); ws.onCloseCallbacks.push(spy); $websocketBackend.expectClose(url); - $websocketBackend.mockClose(url); + $websocketBackend.fakeClose(url); + $websocketBackend.flush(); + expect(spy).toHaveBeenCalled(); + }); + + it("should be automatically called on fake send", function() { + var spy = jasmine.createSpy('callback'); + ws.onMessage(spy); + $websocketBackend.fakeMessage(url, {data: "test"}); $websocketBackend.flush(); expect(spy).toHaveBeenCalled(); }); From 690d078d69541296f89fae5266384d6bb371c2f2 Mon Sep 17 00:00:00 2001 From: Christian Jeschke Date: Wed, 6 Jul 2016 13:23:32 +0200 Subject: [PATCH 4/7] getting closer to reality - data is received as string in data property --- dist/angular-websocket-mock.js | 2 +- src/angular-websocket-mock.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/angular-websocket-mock.js b/dist/angular-websocket-mock.js index e025163..baf6808 100644 --- a/dist/angular-websocket-mock.js +++ b/dist/angular-websocket-mock.js @@ -116,7 +116,7 @@ if (existingMocks[url]) { existingMocks[url].map(function (socketMock) { if (socketMock.onmessage && typeof socketMock.onmessage === "function") { - socketMock.onmessage(data); + socketMock.onmessage({ data: JSON.stringify(data) }); } }); } diff --git a/src/angular-websocket-mock.js b/src/angular-websocket-mock.js index f686de2..89ca255 100644 --- a/src/angular-websocket-mock.js +++ b/src/angular-websocket-mock.js @@ -96,7 +96,7 @@ function $WebSocketBackend() { if(existingMocks[url]) { existingMocks[url].map(function(socketMock) { if(socketMock.onmessage && typeof socketMock.onmessage === "function") { - socketMock.onmessage(data); + socketMock.onmessage({data: JSON.stringify(data)}); } }); } From 794964c24a140ed800dae9a4b93324cae3459ce0 Mon Sep 17 00:00:00 2001 From: Christian Jeschke Date: Wed, 6 Jul 2016 16:14:08 +0200 Subject: [PATCH 5/7] removed test mock code from production code by implementing readyState in socketBackendMock --- dist/angular-websocket-mock.js | 10 ++++++++++ dist/angular-websocket.js | 4 ---- dist/angular-websocket.min.js | 2 +- dist/angular-websocket.min.js.map | 2 +- src/angular-websocket-mock.js | 10 ++++++++++ src/angular-websocket.js | 5 ----- test/angular-websocket.spec.js | 14 ++++++++++++-- 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/dist/angular-websocket-mock.js b/dist/angular-websocket-mock.js index baf6808..0887832 100644 --- a/dist/angular-websocket-mock.js +++ b/dist/angular-websocket-mock.js @@ -122,6 +122,14 @@ } } + function setReadyState(url, state) { + if (existingMocks[url]) { + existingMocks[url].map(function (socketMock) { + socketMock.readyState = state; + }); + } + } + this.flush = function () { var url, msg, config; while (url = pendingConnects.shift()) { @@ -129,6 +137,7 @@ if (i > -1) { connectQueue.splice(i, 1); callOpenCallbacks(url); + setReadyState(url, 1); } // if (config && config.url) { // } @@ -140,6 +149,7 @@ if (i > -1) { closeQueue.splice(i, 1); callCloseCallbacks(pendingClose.url, pendingClose.code); + setReadyState(pendingClose.url, 3); } } diff --git a/dist/angular-websocket.js b/dist/angular-websocket.js index f1a9afb..50faaac 100644 --- a/dist/angular-websocket.js +++ b/dist/angular-websocket.js @@ -314,10 +314,6 @@ return self; } - if ($websocketBackend.isMocked && $websocketBackend.isMocked() && $websocketBackend.isConnected(this.url)) { - this._onMessageHandler($websocketBackend.mockSend()); - } - return promise; }; diff --git a/dist/angular-websocket.min.js b/dist/angular-websocket.min.js index 4f7ee88..1092648 100644 --- a/dist/angular-websocket.min.js +++ b/dist/angular-websocket.min.js @@ -1,2 +1,2 @@ -!function(e,t){if("function"==typeof define&&define.amd)define(["module","exports","angular","ws"],t);else if("undefined"!=typeof exports)t(module,exports,require("angular"),require("ws"));else{var o={exports:{}};t(o,o.exports,e.angular,e.ws),e.angularWebsocket=o.exports}}(this,function(e,t,o,n){"use strict";function s(e){return e&&e.__esModule?e:{default:e}}function r(e,t,o,n){function s(t,o,n){n||!y(o)||b(o)||(n=o,o=void 0),this.protocols=o,this.url=t||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=n&&n.scope||e,this.rootScopeFailover=n&&n.rootScopeFailover&&!0,this.useApplyAsync=n&&n.useApplyAsync||!1,this.initialTimeout=n&&n.initialTimeout||500,this.maxTimeout=n&&n.maxTimeout||3e5,this.reconnectIfNotNormalClose=n&&n.reconnectIfNotNormalClose||!1,this.binaryType=n&&n.binaryType||"blob",this._reconnectAttempts=0,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],u(this._readyStateConstants),t?this._connect():this._setInternalState(0)}return s.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},s.prototype._normalCloseCode=1e3,s.prototype._reconnectableStatusCodes=[4e3],s.prototype.safeDigest=function(e){e&&!this.scope.$$phase&&this.scope.$digest()},s.prototype.bindToScope=function(t){var o=this;return t&&(this.scope=t,this.rootScopeFailover&&this.scope.$on("$destroy",function(){o.scope=e})),o},s.prototype._connect=function(e){!e&&this.socket&&this.socket.readyState===this._readyStateConstants.OPEN||(this.socket=n.create(this.url,this.protocols),this.socket.onmessage=c.default.bind(this,this._onMessageHandler),this.socket.onopen=c.default.bind(this,this._onOpenHandler),this.socket.onerror=c.default.bind(this,this._onErrorHandler),this.socket.onclose=c.default.bind(this,this._onCloseHandler),this.socket.binaryType=this.binaryType)},s.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var e=this.sendQueue.shift();this.socket.send(f(e.message)||"blob"!=this.binaryType?e.message:JSON.stringify(e.message)),e.deferred.resolve()}},s.prototype.notifyOpenCallbacks=function(e){for(var t=0;t-1)&&this.reconnect()},s.prototype._onErrorHandler=function(e){var t=this;t.useApplyAsync?t.scope.$applyAsync(function(){t.notifyErrorCallbacks(e)}):(t.notifyErrorCallbacks(e),t.safeDigest(!0))},s.prototype._onMessageHandler=function(e){function t(e,t,o){o=C.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){e.apply(s,o)}):(e.apply(s,o),s.safeDigest(t))}for(var o,n,s=this,r=0;r4)throw new Error("state must be an integer between 0 and 4, got: "+e);p||(this.readyState=e||this.socket.readyState),this._internalConnectionState=e,k(this.sendQueue,function(e){e.deferred.reject("Message cancelled due to closed socket connection")})},p&&p(s.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(e,t,o){return new s(e,t,o)}}function i(e){this.create=function(e,t){var o=/wss?:\/\//.exec(e);if(!o)throw new Error("Invalid url provided");return t?new a(e,t):new a(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}Object.defineProperty(t,"__esModule",{value:!0});var a,c=s(o);if("undefined"==typeof window)try{a=n.Client||n.client||n}catch(e){}a=a||window.WebSocket||window.MozWebSocket;var l=c.default.noop,u=Object.freeze?Object.freeze:l,p=Object.defineProperty,f=c.default.isString,h=c.default.isFunction,d=c.default.isDefined,y=c.default.isObject,b=c.default.isArray,k=c.default.forEach,C=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(e){var t=this.length>>>0,o=Number(arguments[1])||0;for(o=o<0?Math.ceil(o):Math.floor(o),o<0&&(o+=t);o-1)&&this.reconnect()},s.prototype._onErrorHandler=function(e){var t=this;t.useApplyAsync?t.scope.$applyAsync(function(){t.notifyErrorCallbacks(e)}):(t.notifyErrorCallbacks(e),t.safeDigest(!0))},s.prototype._onMessageHandler=function(e){function t(e,t,o){o=C.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){e.apply(s,o)}):(e.apply(s,o),s.safeDigest(t))}for(var o,n,s=this,r=0;r4)throw new Error("state must be an integer between 0 and 4, got: "+e);p||(this.readyState=e||this.socket.readyState),this._internalConnectionState=e,k(this.sendQueue,function(e){e.deferred.reject("Message cancelled due to closed socket connection")})},p&&p(s.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(e,t,o){return new s(e,t,o)}}function i(e){this.create=function(e,t){var o=/wss?:\/\//.exec(e);if(!o)throw new Error("Invalid url provided");return t?new a(e,t):new a(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}Object.defineProperty(t,"__esModule",{value:!0});var a,c=s(o);if("undefined"==typeof window)try{a=n.Client||n.client||n}catch(e){}a=a||window.WebSocket||window.MozWebSocket;var l=c.default.noop,u=Object.freeze?Object.freeze:l,p=Object.defineProperty,f=c.default.isString,h=c.default.isFunction,d=c.default.isDefined,y=c.default.isObject,b=c.default.isArray,k=c.default.forEach,C=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(e){var t=this.length>>>0,o=Number(arguments[1])||0;for(o=o<0?Math.ceil(o):Math.floor(o),o<0&&(o+=t);o -1) { connectQueue.splice(i, 1); callOpenCallbacks(url); + setReadyState(url, 1); } // if (config && config.url) { // } @@ -120,6 +129,7 @@ function $WebSocketBackend() { if(i > -1) { closeQueue.splice(i, 1); callCloseCallbacks(pendingClose.url, pendingClose.code); + setReadyState(pendingClose.url, 3); } } diff --git a/src/angular-websocket.js b/src/angular-websocket.js index 3f4e919..58d92dc 100644 --- a/src/angular-websocket.js +++ b/src/angular-websocket.js @@ -299,11 +299,6 @@ function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) { return self; } - if ($websocketBackend.isMocked && $websocketBackend.isMocked() && - $websocketBackend.isConnected(this.url)) { - this._onMessageHandler($websocketBackend.mockSend()); - } - return promise; }; diff --git a/test/angular-websocket.spec.js b/test/angular-websocket.spec.js index 713e65b..ebb6674 100644 --- a/test/angular-websocket.spec.js +++ b/test/angular-websocket.spec.js @@ -154,6 +154,11 @@ describe('angular-websocket', function() { expect(typeof ws.socket.onerror).toBe('function'); expect(typeof ws.socket.onclose).toBe('function'); }); + + it('should set readyState', function() { + $websocketBackend.flush(); + expect(ws.readyState).toEqual(ws._readyStateConstants.OPEN); + }); }); @@ -190,6 +195,13 @@ describe('angular-websocket', function() { ws.socket.bufferedAmount = 5; ws.close(true); }); + + it('should set readyState', function() { + $websocketBackend.expectClose(url); + ws.close(); + $websocketBackend.flush(); + expect(ws.readyState).toEqual(ws._readyStateConstants.CLOSED); + }); }); @@ -959,8 +971,6 @@ describe('angular-websocket', function() { $websocketBackend.flush(); expect(spy).toHaveBeenCalled(); }); - - //it(should be automatically called on error) }); }); // end $websocketBackend }); // end $websocket From 3a191cfb875cd090820ba869acff784017f58bf9 Mon Sep 17 00:00:00 2001 From: Christian Jeschke Date: Mon, 11 Jul 2016 09:03:27 +0200 Subject: [PATCH 6/7] added some samples on how to write unit tests + fixed minor ugly API handling --- README.md | 2 +- dist/angular-websocket-mock.js | 1 + example/howToTest/controller.js | 25 +++++++++++ example/howToTest/controller.spec.js | 57 ++++++++++++++++++++++++ example/howToTest/karma.conf.js | 65 ++++++++++++++++++++++++++++ example/howToTest/package.json | 6 +++ karma.conf.js | 4 +- src/angular-websocket-mock.js | 1 + test/angular-websocket.spec.js | 1 - 9 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 example/howToTest/controller.js create mode 100644 example/howToTest/controller.spec.js create mode 100644 example/howToTest/karma.conf.js create mode 100644 example/howToTest/package.json diff --git a/README.md b/README.md index 4ea8bbe..8fbfdd4 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ server in order to test your applications: ```javascript var $websocketBackend; - beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock'); + beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock')); beforeEach(inject(function (_$websocketBackend_) { $websocketBackend = _$websocketBackend_; diff --git a/dist/angular-websocket-mock.js b/dist/angular-websocket-mock.js index 0887832..222c002 100644 --- a/dist/angular-websocket-mock.js +++ b/dist/angular-websocket-mock.js @@ -54,6 +54,7 @@ }; this.fakeClose = function (url, code) { + closeQueue.push(url); if (existingMocks[url]) { existingMocks[url].map(function (mockSocket) { mockSocket.close(code); diff --git a/example/howToTest/controller.js b/example/howToTest/controller.js new file mode 100644 index 0000000..47390b4 --- /dev/null +++ b/example/howToTest/controller.js @@ -0,0 +1,25 @@ +angular.module("appToTest", ['ngWebSocket']).controller("ControllerToTest", ["$scope", "$websocket", function($scope, $websocket) { + var socket; + $scope.isConnected = false; + + function initializeSocket() { + socket.onOpen(function() { + $scope.isConnected = true; + }); + socket.onClose(function() { + $scope.isConnected = false; + }); + socket.onMessage(function(message) { + $scope.lastReceivedMessage = JSON.parse(message.data); + }); + } + + $scope.connect = function() { + socket = $websocket("wss://foo"); + initializeSocket(); + }; + + $scope.sendMessage = function(sMessage) { + socket.send(sMessage); + }; +}]); diff --git a/example/howToTest/controller.spec.js b/example/howToTest/controller.spec.js new file mode 100644 index 0000000..4003387 --- /dev/null +++ b/example/howToTest/controller.spec.js @@ -0,0 +1,57 @@ +describe("ControllerToTest", function() { + var controller, $scope, $websocketBackend; + + beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock')); + + beforeEach(function() { + module("appToTest"); + inject(function(_$websocketBackend_, _$controller_) { + $websocketBackend = _$websocketBackend_; + $scope = {}; + controller = _$controller_('ControllerToTest', { $scope: $scope }); + }); + + $websocketBackend.expectConnect("wss://foo"); + }); + + afterEach(function() { + $websocketBackend.verifyNoOutstandingRequest(); + $websocketBackend.verifyNoOutstandingExpectation(); + }); + + function connect() { + $scope.connect(); + $websocketBackend.flush(); + } + + it("should initiate a connection on connect", function() { + connect(); + }); + + it("should react on an established connection", function() { + expect($scope.isConnected).toEqual(false); + connect(); + expect($scope.isConnected).toEqual(true); + }); + + it("should react on a closing connection", function() { + connect(); + $websocketBackend.fakeClose("wss://foo"); + $websocketBackend.flush(); + expect($scope.isConnected).toEqual(false); + }); + + it("should react on an incoming message", function() { + connect(); + $websocketBackend.fakeMessage("wss://foo", "hello"); + $websocketBackend.flush(); + expect($scope.lastReceivedMessage).toEqual("hello"); + }); + + it("should send a message to the socket", function() { + connect(); + $websocketBackend.expectSend("wss://foo", "greetings"); + $scope.sendMessage("greetings"); + $websocketBackend.flush(); + }); +}); \ No newline at end of file diff --git a/example/howToTest/karma.conf.js b/example/howToTest/karma.conf.js new file mode 100644 index 0000000..7fc7f6a --- /dev/null +++ b/example/howToTest/karma.conf.js @@ -0,0 +1,65 @@ +// Karma configuration +// Generated on Thu Dec 25 2014 14:12:24 GMT-0800 (PST) + +module.exports = function(config) { + + var configuration = { + + // base path, that will be used to resolve files and exclude + basePath: '', + + // frameworks to use + frameworks: ['jasmine'], + + // list of files / patterns to load in the browser + files: [ + '../../bower_components/angular/angular.js', + '../../bower_components/angular-mocks/angular-mocks.js', + '../../dist/angular-websocket.js', + '../../dist/angular-websocket-mock.js', + './controller.js', + './controller.spec.js' + ], + + // list of files to exclude + exclude: [ + + ], + + // test results reporter to use + // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' + reporters: ['progress'], + + // web server port + port: 9876, + + // enable / disable colors in the output (reporters and logs) + colors: true, + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_WARN, + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: false, + + // Start these browsers, currently available: + // - Chrome + // - ChromeCanary + // - Firefox + // - Opera (has to be installed with `npm install karma-opera-launcher`) + // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`) + // - PhantomJS + // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`) + browsers: ['PhantomJS'], + + // If browser does not capture in given timeout [ms], kill it + captureTimeout: 60000, + + // Continuous Integration mode + // if true, it capture browsers, run tests and exit + singleRun: true + }; + + config.set(configuration); +}; diff --git a/example/howToTest/package.json b/example/howToTest/package.json new file mode 100644 index 0000000..0e0458b --- /dev/null +++ b/example/howToTest/package.json @@ -0,0 +1,6 @@ +{ + "name": "howToTest", + "scripts": { + "test": "karma start karma.conf.js" + } +} diff --git a/karma.conf.js b/karma.conf.js index 2ff1c92..0d58c1c 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -17,7 +17,9 @@ module.exports = function(config) { 'bower_components/angular-mocks/angular-mocks.js', 'dist/angular-websocket.js', 'dist/angular-websocket-mock.js', - 'test/angular-websocket.spec.js' + 'test/angular-websocket.spec.js', + 'example/howToTest/controller.js', + 'example/howToTest/controller.spec.js', ], // list of files to exclude diff --git a/src/angular-websocket-mock.js b/src/angular-websocket-mock.js index 28a5166..04416e1 100644 --- a/src/angular-websocket-mock.js +++ b/src/angular-websocket-mock.js @@ -34,6 +34,7 @@ function $WebSocketBackend() { }; this.fakeClose = function(url, code) { + closeQueue.push(url); if(existingMocks[url]) { existingMocks[url].map(function(mockSocket) { mockSocket.close(code); diff --git a/test/angular-websocket.spec.js b/test/angular-websocket.spec.js index ebb6674..d6c8527 100644 --- a/test/angular-websocket.spec.js +++ b/test/angular-websocket.spec.js @@ -958,7 +958,6 @@ describe('angular-websocket', function() { it("should be automatically called on fake close", function() { var spy = jasmine.createSpy('callback'); ws.onCloseCallbacks.push(spy); - $websocketBackend.expectClose(url); $websocketBackend.fakeClose(url); $websocketBackend.flush(); expect(spy).toHaveBeenCalled(); From 71bbfd8e8a71f38f78150704c777dbe413d1b03b Mon Sep 17 00:00:00 2001 From: Christian Jeschke Date: Fri, 29 Jul 2016 10:01:26 +0200 Subject: [PATCH 7/7] allowing garbage collector to clean WebSocket instances this should avoid memory leaks if the webserver is not available added a test on reconnects to the howToTest package --- dist/angular-websocket.js | 7 +++++++ dist/angular-websocket.min.js | 2 +- dist/angular-websocket.min.js.map | 2 +- example/howToTest/controller.spec.js | 16 ++++++++++++++-- src/angular-websocket-mock.js | 1 - src/angular-websocket.js | 8 +++++++- 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/dist/angular-websocket.js b/dist/angular-websocket.js index 50faaac..54ab2cf 100644 --- a/dist/angular-websocket.js +++ b/dist/angular-websocket.js @@ -142,6 +142,13 @@ }; $WebSocket.prototype._connect = function _connect(force) { + if (this.socket) { + this.socket.onmessage = null; + this.socket.onopen = null; + this.socket.onerror = null; + this.socket.onclose = null; + this.socket.close(); + } if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) { this.socket = $websocketBackend.create(this.url, this.protocols); this.socket.onmessage = _angular2.default.bind(this, this._onMessageHandler); diff --git a/dist/angular-websocket.min.js b/dist/angular-websocket.min.js index 1092648..d8a7f43 100644 --- a/dist/angular-websocket.min.js +++ b/dist/angular-websocket.min.js @@ -1,2 +1,2 @@ -!function(e,t){if("function"==typeof define&&define.amd)define(["module","exports","angular","ws"],t);else if("undefined"!=typeof exports)t(module,exports,require("angular"),require("ws"));else{var o={exports:{}};t(o,o.exports,e.angular,e.ws),e.angularWebsocket=o.exports}}(this,function(e,t,o,n){"use strict";function s(e){return e&&e.__esModule?e:{default:e}}function r(e,t,o,n){function s(t,o,n){n||!y(o)||b(o)||(n=o,o=void 0),this.protocols=o,this.url=t||"Missing URL",this.ssl=/(wss)/i.test(this.url),this.scope=n&&n.scope||e,this.rootScopeFailover=n&&n.rootScopeFailover&&!0,this.useApplyAsync=n&&n.useApplyAsync||!1,this.initialTimeout=n&&n.initialTimeout||500,this.maxTimeout=n&&n.maxTimeout||3e5,this.reconnectIfNotNormalClose=n&&n.reconnectIfNotNormalClose||!1,this.binaryType=n&&n.binaryType||"blob",this._reconnectAttempts=0,this.sendQueue=[],this.onOpenCallbacks=[],this.onMessageCallbacks=[],this.onErrorCallbacks=[],this.onCloseCallbacks=[],u(this._readyStateConstants),t?this._connect():this._setInternalState(0)}return s.prototype._readyStateConstants={CONNECTING:0,OPEN:1,CLOSING:2,CLOSED:3,RECONNECT_ABORTED:4},s.prototype._normalCloseCode=1e3,s.prototype._reconnectableStatusCodes=[4e3],s.prototype.safeDigest=function(e){e&&!this.scope.$$phase&&this.scope.$digest()},s.prototype.bindToScope=function(t){var o=this;return t&&(this.scope=t,this.rootScopeFailover&&this.scope.$on("$destroy",function(){o.scope=e})),o},s.prototype._connect=function(e){!e&&this.socket&&this.socket.readyState===this._readyStateConstants.OPEN||(this.socket=n.create(this.url,this.protocols),this.socket.onmessage=c.default.bind(this,this._onMessageHandler),this.socket.onopen=c.default.bind(this,this._onOpenHandler),this.socket.onerror=c.default.bind(this,this._onErrorHandler),this.socket.onclose=c.default.bind(this,this._onCloseHandler),this.socket.binaryType=this.binaryType)},s.prototype.fireQueue=function(){for(;this.sendQueue.length&&this.socket.readyState===this._readyStateConstants.OPEN;){var e=this.sendQueue.shift();this.socket.send(f(e.message)||"blob"!=this.binaryType?e.message:JSON.stringify(e.message)),e.deferred.resolve()}},s.prototype.notifyOpenCallbacks=function(e){for(var t=0;t-1)&&this.reconnect()},s.prototype._onErrorHandler=function(e){var t=this;t.useApplyAsync?t.scope.$applyAsync(function(){t.notifyErrorCallbacks(e)}):(t.notifyErrorCallbacks(e),t.safeDigest(!0))},s.prototype._onMessageHandler=function(e){function t(e,t,o){o=C.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){e.apply(s,o)}):(e.apply(s,o),s.safeDigest(t))}for(var o,n,s=this,r=0;r4)throw new Error("state must be an integer between 0 and 4, got: "+e);p||(this.readyState=e||this.socket.readyState),this._internalConnectionState=e,k(this.sendQueue,function(e){e.deferred.reject("Message cancelled due to closed socket connection")})},p&&p(s.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(e,t,o){return new s(e,t,o)}}function i(e){this.create=function(e,t){var o=/wss?:\/\//.exec(e);if(!o)throw new Error("Invalid url provided");return t?new a(e,t):new a(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}Object.defineProperty(t,"__esModule",{value:!0});var a,c=s(o);if("undefined"==typeof window)try{a=n.Client||n.client||n}catch(e){}a=a||window.WebSocket||window.MozWebSocket;var l=c.default.noop,u=Object.freeze?Object.freeze:l,p=Object.defineProperty,f=c.default.isString,h=c.default.isFunction,d=c.default.isDefined,y=c.default.isObject,b=c.default.isArray,k=c.default.forEach,C=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(e){var t=this.length>>>0,o=Number(arguments[1])||0;for(o=o<0?Math.ceil(o):Math.floor(o),o<0&&(o+=t);o-1)&&this.reconnect()},s.prototype._onErrorHandler=function(e){var t=this;t.useApplyAsync?t.scope.$applyAsync(function(){t.notifyErrorCallbacks(e)}):(t.notifyErrorCallbacks(e),t.safeDigest(!0))},s.prototype._onMessageHandler=function(e){function t(e,t,o){o=C.call(arguments,2),s.useApplyAsync?s.scope.$applyAsync(function(){e.apply(s,o)}):(e.apply(s,o),s.safeDigest(t))}for(var o,n,s=this,r=0;r4)throw new Error("state must be an integer between 0 and 4, got: "+e);p||(this.readyState=e||this.socket.readyState),this._internalConnectionState=e,k(this.sendQueue,function(e){e.deferred.reject("Message cancelled due to closed socket connection")})},p&&p(s.prototype,"readyState",{get:function(){return this._internalConnectionState||this.socket.readyState},set:function(){throw new Error("The readyState property is read-only")}}),function(e,t,o){return new s(e,t,o)}}function i(e){this.create=function(e,t){var o=/wss?:\/\//.exec(e);if(!o)throw new Error("Invalid url provided");return t?new a(e,t):new a(e)},this.createWebSocketBackend=function(t,o){return e.warn("Deprecated: Please use .create(url, protocols)"),this.create(t,o)}}Object.defineProperty(t,"__esModule",{value:!0});var a,c=s(o);if("undefined"==typeof window)try{a=n.Client||n.client||n}catch(e){}a=a||window.WebSocket||window.MozWebSocket;var l=c.default.noop,u=Object.freeze?Object.freeze:l,p=Object.defineProperty,h=c.default.isString,f=c.default.isFunction,d=c.default.isDefined,y=c.default.isObject,b=c.default.isArray,k=c.default.forEach,C=Array.prototype.slice;Array.prototype.indexOf||(Array.prototype.indexOf=function(e){var t=this.length>>>0,o=Number(arguments[1])||0;for(o=o<0?Math.ceil(o):Math.floor(o),o<0&&(o+=t);o