From: Darren Date: Fri, 30 Aug 2013 11:08:06 +0000 (+0100) Subject: Engine.io.tools file + ignoring built bundle X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=06e30f061b99687f78e47c7c5a0ec70cc55a8952;p=KiwiIRC.git Engine.io.tools file + ignoring built bundle --- diff --git a/.gitignore b/.gitignore index a32777d..16f2aee 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ node/node_modules/ node_modules/ doc/ +client/assets/libs/engine.io.bundle.* client/assets/kiwi.js client/assets/kiwi.min.js client/assets/locales/*.json diff --git a/client/assets/libs/engine.io.tools.js b/client/assets/libs/engine.io.tools.js new file mode 100644 index 0000000..47e1a15 --- /dev/null +++ b/client/assets/libs/engine.io.tools.js @@ -0,0 +1,266 @@ +var EngineioTools = { + ReconnectingSocket: function ReconnectingSocket(server_uri, socket_options) { + var connected = false; + var is_reconnecting = false; + + var reconnect_delay = 2000; + var reconnect_last_delay = 0; + var reconnect_delay_exponential = true; + var reconnect_max_attempts = 5; + var reconnect_step = 0; + var reconnect_tmr = null; + + var original_disconnect; + var planned_disconnect = false; + + var socket = eio.apply(eio, arguments); + socket.on('open', onOpen); + socket.on('close', onClose); + socket.on('error', onError); + + original_disconnect = socket.close; + socket.close = close; + + // Apply any custom reconnection config + if (socket_options) { + if (typeof socket_options.reconnect_delay === 'number') + reconnect_delay = socket_options.reconnect_delay; + + if (typeof socket_options.reconnect_max_attempts === 'number') + reconnect_max_attempts = socket_options.reconnect_max_attempts; + + if (typeof socket_options.reconnect_delay_exponential !== 'undefined') + reconnect_delay_exponential = !!socket_options.reconnect_delay_exponential; + } + + + function onOpen() { + connected = true; + is_reconnecting = false; + planned_disconnect = false; + + reconnect_step = 0; + reconnect_last_delay = 0; + + clearTimeout(reconnect_tmr); + } + + + function onClose() { + connected = false; + + if (!planned_disconnect) + reconnect(); + } + + + function onError() { + // This will be called when a reconnect fails + if (is_reconnecting) + reconnect(); + } + + + function close() { + planned_disconnect = true; + original_disconnect.call(socket); + } + + + function reconnect() { + if (reconnect_step >= reconnect_max_attempts) { + socket.emit('reconnecting_failed'); + return; + } + + var delay = reconnect_delay_exponential ? + (reconnect_last_delay || reconnect_delay / 2) * 2 : + reconnect_delay * reconnect_step; + + is_reconnecting = true; + + reconnect_tmr = setTimeout(function() { + socket.open(); + }, delay); + + reconnect_last_delay = delay; + + socket.emit('reconnecting', { + attempt: reconnect_step + 1, + max_attempts: reconnect_max_attempts, + delay: delay + }); + + reconnect_step++; + } + + return socket; + }, + + + + + Rpc: (function(){ + /* + Create a document explaining the protocol + */ + + function WebsocketRpc(eio_socket) { + var self = this; + + this._next_id = 0; + this._callbacks = {}; + this._socket = eio_socket; + + this._mixinEmitter(); + this._bindSocketListeners(); + } + + + WebsocketRpc.prototype._bindSocketListeners = function() { + var self = this; + + // Proxy the onMessage listener + this._onMessageProxy = function rpcOnMessageBoundFunction(){ + self._onMessage.apply(self, arguments); + }; + this._socket.on('message', this._onMessageProxy); + }; + + + + WebsocketRpc.prototype.dispose = function() { + if (this._onMessageProxy) { + this._socket.removeListener('message', this._onMessageProxy); + delete this._onMessageProxy; + } + + this.removeAllListeners(); + }; + + + + + /** + * The engine.io socket already has an emitter mixin so steal it from there + */ + WebsocketRpc.prototype._mixinEmitter = function() { + var funcs = ['on', 'once', 'off', 'removeListener', 'removeAllListeners', 'emit', 'listeners', 'hasListeners']; + + for (var i=0; i