Web-1/Websockets/Nodered-simple-chat

Uit Inf2019
< Web-1‎ | Websockets
Versie door Eelco (overleg | bijdragen) op 7 nov 2017 om 22:52 (1 versie geïmporteerd)
Naar navigatie springen Naar zoeken springen
[{"id":"9f649225.5b509","type":"tab","label":"Simpele chat","disabled":false,"info":""},{"id":"f5ea2879.bc2f88","type":"inject","z":"9f649225.5b509","name":"","topic":"","payload":"Server: Hello world!","payloadType":"str","repeat":"","crontab":"","once":false,"x":268,"y":98,"wires":[["65d5c1db.b97f9"]]},{"id":"5758b368.89d7dc","type":"websocket in","z":"9f649225.5b509","name":"","server":"d1c4a3ad.621f","client":"","x":251,"y":159,"wires":[["65d5c1db.b97f9","709b0224.36af0c"]]},{"id":"709b0224.36af0c","type":"debug","z":"9f649225.5b509","name":"","active":true,"console":"false","complete":"false","x":471,"y":211,"wires":[]},{"id":"5f94bde.55ea044","type":"websocket out","z":"9f649225.5b509","name":"","server":"d1c4a3ad.621f","client":"","x":671,"y":159,"wires":[]},{"id":"a58a4819.693408","type":"http in","z":"9f649225.5b509","name":"","url":"/","method":"get","upload":false,"swaggerDoc":"","x":100,"y":300,"wires":[["8c13fc17.e526d"]]},{"id":"6d188390.59015c","type":"http response","z":"9f649225.5b509","name":"","statusCode":"","headers":{},"x":770,"y":300,"wires":[]},{"id":"afed89e3.c11608","type":"template","z":"9f649225.5b509","name":"Page-html","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n  <head>\n\t<title>Simple chat</title>\n  </head>\n  <body>\n  \n    <h3>Simple chat</h3>\n    {{^user}}\n    <p>Please <a href=\"/login\">login</a>.</p>\n    {{/user}}\n    {{#user}}\n    <p>\n      Welcome {{user}}! <br>\n      <a href=\"/login\">switch to other user</a>\n    </p>\n    <div id=\"logdiv\">{{{chatlog}}}</div>\n    <form id=\"chatform\">\n      {{user}}:\n      <input type=\"text\" name=\"chattext\"></input>\n      <button type=\"submit\">Send</button>\n    </form>\n    {{/user}}\n    <script>\n        {{{clientscript}}}\n    </script>\n  </body>\n</html>\n","output":"str","x":630,"y":300,"wires":[["6d188390.59015c"]]},{"id":"26e0587.86006a8","type":"template","z":"9f649225.5b509","name":"clientscript","field":"clientscript","fieldType":"msg","format":"javascript","syntax":"mustache","template":"var socket = new WebSocket(\"wss://\" + location.host + \"/ws/test\");\nvar logdiv = document.getElementById(\"logdiv\");     \nvar chatform = document.getElementById(\"chatform\");\n\nsocket.onopen = function (event) {\n    logdiv.innerHTML = logdiv.innerHTML + \"<p>[socket open]</p>\";    \n};\n\nsocket.onmessage = function (event) {\n    logdiv.innerHTML = logdiv.innerHTML + (\"<p>\" + event.data + \"</p>\");\n};\n\nchatform.onsubmit = function (event) {\n    socket.send(\"{{user}}: \" + chatform.chattext.value);\n    event.preventDefault();\n    chatform.chattext.value = \"\";\n}","output":"str","x":477,"y":300,"wires":[["afed89e3.c11608"]]},{"id":"65d5c1db.b97f9","type":"function","z":"9f649225.5b509","name":"kill session","func":"var chatlog = flow.get(\"chatlog\") || \"\";\nflow.set(\"chatlog\", chatlog + \"<p>\" + msg.payload + \"</p>\");\ndelete msg._session; // for message broadcast\nreturn msg;","outputs":1,"noerr":0,"x":471,"y":159,"wires":[["5f94bde.55ea044"]]},{"id":"8c13fc17.e526d","type":"function","z":"9f649225.5b509","name":"process-home","func":"var user = msg.req.cookies[\"user\"] || \"\";\n\nmsg.user = user;\nmsg.cookies = {user: user};\nmsg.chatlog = flow.get(\"chatlog\") || \"\";\nreturn msg;","outputs":1,"noerr":0,"x":281,"y":300,"wires":[["26e0587.86006a8"]]},{"id":"2c5cb506.03d1aa","type":"http in","z":"9f649225.5b509","name":"","url":"/login-form","method":"post","upload":false,"swaggerDoc":"","x":126.5,"y":372,"wires":[["ab3700f7.d8ea7"]]},{"id":"e8ec5cb6.f1777","type":"http in","z":"9f649225.5b509","name":"","url":"/login","method":"get","upload":false,"swaggerDoc":"","x":119.5,"y":438,"wires":[["b8ec830c.882e7"]]},{"id":"b8ec830c.882e7","type":"template","z":"9f649225.5b509","name":"login-html","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!doctype html>\n<html>\n  <head>\n      <title>Login</title>\n  </head>\n  <body>\n    <h3>Login</h3>\n    <p>Please enter your name.</p>\n    <div id=\"logdiv\">{{{chatlog}}}</div>\n    <form method=\"post\" action=\"login-form\" >\n      Your name:\n      <input type=\"text\" name=\"username\"></input>\n      <button type=\"submit\" \">Submit</button>\n    </form>\n  </body>\n</html>","output":"str","x":314.5,"y":439,"wires":[["c2644cd4.2036a"]]},{"id":"c2644cd4.2036a","type":"http response","z":"9f649225.5b509","name":"","statusCode":"","headers":{},"x":488.5,"y":439,"wires":[]},{"id":"ab3700f7.d8ea7","type":"function","z":"9f649225.5b509","name":"process-login-form","func":"var user = msg.payload.username || \"\";\nmsg.user = user;\nmsg.cookies = {user: user};\nmsg.chatlog = flow.get(\"chatlog\") || \"\";\nreturn msg;","outputs":1,"noerr":0,"x":330.5,"y":372,"wires":[["26e0587.86006a8"]]},{"id":"d1c4a3ad.621f","type":"websocket-listener","z":"","path":"/ws/test","wholemsg":"false"}]