|
50 | 50 | * |
51 | 51 | * Latest version: https://github.com/joewalnes/reconnecting-websocket/ |
52 | 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 | + * reconnectInterval |
| 80 | + * - The number of milliseconds to delay before attempting to reconnect. Accepts integer. Default: 1000. |
| 81 | + * |
| 82 | + * reconnectDecay |
| 83 | + * - The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. Accepts integer or float. Default: 1.5. |
| 84 | + * |
| 85 | + * timeoutInterval |
| 86 | + * - The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. Accepts integer. Default: 2000. |
| 87 | + * |
53 | 88 | */ |
54 | 89 | (function (global, factory) { |
55 | 90 | if (typeof define === 'function' && define.amd) { |
|
61 | 96 | } |
62 | 97 | })(this, function () { |
63 | 98 |
|
64 | | - function ReconnectingWebSocket(url, protocols) { |
65 | | - // These can be altered by calling code. |
| 99 | + function ReconnectingWebSocket(url, protocols, options) { |
66 | 100 |
|
67 | | - /** Whether this instance should log debug messages. */ |
68 | | - this.debug = false; |
69 | | - /** The number of milliseconds to delay before attempting to reconnect. */ |
70 | | - this.reconnectInterval = 1000; |
71 | | - /** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */ |
72 | | - this.reconnectDecay = 1.5; |
73 | | - /** The number of attempted reconnects since starting, or the last successful connection. */ |
74 | | - this.reconnectAttempts = 0; |
75 | | - /** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */ |
76 | | - this.timeoutInterval = 2000; |
| 101 | + // Default settings |
| 102 | + var settings = { |
| 103 | + /** Whether this instance should log debug messages. */ |
| 104 | + debug: false, |
| 105 | + /** The number of milliseconds to delay before attempting to reconnect. */ |
| 106 | + reconnectInterval: 1000, |
| 107 | + /** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */ |
| 108 | + reconnectDecay: 1.5, |
| 109 | + /** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */ |
| 110 | + timeoutInterval: 2000 |
| 111 | + } |
| 112 | + |
| 113 | + // Overwrite and define settings with options if they exist. |
| 114 | + for (var key in settings) { |
| 115 | + if (typeof options[key] !== 'undefined') { |
| 116 | + this[key] = options[key]; |
| 117 | + } else { |
| 118 | + this[key] = settings[key]; |
| 119 | + } |
| 120 | + } |
77 | 121 |
|
78 | 122 | // These should be treated as read-only properties |
79 | 123 |
|
80 | 124 | /** The URL as resolved by the constructor. This is always an absolute URL. Read only. */ |
81 | 125 | this.url = url; |
| 126 | + /** The number of attempted reconnects since starting, or the last successful connection. Read only. */ |
| 127 | + this.reconnectAttempts = 0; |
82 | 128 | /** |
83 | 129 | * The current state of the connection. |
84 | 130 | * Can be one of: WebSocket.CONNECTING, WebSocket.OPEN, WebSocket.CLOSING, WebSocket.CLOSED |
|
0 commit comments