Skip to content

Commit

Permalink
Merge pull request #35 from pipedrive/fix_channel
Browse files Browse the repository at this point in the history
Implement multi-channel connectivity
  • Loading branch information
andrewkarell authored Dec 16, 2016
2 parents cc9b85e + 2f3eff9 commit 4910ff1
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ function Channel(apiToken) {
});
}

if (data.switch_to_channel && data.switch_to_channel.toString().match(/^\d+$/)) {
process.env.PIPEDRIVE_CHANNEL_HOST = 'channel' + data.switch_to_channel + '.pipedrive.com';
self.restartClient();
return;
}

if (data.rabbitStateChange === 'open') {
if (handlers['connect']) {
_.each(handlers['connect'], function(handler) {
Expand All @@ -96,7 +102,7 @@ function Channel(apiToken) {
client.onclose = function (e) {
if (!clientClosed) {
// not closed by user - we have some connection error.
self.restartClient();
self.startClient();
return;
}

Expand All @@ -111,13 +117,11 @@ function Channel(apiToken) {

this.restartClient = function() {
client.onopen = null;
client.onclose = null;
client.onmessage = null;
client = null;

clientStarted = false;
clientClosed = false;

setTimeout(self.startClient, (1+Math.random()*4)*1000);
client.close();
};

this.on = function(method, handler) {
Expand Down Expand Up @@ -145,6 +149,6 @@ function Channel(apiToken) {
if (client && client.close) {
client.close();
}
}
};

}

0 comments on commit 4910ff1

Please sign in to comment.