Skip to content

Commit

Permalink
The socket reported erroneously timeouts once the connection has been…
Browse files Browse the repository at this point in the history
… established.

This was simply due to inactivity on that socket.
I disabled the timeout once the connection is established so that the keepalive mechanism of ember can take place.
  • Loading branch information
BIALOBOS, Christophe committed Jan 17, 2019
1 parent 67592ad commit 963026c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ S101Socket.prototype.connect = function (timeout = 2) {
self.emit('connecting');

self.codec = new S101Codec();

const connectTimeoutListener = () => {
self.socket.destroy();
self.emit("error", new Error(`Could not connect to ${self.address}:${self.port} after a timeout of ${timeout} seconds`));
};

self.socket = net.createConnection({
port: self.port,
host: self.address,
Expand All @@ -173,6 +179,10 @@ S101Socket.prototype.connect = function (timeout = 2) {
() => {
winston.debug('socket connected');

// Disable connect timeout to hand-over to keepalive mechanism
self.socket.removeListener("timeout", connectTimeoutListener);
self.socket.setTimeout(0);

self.keepaliveIntervalTimer = setInterval(() => {
try {
self.sendKeepaliveRequest();
Expand Down Expand Up @@ -204,10 +214,8 @@ S101Socket.prototype.connect = function (timeout = 2) {
}
).on('error', (e) => {
self.emit("error", e);
}).on("timeout", () => {
self.socket.destroy();
self.emit("error", new Error(`Could not connect to ${self.address}:${self.port} after a timeout of ${timeout} seconds`));
}).on('data', (data) => {
}).once("timeout", connectTimeoutListener
).on('data', (data) => {
if (self.isConnected()) {
self.codec.dataIn(data);
}
Expand Down
16 changes: 16 additions & 0 deletions test/DeviceTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ describe("DeviceTree", () => {
.then(() => tree.disconnect())
})
});

it("should not disconnect after 5 seconds of inactivity", () => {
return Promise.resolve()
.then(() => {
let tree = new DeviceTree(LOCALHOST, PORT);

tree.on("error", error => {
throw error;
});

return Promise.resolve()
.then(() => tree.connect())
.then(() => new Promise(resolve => setTimeout(resolve, 5000)))
.then(() => tree.disconnect())
})
}, 7000);
});

it("timeout should be taken into account when connecting to unknown host", () => {
Expand Down

0 comments on commit 963026c

Please sign in to comment.