Skip to content

Commit

Permalink
Added the cancel() function to stop re-connections. Now all socket cl…
Browse files Browse the repository at this point in the history
…osures, even those resulting from error, will be reconnected. Closes #2.
  • Loading branch information
alexkwolfe committed Oct 7, 2013
1 parent 04c2078 commit c3565ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 49 deletions.
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var EverSocket = function (options) {
waiting: false
};

this._destroyed = false;
this._cancelled = false;

this._setup();
};
Expand All @@ -31,8 +31,10 @@ EverSocket.prototype.setReconnectOnTimeout = function reconnectOnTimeout(reconne
this._setupTimeoutListener();
};

EverSocket.prototype.destroy = function destroy() {
this._destroyed = true;
EverSocket.prototype.cancel = function cancel() {
if (this._cancelled)
return;
this._cancelled = true;
if (this._timeoutListener) {
this.removeListener('timeout', this._timeoutListener);
this._timeoutListener = null;
Expand All @@ -41,11 +43,10 @@ EverSocket.prototype.destroy = function destroy() {
this.removeListener('close', this._closeListener);
this._closeListener = null;
}
this.constructor.super_.prototype.destroy.call(this);
};

EverSocket.prototype.reset = function reset() {
this._destroyed = false;
this._cancelled = false;
this._retry.waiting = false;
this.constructor.super_.prototype.destroy.call(this);
};
Expand All @@ -57,7 +58,7 @@ EverSocket.prototype.reconnect = function reconnect() {
function doReconnect() {

// Bail if we're already reconnecting or if we have been destroyed
if (self._retry.waiting || self._destroyed)
if (self._retry.waiting || self._cancelled)
return;

// Set flag to indicate reconnecting
Expand Down Expand Up @@ -134,7 +135,7 @@ EverSocket.prototype._setup = function _setup() {
};

EverSocket.prototype._setupTimeoutListener = function _setupTimeoutListener() {
if (this._destroyed) return;
if (this._cancelled) return;
var self = this;
if (this._retry.onTimeout && !this._timeoutListener) {
this._timeoutListener = function () {
Expand Down
18 changes: 15 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ The regular net.Socket events are emitted. The `reconnect` event has been added

```javascript
var EverSocket = require('eversocket').EverSocket;
var socket = new EverSocket({
type: 'tcp4',
var socket = new EverSocket({
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
Expand All @@ -28,4 +27,17 @@ socket.on('reconnect', function() {
console.log('the socket reconnected following a close or timeout event');
});
socket.connect(4999);
```
```

## Cancelling re-connections

In order to destroy the socket, re-connections must be cancelled.

```javascript
var EverSocket = require('eversocket').EverSocket;
var socket = new EverSocket();
socket.connect(4999);
// ...
socket.cancel(); // cancel re-connections
socket.destroy();
```
57 changes: 18 additions & 39 deletions spec/reconnect_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ describe("EverSocket", function() {
afterEach(function() {
try {
socket.destroy();
} catch (e) {}
} catch(e) {}
try {
server.close();
} catch (e) {}
} catch(e) {}
});

it('should connect', function(done) {
Expand All @@ -34,7 +34,7 @@ describe("EverSocket", function() {
});
socket.on('connect', function() {
clientConnected = true;
})
});
socket.connect(port);
setTimeout(function() {
assert.isTrue(serverConnected);
Expand All @@ -44,50 +44,29 @@ describe("EverSocket", function() {
});

it('should reconnect', function(done) {
var reconnected = false;

server.once('connection', function() {
// socket connected for the first time
server.once('connection', function() {
// socket has reconnected
reconnected = true;
});

// close the socket
socket.constructor.super_.prototype.destroy.call(socket);

// listen for reconnect
socket.once('reconnect', function() {
setTimeout(function() {
assert.isTrue(reconnected);
done();
}, 100);
});
setTimeout(function() {
server.once('connection', function() {
done()
});
socket.destroy();
}, 10)
});

socket.connect(port);
});

it('should not reconnect after destroy', function(done) {
var reconnected = false;

it('should not reconnect after cancel', function(done) {
server.once('connection', function() {
// socket connected for the first time
server.once('connection', function() {
// socket has reconnected
reconnected = true;
});
socket.destroy();
setTimeout(function() {
server.once('connection', function() {
assert.isTrue(false, 'should not have reconnected');
});
setTimeout(done, 100);
socket.cancel();
socket.destroy();
}, 10)
});

socket.connect(port);

setTimeout(function() {
assert.isFalse(reconnected);
done();
}, 100);
});


});

0 comments on commit c3565ba

Please sign in to comment.