-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple-web-socket.json
1 lines (1 loc) · 3.85 KB
/
simple-web-socket.json
1
[{"id":"225febda.fd75e4","type":"inject","z":"414549e2.074918","name":"Tick every 5 secs","topic":"test","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"x":150,"y":256,"wires":[["7fedb511.68b6bc"]]},{"id":"a1e5189e.ad0458","type":"websocket out","z":"414549e2.074918","name":"","server":"af3efbbf.7e5e38","client":"","x":571,"y":256,"wires":[]},{"id":"11de335c.154bbd","type":"http response","z":"414549e2.074918","name":"","x":542,"y":196,"wires":[]},{"id":"2b420191.a8947e","type":"http in","z":"414549e2.074918","name":"","url":"/simple","method":"get","swaggerDoc":"","x":173,"y":196,"wires":[["302710b8.14e88"]]},{"id":"302710b8.14e88","type":"template","z":"414549e2.074918","name":"Simple Web Page","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<!DOCTYPE HTML>\n<html>\n <head>\n <title>Simple Live Display</title>\n <script type=\"text/javascript\">\n var ws;\n var wsUri = \"ws:\";\n var loc = window.location;\n console.log(loc);\n if (loc.protocol === \"https:\") { wsUri = \"wss:\"; }\n // This needs to point to the web socket in the Node-RED flow\n // ... in this case it's ws/simple\n wsUri += \"//\" + loc.host + loc.pathname.replace(\"simple\",\"ws/simple\");\n\n function wsConnect() {\n console.log(\"connect\",wsUri);\n ws = new WebSocket(wsUri);\n //var line = \"\"; // either uncomment this for a building list of messages\n ws.onmessage = function(msg) {\n var line = \"\"; // or uncomment this to overwrite the existing message\n // parse the incoming message as a JSON object\n var data = msg.data;\n //console.log(data);\n // build the output from the topic and payload parts of the object\n line += \"<p>\"+data+\"</p>\";\n // replace the messages div with the new \"line\"\n document.getElementById('messages').innerHTML = line;\n //ws.send(JSON.stringify({data:data}));\n }\n ws.onopen = function() {\n // update the status div with the connection status\n document.getElementById('status').innerHTML = \"connected\";\n //ws.send(\"Open for data\");\n console.log(\"connected\");\n }\n ws.onclose = function() {\n // update the status div with the connection status\n document.getElementById('status').innerHTML = \"not connected\";\n // in case of lost connection tries to reconnect every 3 secs\n setTimeout(wsConnect,3000);\n }\n }\n \n function doit(m) {\n if (ws) { ws.send(m); }\n }\n </script>\n </head>\n <body onload=\"wsConnect();\" onunload=\"ws.disconnect();\">\n <font face=\"Arial\">\n <h1>Simple Live Display</h1>\n <div id=\"messages\"></div>\n <button type=\"button\" onclick='doit(\"click\");'>Click to send message</button>\n <hr/>\n <div id=\"status\">unknown</div>\n </font>\n </body>\n</html>\n","x":370,"y":196,"wires":[["11de335c.154bbd"]]},{"id":"7fedb511.68b6bc","type":"function","z":"414549e2.074918","name":"format time nicely","func":"msg.payload = Date(msg.payload).toString();\nreturn msg;","outputs":1,"noerr":0,"x":359,"y":256,"wires":[["a1e5189e.ad0458"]]},{"id":"b396c340.14c1a","type":"websocket in","z":"414549e2.074918","name":"","server":"af3efbbf.7e5e38","client":"","x":358,"y":308,"wires":[["ad353ccc.9851"]]},{"id":"ad353ccc.9851","type":"debug","z":"414549e2.074918","name":"","active":true,"console":"false","complete":"false","x":548,"y":308,"wires":[]},{"id":"af3efbbf.7e5e38","type":"websocket-listener","z":"414549e2.074918","path":"/ws/simple","wholemsg":"false"}]