angular-websocket.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. (function() {
  2. 'use strict';
  3. var noop = angular.noop;
  4. var objectFreeze = (Object.freeze) ? Object.freeze : noop;
  5. var objectDefineProperty = Object.defineProperty;
  6. var isString = angular.isString;
  7. var isFunction = angular.isFunction;
  8. var isDefined = angular.isDefined;
  9. var isObject = angular.isObject;
  10. var isArray = angular.isArray;
  11. var forEach = angular.forEach;
  12. var arraySlice = Array.prototype.slice;
  13. // ie8 wat
  14. if (!Array.prototype.indexOf) {
  15. Array.prototype.indexOf = function(elt /*, from*/) {
  16. var len = this.length >>> 0;
  17. var from = Number(arguments[1]) || 0;
  18. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  19. if (from < 0) {
  20. from += len;
  21. }
  22. for (; from < len; from++) {
  23. if (from in this && this[from] === elt) { return from; }
  24. }
  25. return -1;
  26. };
  27. }
  28. // $WebSocketProvider.$inject = ['$rootScope', '$q', '$timeout', '$websocketBackend'];
  29. function $WebSocketProvider($rootScope, $q, $timeout, $websocketBackend) {
  30. function $WebSocket(url, protocols, options) {
  31. if (!options && isObject(protocols) && !isArray(protocols)) {
  32. options = protocols;
  33. protocols = undefined;
  34. }
  35. this.protocols = protocols;
  36. this.url = url || 'Missing URL';
  37. this.ssl = /(wss)/i.test(this.url);
  38. // this.binaryType = '';
  39. // this.extensions = '';
  40. // this.bufferedAmount = 0;
  41. // this.trasnmitting = false;
  42. // this.buffer = [];
  43. // TODO: refactor options to use isDefined
  44. this.scope = options && options.scope || $rootScope;
  45. this.rootScopeFailover = options && options.rootScopeFailover && true;
  46. this.useApplyAsync = options && options.useApplyAsync || false;
  47. this.initialTimeout = options && options.initialTimeout || 500; // 500ms
  48. this.maxTimeout = options && options.maxTimeout || 5 * 60 * 1000; // 5 minutes
  49. this.reconnectIfNotNormalClose = options && options.reconnectIfNotNormalClose || false;
  50. this.binaryType = options && options.binaryType || 'blob';
  51. this._reconnectAttempts = 0;
  52. this.sendQueue = [];
  53. this.onOpenCallbacks = [];
  54. this.onMessageCallbacks = [];
  55. this.onErrorCallbacks = [];
  56. this.onCloseCallbacks = [];
  57. objectFreeze(this._readyStateConstants);
  58. if (url) {
  59. this._connect();
  60. } else {
  61. this._setInternalState(0);
  62. }
  63. }
  64. $WebSocket.prototype._readyStateConstants = {
  65. 'CONNECTING': 0,
  66. 'OPEN': 1,
  67. 'CLOSING': 2,
  68. 'CLOSED': 3,
  69. 'RECONNECT_ABORTED': 4
  70. };
  71. $WebSocket.prototype._normalCloseCode = 1000;
  72. $WebSocket.prototype._reconnectableStatusCodes = [
  73. 4000
  74. ];
  75. $WebSocket.prototype.safeDigest = function safeDigest(autoApply) {
  76. if (autoApply && !this.scope.$$phase) {
  77. this.scope.$digest();
  78. }
  79. };
  80. $WebSocket.prototype.bindToScope = function bindToScope(scope) {
  81. var self = this;
  82. if (scope) {
  83. this.scope = scope;
  84. if (this.rootScopeFailover) {
  85. this.scope.$on('$destroy', function() {
  86. self.scope = $rootScope;
  87. });
  88. }
  89. }
  90. return self;
  91. };
  92. $WebSocket.prototype._connect = function _connect(force) {
  93. if (force || !this.socket || this.socket.readyState !== this._readyStateConstants.OPEN) {
  94. this.socket = $websocketBackend.create(this.url, this.protocols);
  95. this.socket.onmessage = angular.bind(this, this._onMessageHandler);
  96. this.socket.onopen = angular.bind(this, this._onOpenHandler);
  97. this.socket.onerror = angular.bind(this, this._onErrorHandler);
  98. this.socket.onclose = angular.bind(this, this._onCloseHandler);
  99. this.socket.binaryType = this.binaryType;
  100. }
  101. };
  102. $WebSocket.prototype.fireQueue = function fireQueue() {
  103. while (this.sendQueue.length && this.socket.readyState === this._readyStateConstants.OPEN) {
  104. var data = this.sendQueue.shift();
  105. this.socket.send(
  106. isString(data.message) || this.binaryType != "blob" ? data.message : JSON.stringify(data.message)
  107. );
  108. data.deferred.resolve();
  109. }
  110. };
  111. $WebSocket.prototype.notifyOpenCallbacks = function notifyOpenCallbacks(event) {
  112. for (var i = 0; i < this.onOpenCallbacks.length; i++) {
  113. this.onOpenCallbacks[i].call(this, event);
  114. }
  115. };
  116. $WebSocket.prototype.notifyCloseCallbacks = function notifyCloseCallbacks(event) {
  117. for (var i = 0; i < this.onCloseCallbacks.length; i++) {
  118. this.onCloseCallbacks[i].call(this, event);
  119. }
  120. };
  121. $WebSocket.prototype.notifyErrorCallbacks = function notifyErrorCallbacks(event) {
  122. for (var i = 0; i < this.onErrorCallbacks.length; i++) {
  123. this.onErrorCallbacks[i].call(this, event);
  124. }
  125. };
  126. $WebSocket.prototype.onOpen = function onOpen(cb) {
  127. this.onOpenCallbacks.push(cb);
  128. return this;
  129. };
  130. $WebSocket.prototype.onClose = function onClose(cb) {
  131. this.onCloseCallbacks.push(cb);
  132. return this;
  133. };
  134. $WebSocket.prototype.onError = function onError(cb) {
  135. this.onErrorCallbacks.push(cb);
  136. return this;
  137. };
  138. $WebSocket.prototype.onMessage = function onMessage(callback, options) {
  139. if (!isFunction(callback)) {
  140. throw new Error('Callback must be a function');
  141. }
  142. if (options && isDefined(options.filter) && !isString(options.filter) && !(options.filter instanceof RegExp)) {
  143. throw new Error('Pattern must be a string or regular expression');
  144. }
  145. this.onMessageCallbacks.push({
  146. fn: callback,
  147. pattern: options ? options.filter : undefined,
  148. autoApply: options ? options.autoApply : true
  149. });
  150. return this;
  151. };
  152. $WebSocket.prototype._onOpenHandler = function _onOpenHandler(event) {
  153. this._reconnectAttempts = 0;
  154. this.notifyOpenCallbacks(event);
  155. this.fireQueue();
  156. };
  157. $WebSocket.prototype._onCloseHandler = function _onCloseHandler(event) {
  158. var self = this;
  159. if (self.useApplyAsync) {
  160. self.scope.$applyAsync(function() {
  161. self.notifyCloseCallbacks(event);
  162. });
  163. } else {
  164. self.notifyCloseCallbacks(event);
  165. self.safeDigest(autoApply);
  166. }
  167. if ((this.reconnectIfNotNormalClose && event.code !== this._normalCloseCode) || this._reconnectableStatusCodes.indexOf(event.code) > -1) {
  168. this.reconnect();
  169. }
  170. };
  171. $WebSocket.prototype._onErrorHandler = function _onErrorHandler(event) {
  172. var self = this;
  173. if (self.useApplyAsync) {
  174. self.scope.$applyAsync(function() {
  175. self.notifyErrorCallbacks(event);
  176. });
  177. } else {
  178. self.notifyErrorCallbacks(event);
  179. self.safeDigest(autoApply);
  180. }
  181. };
  182. $WebSocket.prototype._onMessageHandler = function _onMessageHandler(message) {
  183. var pattern;
  184. var self = this;
  185. var currentCallback;
  186. for (var i = 0; i < self.onMessageCallbacks.length; i++) {
  187. currentCallback = self.onMessageCallbacks[i];
  188. pattern = currentCallback.pattern;
  189. if (pattern) {
  190. if (isString(pattern) && message.data === pattern) {
  191. applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
  192. }
  193. else if (pattern instanceof RegExp && pattern.exec(message.data)) {
  194. applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
  195. }
  196. }
  197. else {
  198. applyAsyncOrDigest(currentCallback.fn, currentCallback.autoApply, message);
  199. }
  200. }
  201. function applyAsyncOrDigest(callback, autoApply, args) {
  202. args = arraySlice.call(arguments, 2);
  203. if (self.useApplyAsync) {
  204. self.scope.$applyAsync(function() {
  205. callback.apply(self, args);
  206. });
  207. } else {
  208. callback.apply(self, args);
  209. self.safeDigest(autoApply);
  210. }
  211. }
  212. };
  213. $WebSocket.prototype.close = function close(force) {
  214. if (force || !this.socket.bufferedAmount) {
  215. this.socket.close();
  216. }
  217. return this;
  218. };
  219. $WebSocket.prototype.send = function send(data) {
  220. var deferred = $q.defer();
  221. var self = this;
  222. var promise = cancelableify(deferred.promise);
  223. if (self.readyState === self._readyStateConstants.RECONNECT_ABORTED) {
  224. deferred.reject('Socket connection has been closed');
  225. }
  226. else {
  227. self.sendQueue.push({
  228. message: data,
  229. deferred: deferred
  230. });
  231. self.fireQueue();
  232. }
  233. // Credit goes to @btford
  234. function cancelableify(promise) {
  235. promise.cancel = cancel;
  236. var then = promise.then;
  237. promise.then = function() {
  238. var newPromise = then.apply(this, arguments);
  239. return cancelableify(newPromise);
  240. };
  241. return promise;
  242. }
  243. function cancel(reason) {
  244. self.sendQueue.splice(self.sendQueue.indexOf(data), 1);
  245. deferred.reject(reason);
  246. return self;
  247. }
  248. if ($websocketBackend.isMocked && $websocketBackend.isMocked() &&
  249. $websocketBackend.isConnected(this.url)) {
  250. this._onMessageHandler($websocketBackend.mockSend());
  251. }
  252. return promise;
  253. };
  254. $WebSocket.prototype.reconnect = function reconnect() {
  255. this.close();
  256. var backoffDelay = this._getBackoffDelay(++this._reconnectAttempts);
  257. var backoffDelaySeconds = backoffDelay / 1000;
  258. console.log('Reconnecting in ' + backoffDelaySeconds + ' seconds');
  259. $timeout(angular.bind(this, this._connect), backoffDelay);
  260. return this;
  261. };
  262. // Exponential Backoff Formula by Prof. Douglas Thain
  263. // http://dthain.blogspot.co.uk/2009/02/exponential-backoff-in-distributed.html
  264. $WebSocket.prototype._getBackoffDelay = function _getBackoffDelay(attempt) {
  265. var R = Math.random() + 1;
  266. var T = this.initialTimeout;
  267. var F = 2;
  268. var N = attempt;
  269. var M = this.maxTimeout;
  270. return Math.floor(Math.min(R * T * Math.pow(F, N), M));
  271. };
  272. $WebSocket.prototype._setInternalState = function _setInternalState(state) {
  273. if (Math.floor(state) !== state || state < 0 || state > 4) {
  274. throw new Error('state must be an integer between 0 and 4, got: ' + state);
  275. }
  276. // ie8 wat
  277. if (!objectDefineProperty) {
  278. this.readyState = state || this.socket.readyState;
  279. }
  280. this._internalConnectionState = state;
  281. forEach(this.sendQueue, function(pending) {
  282. pending.deferred.reject('Message cancelled due to closed socket connection');
  283. });
  284. };
  285. // Read only .readyState
  286. if (objectDefineProperty) {
  287. objectDefineProperty($WebSocket.prototype, 'readyState', {
  288. get: function() {
  289. return this._internalConnectionState || this.socket.readyState;
  290. },
  291. set: function() {
  292. throw new Error('The readyState property is read-only');
  293. }
  294. });
  295. }
  296. return function(url, protocols, options) {
  297. return new $WebSocket(url, protocols, options);
  298. };
  299. }
  300. // $WebSocketBackendProvider.$inject = ['$window', '$log'];
  301. function $WebSocketBackendProvider($window, $log) {
  302. this.create = function create(url, protocols) {
  303. var match = /wss?:\/\//.exec(url);
  304. var Socket, ws;
  305. if (!match) {
  306. throw new Error('Invalid url provided');
  307. }
  308. // CommonJS
  309. if (typeof exports === 'object' && require) {
  310. try {
  311. ws = require('ws');
  312. Socket = (ws.Client || ws.client || ws);
  313. } catch(e) {}
  314. }
  315. // Browser
  316. Socket = Socket || $window.WebSocket || $window.MozWebSocket;
  317. if (protocols) {
  318. return new Socket(url, protocols);
  319. }
  320. return new Socket(url);
  321. };
  322. this.createWebSocketBackend = function createWebSocketBackend(url, protocols) {
  323. $log.warn('Deprecated: Please use .create(url, protocols)');
  324. return this.create(url, protocols);
  325. };
  326. }
  327. angular.module('ngWebSocket', [])
  328. .factory('$websocket', ['$rootScope', '$q', '$timeout', '$websocketBackend', $WebSocketProvider])
  329. .factory('WebSocket', ['$rootScope', '$q', '$timeout', 'WebsocketBackend', $WebSocketProvider])
  330. .service('$websocketBackend', ['$window', '$log', $WebSocketBackendProvider])
  331. .service('WebSocketBackend', ['$window', '$log', $WebSocketBackendProvider]);
  332. angular.module('angular-websocket', ['ngWebSocket']);
  333. if (typeof module === 'object' && typeof define !== 'function') {
  334. module.exports = angular.module('ngWebSocket');
  335. }
  336. }());