--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Yes, I do similar for my websocket connected web pages. Usually I have some small indicator to show that the page/data is still live. But this is just standard websocket javascript code and nothing to do with Node-RED per se.
What we are doing is making all node statuses available in the next release.
But this is just standard websocket javascript code and nothing to do with Node-RED per se
/* Runs when the page load has completed */
window.onload = function() {
/* Attempt a connection */
connect();
}
/* Before the page closes, disconnect cleanly from the server */
window.onbeforeunload = function() {
disconnect();
}
/* This function disconnects the websocket and optionally attempts to reconnect */
function disconnect(retry=false) {
/* If connected, disconnect */
if (isopen) socket.close();
/* if retry is specified, attempt to reconnect */
if (retry) reconnect();
}
/* This function handles connecting to the server */
function connect() {
/* if not connected... */
if (!isopen) {
var wsUri = "ws:";
var loc = window.location;
if (loc.protocol === "https:") { wsUri = "wss:"; }
wsUri += "//" + loc.host + loc.pathname.replace("pagename","ws/websocketname");
socket = new WebSocket(wsUri);
//socket = new WebSocket(`ws://${location.host}/ws/updatescheduletest`, 'MyServerProtocol');
/* When the socket connects... */
socket.onopen = function() {
/* Display message in console if debug is enabled */
if (options.debug) console.log('Connected!');
isopen = true;
}
/* When a message from the server is received ... */
socket.onmessage = function(e) {
/* Reset wait time- make sure this matches the value set earlier */
wait = 5;
if (options.debug) console.log('Rx Successful! : ' + e.data);
/* Get the data from the message */
var payload = JSON.parse(e.data);
}
/* when sending data to the server ... */
socket.onsend = new function(e) {
if (options.debug) console.log('Tx Successful!');
}
/* When socket connection closes... */
socket.onclose = function(e) {
if (options.debug) console.log('Disconnected!');
isopen = false;
/* Clear socket */
socket = null;
}
/* When an error occurs- aka an unplanned disconnect... */
socket.onerror = function(e) {
if (options.debug) console.error('Socket error! ', e.message, ' Closing socket!');
/* Close connection and retry to connect */
disconnect(true);
}
}
}
/* This function attempts to reconnect without filling the network with spam */
function reconnect() {
/* if not connected ... */
if (!isopen || socket.readyState == 3) {
if (options.debug) console.log("Disconnected: Trying to reconnect in ", wait, " seconds.");
/* Attempt to reconnect in 'wait' seconds */
setTimeout(function() { connect(); }, wait * 1000 );
/* Increase wait time up to a minute between consecutive tries*/
if (wait <= 60) wait *= 2;
}
}Nice spot.