diff --git a/dist/adapter.esm.js b/dist/adapter.esm.js index 54cd5df20fdb923111cead976f98e7d266fa1215..9398f89ef080b9a7d96e5f880b87a8a2c98b9ffd 100644 --- a/dist/adapter.esm.js +++ b/dist/adapter.esm.js @@ -93,6 +93,10 @@ class Adapter { }); } + /** + * Setup a timer to call poll every interval seconds + * @param {int} interval + */ pollEveryXSeconds(interval) { if (this.pollTimer) clearTimeout(this.pollTimer); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); @@ -138,6 +142,11 @@ class Adapter { }); } + /** + * Process a message received either from poll + * or from socket.onMessage + * @param {object} message + */ receive(message) { try { const parsed = JSON.parse(message.message); @@ -153,21 +162,30 @@ class Adapter { } } + /** + * Decide whether to use http or socket for comms + * with the backbone + * @returns string + */ getMode() { return ('mode' in this.config) ? this.config.mode : 'http'; } + /** + * Create a new socket connection and send an authentication + * message. Websocket doesn't support headers so as soon + * as the socket is open a message is sent containing + * the authorisation header to authenticate the connection + */ openSocket() { this.getAuthorizationHeader() .then((headers) => { const protocols = []; - const options = { headers }; - + const socket = new WebSocket( this.config.socket, protocols, - options, ); socket.addEventListener('open', () => { @@ -193,6 +211,11 @@ class Adapter { }); } + /** + * If the socket connection is lost ensure that a new socket + * is opened rather than continuing to send messages on this + * socket + */ closeSocket() { this.socket = null; } @@ -203,7 +226,6 @@ class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body - * @param {boolean} is_retry * @returns */ publish(topic, body) { @@ -216,6 +238,13 @@ class Adapter { return response; } + /** + * Send via a POST to /send + * @param {string} topic + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_publish(topic, body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -252,6 +281,11 @@ class Adapter { }); } + /** + * Publish via websocket socket.send + * @param {string} topic + * @param {string} body + */ socket_publish(topic, body) { if (this.socket) { this.socket.send(JSON.stringify({ topic, message: body })); @@ -271,7 +305,6 @@ class Adapter { * * Messages should be passed through encode before sending * @param {string} body - * @param {boolean} is_retry * @returns */ broadcast(body) { @@ -284,6 +317,12 @@ class Adapter { return response; } + /** + * Broadcast via POST to /notify + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_broadcast(body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -319,6 +358,10 @@ class Adapter { }); } + /** + * Broadcast via websocket socket.send + * @param {string} body + */ socket_broadcast(body) { if (this.socket) { this.socket.send(JSON.stringify({ message: body })); diff --git a/dist/adapter.js b/dist/adapter.js index 58c2f58558c9efb78e61b58529f65b5bfbc46f6a..a262c180345426b690f83dca16fc3fdf7d71591e 100644 --- a/dist/adapter.js +++ b/dist/adapter.js @@ -95,6 +95,10 @@ class Adapter { }); } + /** + * Setup a timer to call poll every interval seconds + * @param {int} interval + */ pollEveryXSeconds(interval) { if (this.pollTimer) clearTimeout(this.pollTimer); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); @@ -140,6 +144,11 @@ class Adapter { }); } + /** + * Process a message received either from poll + * or from socket.onMessage + * @param {object} message + */ receive(message) { try { const parsed = JSON.parse(message.message); @@ -155,21 +164,30 @@ class Adapter { } } + /** + * Decide whether to use http or socket for comms + * with the backbone + * @returns string + */ getMode() { return ('mode' in this.config) ? this.config.mode : 'http'; } + /** + * Create a new socket connection and send an authentication + * message. Websocket doesn't support headers so as soon + * as the socket is open a message is sent containing + * the authorisation header to authenticate the connection + */ openSocket() { this.getAuthorizationHeader() .then((headers) => { const protocols = []; - const options = { headers }; - + const socket = new WebSocket( this.config.socket, protocols, - options, ); socket.addEventListener('open', () => { @@ -195,6 +213,11 @@ class Adapter { }); } + /** + * If the socket connection is lost ensure that a new socket + * is opened rather than continuing to send messages on this + * socket + */ closeSocket() { this.socket = null; } @@ -205,7 +228,6 @@ class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body - * @param {boolean} is_retry * @returns */ publish(topic, body) { @@ -218,7 +240,14 @@ class Adapter { return response; } - http_publish(topic, body, body, is_retry = false) { + /** + * Send via a POST to /send + * @param {string} topic + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ + http_publish(topic, body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() .then((headers) => { @@ -254,6 +283,11 @@ class Adapter { }); } + /** + * Publish via websocket socket.send + * @param {string} topic + * @param {string} body + */ socket_publish(topic, body) { if (this.socket) { this.socket.send(JSON.stringify({ topic, message: body })); @@ -273,7 +307,6 @@ class Adapter { * * Messages should be passed through encode before sending * @param {string} body - * @param {boolean} is_retry * @returns */ broadcast(body) { @@ -286,6 +319,12 @@ class Adapter { return response; } + /** + * Broadcast via POST to /notify + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_broadcast(body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -321,6 +360,10 @@ class Adapter { }); } + /** + * Broadcast via websocket socket.send + * @param {string} body + */ socket_broadcast(body) { if (this.socket) { this.socket.send(JSON.stringify({ message: body })); diff --git a/src/adapter/index.js b/src/adapter/index.js index e27d9d4cb078b3676936816ff8c457314ca0ac6d..fe7faa1987166365616c4546858a5e2be5b02a51 100644 --- a/src/adapter/index.js +++ b/src/adapter/index.js @@ -93,6 +93,10 @@ export class Adapter { }); } + /** + * Setup a timer to call poll every interval seconds + * @param {int} interval + */ pollEveryXSeconds(interval) { if (this.pollTimer) clearTimeout(this.pollTimer); this.pollTimer = setTimeout(this.pollEveryXSeconds.bind(this, interval), interval*1000); @@ -138,6 +142,11 @@ export class Adapter { }); } + /** + * Process a message received either from poll + * or from socket.onMessage + * @param {object} message + */ receive(message) { try { const parsed = JSON.parse(message.message); @@ -153,21 +162,30 @@ export class Adapter { } } + /** + * Decide whether to use http or socket for comms + * with the backbone + * @returns string + */ getMode() { return ('mode' in this.config) ? this.config.mode : 'http'; } + /** + * Create a new socket connection and send an authentication + * message. Websocket doesn't support headers so as soon + * as the socket is open a message is sent containing + * the authorisation header to authenticate the connection + */ openSocket() { this.getAuthorizationHeader() .then((headers) => { const protocols = []; - const options = { headers }; - + const socket = new WebSocket( this.config.socket, protocols, - options, ); socket.addEventListener('open', () => { @@ -193,6 +211,11 @@ export class Adapter { }); } + /** + * If the socket connection is lost ensure that a new socket + * is opened rather than continuing to send messages on this + * socket + */ closeSocket() { this.socket = null; } @@ -203,7 +226,6 @@ export class Adapter { * Messages should be passed through encode before sending * @param {string} topic * @param {string} body - * @param {boolean} is_retry * @returns */ publish(topic, body) { @@ -216,6 +238,12 @@ export class Adapter { return response; } + /** + * Send via a POST to /send + * @param {string} topic + * @param {string} body + * @returns Promise + */ http_publish(topic, body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -252,6 +280,11 @@ export class Adapter { }); } + /** + * Publish via websocket socket.send + * @param {string} topic + * @param {string} body + */ socket_publish(topic, body) { if (this.socket) { this.socket.send(JSON.stringify({ topic, message: body })); @@ -271,7 +304,6 @@ export class Adapter { * * Messages should be passed through encode before sending * @param {string} body - * @param {boolean} is_retry * @returns */ broadcast(body) { @@ -284,6 +316,12 @@ export class Adapter { return response; } + /** + * Broadcast via POST to /notify + * @param {string} body + * @param {boolean} is_retry + * @returns Promise + */ http_broadcast(body, is_retry = false) { let adapterConfig = this.config; return this.getAuthorizationHeader() @@ -319,6 +357,10 @@ export class Adapter { }); } + /** + * Broadcast via websocket socket.send + * @param {string} body + */ socket_broadcast(body) { if (this.socket) { this.socket.send(JSON.stringify({ message: body }));