angular-websocket.js 12 KB

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