Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function (ccg) {
ccg.prototype.response = "";
ccg.prototype.readyForNextCommand = true;
ccg.prototype.waitingForDoubleTerminator = false;
ccg.prototype.reconnectTimeout = null;

// ccg.connect
// -----------
Expand Down Expand Up @@ -41,17 +42,24 @@ module.exports = function (ccg) {
if (port) self.options.port = port;

if (self.options.reconnect && typeof(self.options.reconnectInterval) != "number") {
self.options.reconnectInterval = 15;
self.options.reconnectInterval = 3;
}

self.disconnecting = false;

var client = self.client = net.connect({port: self.options.port, host: self.options.host});



client.on("connect", function () {
self.log("Connected to", self.options.host, self.options.port);
self.connected = true;

if(self.reconnectTimeout){
clearInterval(self.reconnectTimeout)
self.reconnectTimeout = null
}

if (cb) {
cb();
cb = false;
Expand All @@ -67,11 +75,7 @@ module.exports = function (ccg) {

self.emit("connectionError", err);

if (!self.disconnecting && self.options.reconnect) {
setTimeout(function () {
self.connect(); self.emit("reconnecting");
}, (self.options.reconnectInterval * 1000));
}
self.reconnect()
});

client.on("reconnecting", function () {
Expand All @@ -82,13 +86,8 @@ module.exports = function (ccg) {
self.log("Disconnected");
self.emit("disconnected");
self.connected = false;
client = false;

if (!self.disconnecting && self.options.reconnect) {
setTimeout(function () {
self.connect(); self.emit("reconnecting");
}, (self.options.reconnectInterval * 1000));
}
self.reconnect()
});

client.on("data", function (chunk) {
Expand Down Expand Up @@ -172,8 +171,24 @@ module.exports = function (ccg) {

finishedCommand(self);
});

this.reconnect()
};

ccg.prototype.reconnect = function () {
let self = this
if (!self.disconnecting && self.options.reconnect && !self.reconnectTimeout) {
self.reconnectTimeout = setInterval(function () {
self.client.removeAllListeners()
self.client.end()
self.client.destroy()
self.client = false;
self.emit("reconnecting");
self.connect();
}, (self.options.reconnectInterval * 1000));
}
}

// ccg.disconnect
// --------------
// Empties command queue and closes connection to Caspar CG
Expand Down