From f3382a7f05771be43f9886b96fd409072199307f Mon Sep 17 00:00:00 2001 From: Jake Wilson Date: Sun, 3 Aug 2014 00:09:31 -0600 Subject: [PATCH 1/3] Changed the connect method to a public API, so that a user can manually connect/reconnect. Refresh doesn't work if the user manually closed the connection, forcing the user to recreate the Reconnecting Websocket Object and redefine all methods for it. This pull request solves this problem. --- reconnecting-websocket.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/reconnecting-websocket.js b/reconnecting-websocket.js index d85f6b4..5011fd5 100644 --- a/reconnecting-websocket.js +++ b/reconnecting-websocket.js @@ -40,7 +40,7 @@ * onopen // sometime later... * onmessage * onmessage - * etc... + * etc... * * It is API compatible with the standard WebSocket API. * @@ -71,7 +71,7 @@ var ws; var forcedClose = false; var timedOut = false; - + this.url = url; this.protocols = protocols; this.readyState = WebSocket.CONNECTING; @@ -92,14 +92,14 @@ this.onerror = function(event) { }; - function connect(reconnectAttempt) { + this.connect = function(reconnectAttempt) { ws = new WebSocket(url, protocols); - + self.onconnecting(); if (self.debug || ReconnectingWebSocket.debugAll) { console.debug('ReconnectingWebSocket', 'attempt-connect', url); } - + var localWs = ws; var timeout = setTimeout(function() { if (self.debug || ReconnectingWebSocket.debugAll) { @@ -109,7 +109,7 @@ localWs.close(); timedOut = false; }, self.timeoutInterval); - + ws.onopen = function(event) { clearTimeout(timeout); if (self.debug || ReconnectingWebSocket.debugAll) { @@ -120,7 +120,7 @@ self.reconnectAttempts = 0; self.onopen(event); }; - + ws.onclose = function(event) { clearTimeout(timeout); ws = null; @@ -138,7 +138,7 @@ } setTimeout(function() { self.reconnectAttempts++; - connect(true); + self.connect(true); }, self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts)); } }; @@ -155,7 +155,7 @@ self.onerror(event); }; } - connect(false); + this.connect(false); this.send = function(data) { if (ws) { From c4afa025d034e6ad6cd7c971021cfa01d4f85f64 Mon Sep 17 00:00:00 2001 From: Jake Wilson Date: Mon, 4 Aug 2014 21:11:24 -0600 Subject: [PATCH 2/3] Changed connect() to open() to be more consistent with close(). --- reconnecting-websocket.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reconnecting-websocket.js b/reconnecting-websocket.js index 5011fd5..3b8be84 100644 --- a/reconnecting-websocket.js +++ b/reconnecting-websocket.js @@ -92,7 +92,7 @@ this.onerror = function(event) { }; - this.connect = function(reconnectAttempt) { + this.open = function(reconnectAttempt) { ws = new WebSocket(url, protocols); self.onconnecting(); @@ -138,7 +138,7 @@ } setTimeout(function() { self.reconnectAttempts++; - self.connect(true); + self.open(true); }, self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts)); } }; @@ -155,7 +155,7 @@ self.onerror(event); }; } - this.connect(false); + this.open(false); this.send = function(data) { if (ws) { From 9dc4142add04881910ac3b0a848c1d996a82b61a Mon Sep 17 00:00:00 2001 From: Jake Wilson Date: Mon, 4 Aug 2014 21:48:00 -0600 Subject: [PATCH 3/3] Added basic documentation on using `open`, `close` and `refresh`. --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 9b3d4d4..230e1ae 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,11 @@ With a `ReconnectingWebSocket`, after an `onclose` event is called it will autom This is all handled automatically for you by the library. +Manually Opening, Closing and Refreshing connections +---------------------------------------- + +When you create a new `ReconnectingWebSocket`, the `WebSocket` connection is created and opened for you. In the case that you need to manually close a connection, simply run the `close()` method. To open the connection back up, use `open()`. If you need to close and immediately reopen the connection, use `refresh()`. + More ----