Unverified Commit 3e437e7e authored by Dan Jones's avatar Dan Jones
Browse files

refactor: move setup into adapter start()

Based on mode adapter.start() either opens the websocket
or starts polling for incoming messages from GET/receive
parent 607967b2
......@@ -8,8 +8,20 @@ class Adapter {
this.protocol = protocol;
this.config = config;
this.axios = axios;
console.error(this.getMode());
if (this.getMode() === 'socket') this.openSocket();
this.pollTimer = null;
}
/**
* Start listening for inbound messages
*/
start() {
switch (this.getMode()) {
case 'socket': this.openSocket(); break;
case 'http': {
const interval = ('pollInterval' in this.config) ? this.config.pollInterval : 5;
this.pollEveryXSeconds(interval);
} break;
}
}
/**
......@@ -81,6 +93,14 @@ class Adapter {
});
}
pollEveryXSeconds(interval) {
this.poll()
.then(() => {
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.poll(), interval*1000);
});
}
/**
* Call the GET /receive endpoint and process the messages with decode
*
......@@ -231,7 +251,13 @@ class Adapter {
}
socket_publish(topic, body) {
this.socket.send(JSON.stringify({ topic, message: body }));
if (this.socket) {
this.socket.send(JSON.stringify({ topic, message: body }));
} else {
this.openSocket().then((socket) => {
socket.send(JSON.stringify({ topic, message: body }));
});
}
}
/**
......@@ -292,7 +318,13 @@ class Adapter {
}
socket_broadcast(body) {
this.socket.send(JSON.stringify({ message: body }));
if (this.socket) {
this.socket.send(JSON.stringify({ message: body }));
} else {
this.openSocket().then((socket) => {
socket.send(JSON.stringify({ message: body }));
});
}
}
}
......
......@@ -10,8 +10,20 @@ class Adapter {
this.protocol = protocol;
this.config = config;
this.axios = axios;
console.error(this.getMode());
if (this.getMode() === 'socket') this.openSocket();
this.pollTimer = null;
}
/**
* Start listening for inbound messages
*/
start() {
switch (this.getMode()) {
case 'socket': this.openSocket(); break;
case 'http': {
const interval = ('pollInterval' in this.config) ? this.config.pollInterval : 5;
this.pollEveryXSeconds(interval);
} break;
}
}
/**
......@@ -83,6 +95,14 @@ class Adapter {
});
}
pollEveryXSeconds(interval) {
this.poll()
.then(() => {
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.poll(), interval*1000);
});
}
/**
* Call the GET /receive endpoint and process the messages with decode
*
......@@ -233,7 +253,13 @@ class Adapter {
}
socket_publish(topic, body) {
this.socket.send(JSON.stringify({ topic, message: body }));
if (this.socket) {
this.socket.send(JSON.stringify({ topic, message: body }));
} else {
this.openSocket().then((socket) => {
socket.send(JSON.stringify({ topic, message: body }));
});
}
}
/**
......@@ -294,7 +320,13 @@ class Adapter {
}
socket_broadcast(body) {
this.socket.send(JSON.stringify({ message: body }));
if (this.socket) {
this.socket.send(JSON.stringify({ message: body }));
} else {
this.openSocket().then((socket) => {
socket.send(JSON.stringify({ message: body }));
});
}
}
}
......
......@@ -8,7 +8,20 @@ export class Adapter {
this.protocol = protocol;
this.config = config;
this.axios = axios;
if (this.getMode() === 'socket') this.openSocket();
this.pollTimer = null;
}
/**
* Start listening for inbound messages
*/
start() {
switch (this.getMode()) {
case 'socket': this.openSocket(); break;
case 'http': {
const interval = ('pollInterval' in this.config) ? this.config.pollInterval : 5;
this.pollEveryXSeconds(interval);
} break;
}
}
/**
......@@ -80,6 +93,14 @@ export class Adapter {
});
}
pollEveryXSeconds(interval) {
this.poll()
.then(() => {
if (this.pollTimer) clearTimeout(this.pollTimer);
this.pollTimer = setTimeout(this.poll(), interval*1000);
});
}
/**
* Call the GET /receive endpoint and process the messages with decode
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment