Skip to content

Commit 4910ff1

Browse files
authored
Merge pull request #35 from pipedrive/fix_channel
Implement multi-channel connectivity
2 parents cc9b85e + 2f3eff9 commit 4910ff1

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

lib/channel.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ function Channel(apiToken) {
8484
});
8585
}
8686

87+
if (data.switch_to_channel && data.switch_to_channel.toString().match(/^\d+$/)) {
88+
process.env.PIPEDRIVE_CHANNEL_HOST = 'channel' + data.switch_to_channel + '.pipedrive.com';
89+
self.restartClient();
90+
return;
91+
}
92+
8793
if (data.rabbitStateChange === 'open') {
8894
if (handlers['connect']) {
8995
_.each(handlers['connect'], function(handler) {
@@ -96,7 +102,7 @@ function Channel(apiToken) {
96102
client.onclose = function (e) {
97103
if (!clientClosed) {
98104
// not closed by user - we have some connection error.
99-
self.restartClient();
105+
self.startClient();
100106
return;
101107
}
102108

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

112118
this.restartClient = function() {
113119
client.onopen = null;
114-
client.onclose = null;
115120
client.onmessage = null;
116-
client = null;
117-
118121
clientStarted = false;
122+
clientClosed = false;
119123

120-
setTimeout(self.startClient, (1+Math.random()*4)*1000);
124+
client.close();
121125
};
122126

123127
this.on = function(method, handler) {
@@ -145,6 +149,6 @@ function Channel(apiToken) {
145149
if (client && client.close) {
146150
client.close();
147151
}
148-
}
152+
};
149153

150154
}

0 commit comments

Comments
 (0)