Permalink
Browse files

Added support for automatically reconnecting on timeout.

  • Loading branch information...
alexkwolfe committed Mar 11, 2012
1 parent 66b3259 commit 9f012c15fdd8a6828f1a5c1b8735363e0e6c7941
Showing with 67 additions and 11 deletions.
  1. +31 −2 index.js
  2. +15 −6 readme.md
  3. +21 −3 spec/timeout_spec.js
View
@@ -7,7 +7,10 @@ var EverSocket = function(options) {
net.Socket.call(this, options);
+ this.setTimeout(options.timeout || 0);
+
this._retry = {
+ onTimeout: options.reconnectOnTimeout || false,
wait: options.reconnectWait || 1000,
waiting: false
};
@@ -21,9 +24,20 @@ EverSocket.prototype.setReconnectWait = function reconnectWait(ms) {
this._retry.wait = ms;
};
+EverSocket.prototype.setReconnectOnTimeout = function reconnectOnTimeout(reconnect) {
+ this._retry.onTimeout = reconnect;
+ this._setupTimeoutListener();
+};
+
EverSocket.prototype.destroy = function destroy() {
- this.removeListener('close', this._closeListener);
- this._closeListener = null;
+ if (this._timeoutListener) {
+ this.removeListener('timeout', this._timeoutListener);
+ this._timeoutListener = null;
+ }
+ if (this._closeListener) {
+ this.removeListener('close', this._closeListener);
+ this._closeListener = null;
+ }
this.constructor.super_.prototype.destroy();
};
@@ -102,6 +116,21 @@ EverSocket.prototype._setup = function _setup() {
};
this.on('close', this._closeListener);
}
+ this._setupTimeoutListener();
+};
+
+EverSocket.prototype._setupTimeoutListener = function _setupTimeoutListener() {
+ var self = this;
+ if (this._retry.onTimeout && !this._timeoutListener) {
+ this._timeoutListener = function() {
+ self.reset();
+ self.reconnect();
+ };
+ this.on('timeout', this._timeoutListener);
+ } else if (!this._retry.onTimeout && this._timeoutListener) {
+ this.removeListener('timeout', this._timeoutListener);
+ this._timeoutListener = null;
+ }
};
exports.EverSocket = EverSocket;
View
@@ -1,8 +1,14 @@
-A Node.js net.Socket that automatically reconnects on close.
+A Node.js net.Socket that automatically reconnects on close or timeout.
## Options
-Options are the same as those for the net.Socket constructor, with the exception of `reconnectWait` which specifies the number of milliseconds to wait between reconnection events. This option can also be set using the `setReconnectWait` mutator.
+Options are the same as those for the net.Socket constructor, with a few additions:
+
+ * `reconnectWait` — the number of milliseconds to wait between reconnection events
+ * `reconnectOnTimeout` — `true` or `false`. Whether to automatically reconnect when the connection goes idle.
+ * `timeout` — The idle timeout. You must provide a timeout if you want to use `reconnectOnTimeout`.
+
+All of the options can also be set after socket construction using their mutator methods.
## Events
@@ -12,11 +18,14 @@ The regular net.Socket events are emitted. The `reconnect` event has been added
```javascript
var EverSocket = require('eversocket').EverSocket;
-// Create a new socket.
-// If the connection closes, wait 100ms then reconnect.
-var socket = new EverSocket({ type: 'tcp4', reconnectWait: 100 });
+var socket = new EverSocket({
+ type: 'tcp4',
+ reconnectWait: 100, // wait 100ms after close event before reconnecting
+ timeout: 100, // set the idle timeout to 100ms
+ reconnectOnTimeout: true // reconnect if the connection is idle
+});
socket.on('reconnect', function() {
- console.log('the socket reconnected following a close event');
+ console.log('the socket reconnected following a close or timeout event');
});
socket.connect(4999);
```
View
@@ -14,8 +14,7 @@ describe("EverSocket", function() {
beforeEach(function() {
server = net.createServer();
server.listen(port);
- socket = new EverSocket({ type: 'tcp4' });
- socket.setTimeout(10);
+ socket = new EverSocket({ type: 'tcp4', timeout: 10 });
});
afterEach(function() {
@@ -43,7 +42,26 @@ describe("EverSocket", function() {
socket.connect(port);
});
-
+ it('should reconnect on timeout', function(done) {
+ socket = new EverSocket({ type: 'tcp4', timeout: 10, reconnectOnTimeout: true, reconnectWait: 1 });
+ server.on('connection', function(c) {
+ var writes = 0;
+ var interval = setInterval(function() {
+ c.write('.');
+ if (++writes >= 5)
+ clearInterval(interval);
+ }, 5);
+ });
+ var data = '';
+ socket.on('reconnect', function() {
+ assert.equal('.....', data);
+ done();
+ });
+ socket.on('data', function(d) {
+ data += d;
+ })
+ socket.connect(port);
+ });
});

0 comments on commit 9f012c1

Please sign in to comment.