Comran Morshed | c4ce951 | 2015-03-08 11:51:09 +0000 | [diff] [blame^] | 1 | // MIT License: |
| 2 | // |
| 3 | // Copyright (c) 2010-2012, Joe Walnes |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | // of this software and associated documentation files (the "Software"), to deal |
| 7 | // in the Software without restriction, including without limitation the rights |
| 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | // copies of the Software, and to permit persons to whom the Software is |
| 10 | // furnished to do so, subject to the following conditions: |
| 11 | // |
| 12 | // The above copyright notice and this permission notice shall be included in |
| 13 | // all copies or substantial portions of the Software. |
| 14 | // |
| 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | // THE SOFTWARE. |
| 22 | |
| 23 | /** |
| 24 | * This behaves like a WebSocket in every way, except if it fails to connect, |
| 25 | * or it gets disconnected, it will repeatedly poll until it successfully connects |
| 26 | * again. |
| 27 | * |
| 28 | * It is API compatible, so when you have: |
| 29 | * ws = new WebSocket('ws://....'); |
| 30 | * you can replace with: |
| 31 | * ws = new ReconnectingWebSocket('ws://....'); |
| 32 | * |
| 33 | * The event stream will typically look like: |
| 34 | * onconnecting |
| 35 | * onopen |
| 36 | * onmessage |
| 37 | * onmessage |
| 38 | * onclose // lost connection |
| 39 | * onconnecting |
| 40 | * onopen // sometime later... |
| 41 | * onmessage |
| 42 | * onmessage |
| 43 | * etc... |
| 44 | * |
| 45 | * It is API compatible with the standard WebSocket API, apart from the following members: |
| 46 | * |
| 47 | * - `bufferedAmount` |
| 48 | * - `extensions` |
| 49 | * - `binaryType` |
| 50 | * |
| 51 | * Latest version: https://github.com/joewalnes/reconnecting-websocket/ |
| 52 | * - Joe Walnes |
| 53 | * |
| 54 | * Syntax |
| 55 | * ====== |
| 56 | * var socket = new ReconnectingWebSocket(url, protocols, options); |
| 57 | * |
| 58 | * Parameters |
| 59 | * ========== |
| 60 | * url - The url you are connecting to. |
| 61 | * protocols - Optional string or array of protocols. |
| 62 | * options - See below |
| 63 | * |
| 64 | * Options |
| 65 | * ======= |
| 66 | * Options can either be passed upon instantiation or set after instantiation: |
| 67 | * |
| 68 | * var socket = new ReconnectingWebSocket(url, null, { debug: true, reconnectInterval: 4000 }); |
| 69 | * |
| 70 | * or |
| 71 | * |
| 72 | * var socket = new ReconnectingWebSocket(url); |
| 73 | * socket.debug = true; |
| 74 | * socket.reconnectInterval = 4000; |
| 75 | * |
| 76 | * debug |
| 77 | * - Whether this instance should log debug messages. Accepts true or false. Default: false. |
| 78 | * |
| 79 | * automaticOpen |
| 80 | * - Whether or not the websocket should attempt to connect immediately upon instantiation. The socket can be manually opened or closed at any time using ws.open() and ws.close(). |
| 81 | * |
| 82 | * reconnectInterval |
| 83 | * - The number of milliseconds to delay before attempting to reconnect. Accepts integer. Default: 1000. |
| 84 | * |
| 85 | * maxReconnectInterval |
| 86 | * - The maximum number of milliseconds to delay a reconnection attempt. Accepts integer. Default: 30000. |
| 87 | * |
| 88 | * reconnectDecay |
| 89 | * - The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. Accepts integer or float. Default: 1.5. |
| 90 | * |
| 91 | * timeoutInterval |
| 92 | * - The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. Accepts integer. Default: 2000. |
| 93 | * |
| 94 | */ |
| 95 | |
| 96 | !function(a,b){"function"==typeof define&&define.amd?define([],b):"undefined"!=typeof module&&module.exports?module.exports=b():a.ReconnectingWebSocket=b()}(this,function(){function a(b,c,d){function l(a,b){var c=document.createEvent("CustomEvent");return c.initCustomEvent(a,!1,!1,b),c}var e={debug:!1,automaticOpen:!0,reconnectInterval:1e3,maxReconnectInterval:3e4,reconnectDecay:1.5,timeoutInterval:2e3};d||(d={});for(var f in e)this[f]="undefined"!=typeof d[f]?d[f]:e[f];this.url=b,this.reconnectAttempts=0,this.readyState=WebSocket.CONNECTING,this.protocol=null;var h,g=this,i=!1,j=!1,k=document.createElement("div");k.addEventListener("open",function(a){g.onopen(a)}),k.addEventListener("close",function(a){g.onclose(a)}),k.addEventListener("connecting",function(a){g.onconnecting(a)}),k.addEventListener("message",function(a){g.onmessage(a)}),k.addEventListener("error",function(a){g.onerror(a)}),this.addEventListener=k.addEventListener.bind(k),this.removeEventListener=k.removeEventListener.bind(k),this.dispatchEvent=k.dispatchEvent.bind(k),this.open=function(b){h=new WebSocket(g.url,c||[]),b||k.dispatchEvent(l("connecting")),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","attempt-connect",g.url);var d=h,e=setTimeout(function(){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","connection-timeout",g.url),j=!0,d.close(),j=!1},g.timeoutInterval);h.onopen=function(){clearTimeout(e),(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onopen",g.url),g.protocol=h.protocol,g.readyState=WebSocket.OPEN,g.reconnectAttempts=0;var d=l("open");d.isReconnect=b,b=!1,k.dispatchEvent(d)},h.onclose=function(c){if(clearTimeout(e),h=null,i)g.readyState=WebSocket.CLOSED,k.dispatchEvent(l("close"));else{g.readyState=WebSocket.CONNECTING;var d=l("connecting");d.code=c.code,d.reason=c.reason,d.wasClean=c.wasClean,k.dispatchEvent(d),b||j||((g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onclose",g.url),k.dispatchEvent(l("close")));var e=g.reconnectInterval*Math.pow(g.reconnectDecay,g.reconnectAttempts);setTimeout(function(){g.reconnectAttempts++,g.open(!0)},e>g.maxReconnectInterval?g.maxReconnectInterval:e)}},h.onmessage=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onmessage",g.url,b.data);var c=l("message");c.data=b.data,k.dispatchEvent(c)},h.onerror=function(b){(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","onerror",g.url,b),k.dispatchEvent(l("error"))}},1==this.automaticOpen&&this.open(!1),this.send=function(b){if(h)return(g.debug||a.debugAll)&&console.debug("ReconnectingWebSocket","send",g.url,b),h.send(b);throw"INVALID_STATE_ERR : Pausing to reconnect websocket"},this.close=function(a,b){"undefined"==typeof a&&(a=1e3),i=!0,h&&h.close(a,b)},this.refresh=function(){h&&h.close()}}return a.prototype.onopen=function(){},a.prototype.onclose=function(){},a.prototype.onconnecting=function(){},a.prototype.onmessage=function(){},a.prototype.onerror=function(){},a.debugAll=!1,a.CONNECTING=WebSocket.CONNECTING,a.OPEN=WebSocket.OPEN,a.CLOSING=WebSocket.CLOSING,a.CLOSED=WebSocket.CLOSED,a}); |