Skip to content

Commit 7706574

Browse files
committed
Auto-reconnect improvements. Correctly reading client options within .connect()
1 parent dd79252 commit 7706574

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ IrcClient.prototype.startPeriodicPing = function() {
266266

267267
function pingTimeout() {
268268
that.emit('ping timeout');
269-
that.quit('Ping timeout (' + that.options.ping_timeout + ' seconds)');
269+
var end_msg = that.rawString('QUIT', 'Ping timeout (' + that.options.ping_timeout + ' seconds)');
270+
that.connection.end(end_msg, true);
270271
}
271272

272273
this.resetPingTimer = resetPingTimer;

src/connection.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ function Connection(options) {
1010
this.connected = false;
1111
this.requested_disconnect = false;
1212

13-
this.auto_reconnect = this.options.auto_reconnect || false;
14-
this.auto_reconnect_wait = this.options.auto_reconnect_wait || 4000;
15-
this.auto_reconnect_max_retries = this.options.auto_reconnect_max_retries || 3;
1613
this.reconnect_attempts = 0;
1714

1815
// When an IRC connection was successfully registered.
@@ -49,6 +46,10 @@ Connection.prototype.connect = function(options) {
4946
}
5047
options = this.options;
5148

49+
this.auto_reconnect = options.auto_reconnect || false;
50+
this.auto_reconnect_wait = options.auto_reconnect_wait || 4000;
51+
this.auto_reconnect_max_retries = options.auto_reconnect_max_retries || 3;
52+
5253
if (this.transport) {
5354
unbindTransportEvents(this.transport);
5455
}
@@ -199,21 +200,26 @@ Connection.prototype.clearTimers = function() {
199200
/**
200201
* Close the connection to the IRCd after forcing one last line
201202
*/
202-
Connection.prototype.end = function(data, callback) {
203+
Connection.prototype.end = function(data, had_error) {
203204
var that = this;
204205

205-
this.debugOut('Connection.end() connected=' + this.connected + ' with data=' + !!data);
206+
this.debugOut('Connection.end() connected=' + this.connected + ' with data=' + !!data + ' had_error=' + !!had_error);
206207

207208
if (this.connected && data) {
208209
// Once the last bit of data has been sent, then re-run this function to close the socket
209210
this.write(data, function() {
210-
that.end();
211+
that.end(null, had_error);
211212
});
212213

213214
return;
214215
}
215216

216-
this.requested_disconnect = true;
217+
// Shutdowns of the connection may be caused by errors like ping timeouts, which
218+
// are not requested by the user so we leave requested_disconnect as false to make sure any
219+
// reconnects happen.
220+
if (!had_error) {
221+
this.requested_disconnect = true;
222+
}
217223

218224
if (this.transport) {
219225
this.transport.close();

0 commit comments

Comments
 (0)