Web-1/AJAX/AJAX-polling
Naar navigatie springen
Naar zoeken springen
[{"id":"98e98fd0.7d572","type":"tab","label":"AJAX polling","disabled":false,"info":""},{"id":"9e3dc12b.3ea7d","type":"http in","z":"98e98fd0.7d572","name":"","url":"/login","method":"get","upload":false,"swaggerDoc":"","x":100,"y":80,"wires":[["154404a0.e5499b"]]},{"id":"593dd09b.bc9be","type":"http in","z":"98e98fd0.7d572","name":"","url":"/api/users","method":"get","upload":false,"swaggerDoc":"","x":120,"y":200,"wires":[["d60de6b5.b43338"]]},{"id":"c55040a1.6898b","type":"http response","z":"98e98fd0.7d572","name":"","statusCode":"","headers":{},"x":750,"y":80,"wires":[]},{"id":"992c02d1.fb30c","type":"http response","z":"98e98fd0.7d572","name":"JSON","statusCode":"","headers":{"content-type":"application/json"},"x":750,"y":200,"wires":[]},{"id":"deb8070.cda98f8","type":"template","z":"98e98fd0.7d572","name":"Page-template","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<!doctype html>\n<html>\n <head>\n <title>Login</title>\n <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script> \n </head>\n <body>\n <h3>Login</h3>\n <form id=\"loginform\">\n Login:\n <input type=\"text\" name=\"username\">\n <button type=\"submit\">Submit</button>\n </form>\n <div>\n active users:\n <div id=\"users\"></div>\n </div>\n <script>\n {{{clientscript}}}\n </script>\n </body>\n</html>","output":"str","x":600,"y":80,"wires":[["c55040a1.6898b"]]},{"id":"edeeb73a.2dddb8","type":"template","z":"98e98fd0.7d572","name":"clientscript","field":"clientscript","fieldType":"msg","format":"javascript","syntax":"mustache","template":"var users = document.getElementById(\"users\");\nvar login = document.getElementById(\"loginform\");\n\nlogin.onsubmit= function (event) {\n event.preventDefault();\n $.post(\"/login-form\", $(\"#loginform\").serialize(), function (data) {\n users.innerHTML = JSON.stringify(data);\n login.username.value = \"\";\n });\n};\n\nfunction getUsers() {\n $.get(\"api/users\", function (data) {\n users.innerHTML = JSON.stringify(data);\n });\n}\n\ngetUsers();\n\nwindow.setInterval(getUsers, 60000); // timer in msec","output":"str","x":430,"y":80,"wires":[["deb8070.cda98f8"]]},{"id":"154404a0.e5499b","type":"function","z":"98e98fd0.7d572","name":"process-login","func":"\nreturn msg;","outputs":1,"noerr":0,"x":260,"y":80,"wires":[["edeeb73a.2dddb8"]]},{"id":"128f58af.1c2337","type":"http in","z":"98e98fd0.7d572","name":"","url":"/login-form","method":"post","upload":false,"swaggerDoc":"","x":120,"y":140,"wires":[["bf279a86.9d3bb8"]]},{"id":"bf279a86.9d3bb8","type":"function","z":"98e98fd0.7d572","name":"process-login-form","func":"var username = msg.payload.username || \"\";\nvar users = flow.get(\"users\") || [];\nif (!users.includes(username) && (username !== \"\")) {\n users.push(username);\n}\nflow.set(\"users\", users);\nmsg.user = username;\nmsg.cookies = {user: username};\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":140,"wires":[["d60de6b5.b43338"]]},{"id":"d60de6b5.b43338","type":"function","z":"98e98fd0.7d572","name":"get users","func":"msg.payload = flow.get(\"users\") || [];\nreturn msg;","outputs":1,"noerr":0,"x":500,"y":200,"wires":[["4789196b.adbd28"]]},{"id":"64e6bfdb.0df81","type":"inject","z":"98e98fd0.7d572","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":260,"wires":[["f8443346.f1f67"]]},{"id":"f8443346.f1f67","type":"function","z":"98e98fd0.7d572","name":"no-users","func":"flow.set(\"users\", []);\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":260,"wires":[[]]},{"id":"4789196b.adbd28","type":"json","z":"98e98fd0.7d572","name":"","pretty":false,"x":630,"y":200,"wires":[["992c02d1.fb30c"]]}]