1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 9× 1× 208× 31× 6× 1× 6× 1× 5× 5× 1× 1× 16× 10× 11× 11× 16× 32× 1× 10× 10× 10× 10× 10× 10× 10× 10× 10× 1× 9× 9× 63× 21× 9× 1× 8× 5× 5× 6× 5× 8× 6× 6× 6× 3× 3× 3× 6× 3× 8× 16× 16× 16× 16× 16× 16× 256× 208× 16× 1× 1× 1× 1× 1× 16× 16× 16× 16× 16× 6× 6× 10× 5× 5× 10× 10× 8× 16× 8× 8× 8× 2× 2× 8× 12× 3× 4× 4× 1× 9× 12× 8× 5× 4× 3× 3× 5× 1× | "use strict"; var getDefaultOptions = function () { return ({ constructor: (typeof WebSocket === 'function') ? WebSocket : null, maxReconnectionDelay: 10000, minReconnectionDelay: 1500, reconnectionDelayGrowFactor: 1.3, connectionTimeout: 4000, maxRetries: Infinity, debug: false, }); }; var bypassProperty = function (src, dst, name) { Object.defineProperty(dst, name, { get: function () { return src[name]; }, set: function (value) { src[name] = value; }, enumerable: true, configurable: true, }); }; var initReconnectionDelay = function (config) { return (config.minReconnectionDelay + Math.random() * config.minReconnectionDelay); }; var updateReconnectionDelay = function (config, previousDelay) { var newDelay = previousDelay * config.reconnectionDelayGrowFactor; return (newDelay > config.maxReconnectionDelay) ? config.maxReconnectionDelay : newDelay; }; var LEVEL_0_EVENTS = ['onopen', 'onclose', 'onmessage', 'onerror']; var reassignEventListeners = function (ws, oldWs, listeners) { Object.keys(listeners).forEach(function (type) { listeners[type].forEach(function (_a) { var listener = _a[0], options = _a[1]; ws.addEventListener(type, listener, options); }); }); if (oldWs) { LEVEL_0_EVENTS.forEach(function (name) { ws[name] = oldWs[name]; }); } }; var ReconnectingWebsocket = function (url, protocols, options) { var _this = this; if (options === void 0) { options = {}; } var ws; var connectingTimeout; var reconnectDelay = 0; var retriesCount = 0; var shouldRetry = true; var listeners = {}; // require new to construct if (!(this instanceof ReconnectingWebsocket)) { throw new TypeError("Failed to construct 'ReconnectingWebSocket': Please use the 'new' operator"); } // Set config. Not using `Object.assign` because of IE11 var config = getDefaultOptions(); Object.keys(config) .filter(function (key) { return options.hasOwnProperty(key); }) .forEach(function (key) { return config[key] = options[key]; }); if (typeof config.constructor !== 'function') { throw new TypeError('WebSocket constructor not set. Set `options.constructor`'); } var log = config.debug ? function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i - 0] = arguments[_i]; } return console.log.apply(console, ['RWS:'].concat(params)); } : function () { }; /** * Not using dispatchEvent, otherwise we must use a DOM Event object * Deferred because we want to handle the close event before this */ var emitError = function (code, msg) { return setTimeout(function () { var err = new Error(msg); err.code = code; if (Array.isArray(listeners.error)) { listeners.error.forEach(function (_a) { var fn = _a[0]; return fn(err); }); } if (ws.onerror) { ws.onerror(err); } }, 0); }; var connect = function () { log('connect'); var oldWs = ws; ws = new config.constructor(url, protocols); connectingTimeout = setTimeout(function () { log('timeout'); ws.close(); emitError('ETIMEDOUT', 'Connection timeout'); }, config.connectionTimeout); log('bypass properties'); for (var key in ws) { // @todo move to constant if (['addEventListener', 'removeEventListener', 'close'].indexOf(key) < 0) { bypassProperty(ws, _this, key); } } ws.addEventListener('open', function () { clearTimeout(connectingTimeout); log('open'); reconnectDelay = initReconnectionDelay(config); log('reconnectDelay:', reconnectDelay); retriesCount = 0; }); ws.addEventListener('close', function () { log('close'); retriesCount++; log('retries count:', retriesCount); if (retriesCount > config.maxRetries) { emitError('EHOSTDOWN', 'Too many failed connection attempts'); return; } if (!reconnectDelay) { reconnectDelay = initReconnectionDelay(config); } else { reconnectDelay = updateReconnectionDelay(config, reconnectDelay); } log('reconnectDelay:', reconnectDelay); if (shouldRetry) { setTimeout(connect, reconnectDelay); } }); reassignEventListeners(ws, oldWs, listeners); }; log('init'); connect(); this.close = function () { shouldRetry = false; ws.close(); }; this.addEventListener = function (type, listener, options) { if (Array.isArray(listeners[type])) { if (!listeners[type].some(function (_a) { var l = _a[0]; return l === listener; })) { listeners[type].push([listener, options]); } } else { listeners[type] = [[listener, options]]; } ws.addEventListener(type, listener, options); }; this.removeEventListener = function (type, listener, options) { if (Array.isArray(listeners[type])) { listeners[type] = listeners[type].filter(function (_a) { var l = _a[0]; return l !== listener; }); } ws.removeEventListener(type, listener, options); }; }; module.exports = ReconnectingWebsocket; |