Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active September 21, 2016 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BananaAcid/bed6f676d088670d7e1bc298943338b5 to your computer and use it in GitHub Desktop.
Save BananaAcid/bed6f676d088670d7e1bc298943338b5 to your computer and use it in GitHub Desktop.
Example: basic TCP messaging

A simple TCP-server setup.

[Using Node-RED]

Easy to test your clients used. Like Telnet and others.

Contains:

  • Server
  • Client
  • Proxy (for clients not terminating their messages with \n)

and a button to open a telnet session in windows

[{"id":"d0ed337.b37dbd","type":"tcp in","z":"2128a3cc.c38a5c","name":"Server holding connections at :1025","server":"server","host":"","port":"1025","datamode":"stream","datatype":"utf8","newline":"\\n","topic":"text","base64":false,"x":160,"y":120,"wires":[["223f744a.5e373c","337733.66a118ce"]]},{"id":"c4fce4d0.df1838","type":"tcp out","z":"2128a3cc.c38a5c","host":"","port":"","beserver":"reply","base64":false,"end":false,"name":"reply: all open connections will be messaged","x":1170,"y":120,"wires":[]},{"id":"94d4c7bc.6788f8","type":"debug","z":"2128a3cc.c38a5c","name":"show incomming text at the debug tab","active":true,"console":"false","complete":"payload","x":470,"y":220,"wires":[]},{"id":"a6a1d927.3f5878","type":"inject","z":"2128a3cc.c38a5c","name":"button: create a msg","topic":"","payload":"Hello from Node-RED server.","payloadType":"str","repeat":"","crontab":"","once":false,"x":840,"y":40,"wires":[["157cdc82.434e43"]]},{"id":"e322a7da.923938","type":"debug","z":"2128a3cc.c38a5c","name":"show incomming text at the debug tab","active":true,"console":"false","complete":"payload","x":350,"y":660,"wires":[]},{"id":"f0eed7a2.89f068","type":"inject","z":"2128a3cc.c38a5c","name":"button: create a msg","topic":"","payload":"Hello from Node-RED client.","payloadType":"str","repeat":"","crontab":"","once":false,"x":840,"y":480,"wires":[["198755f6.e33e0a"]]},{"id":"4ace7c3a.1379c4","type":"tcp out","z":"2128a3cc.c38a5c","host":"","port":"","beserver":"reply","base64":false,"end":false,"name":"reply: the server will get messaged","x":1140,"y":560,"wires":[]},{"id":"e6e274df.2cf878","type":"tcp in","z":"2128a3cc.c38a5c","name":"Node-RED Client","server":"client","host":"localhost","port":"1025","datamode":"stream","datatype":"utf8","newline":"\\n","topic":"text2","base64":false,"x":100,"y":560,"wires":[["43a2a0dc.3d1ff","65bebabe.906914"]]},{"id":"43a2a0dc.3d1ff","type":"template","z":"2128a3cc.c38a5c","name":"\"CLIENT GOT:\" +","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"CLIENT GOT:\n{{payload}}","x":290,"y":620,"wires":[["e322a7da.923938"]]},{"id":"223f744a.5e373c","type":"template","z":"2128a3cc.c38a5c","name":"\"SERVER GOT:\" +","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"SERVER GOT:\n{{payload}}","x":410,"y":180,"wires":[["94d4c7bc.6788f8"]]},{"id":"337733.66a118ce","type":"template","z":"2128a3cc.c38a5c","name":"modify received msg to be send back","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"You were sending: {{payload}}\n","x":670,"y":120,"wires":[["c4fce4d0.df1838"]]},{"id":"7d0dd2c7.0d1bac","type":"comment","z":"2128a3cc.c38a5c","name":"Telnet client: open your command prompt, and type: `telnet localhost 1025` and start typing","info":"Telnet client: open your command prompt, and type: `telnet localhost 1025` and start typing","x":340,"y":740,"wires":[]},{"id":"40767085.d36f2","type":"tcp in","z":"2128a3cc.c38a5c","name":"Proxy: fixing client with no NL :1026","server":"server","host":"","port":"1026","datamode":"stream","datatype":"utf8","newline":"","topic":"text","base64":false,"x":160,"y":380,"wires":[["6e966bfa.8ad5d4"]]},{"id":"6e966bfa.8ad5d4","type":"template","z":"2128a3cc.c38a5c","name":"fix incomming message with a newline","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload}}\n","x":470,"y":380,"wires":[["7de967f0.d50a08"]]},{"id":"198755f6.e33e0a","type":"template","z":"2128a3cc.c38a5c","name":"+ newline","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload}}\n","x":1000,"y":480,"wires":[["4ace7c3a.1379c4"]]},{"id":"157cdc82.434e43","type":"template","z":"2128a3cc.c38a5c","name":"+ newline","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload}}\n","x":1000,"y":40,"wires":[["c4fce4d0.df1838"]]},{"id":"7de967f0.d50a08","type":"tcp request","z":"2128a3cc.c38a5c","server":"localhost","port":"1025","out":"time","splitc":"0","name":"connect the Node-RED server","x":810,"y":380,"wires":[["e267c9ff.9750f8"]]},{"id":"e267c9ff.9750f8","type":"tcp out","z":"2128a3cc.c38a5c","host":"","port":"","beserver":"reply","base64":false,"end":false,"name":"reply: any answer back to the client","x":1140,"y":380,"wires":[]},{"id":"4a69acf7.609ab4","type":"inject","z":"2128a3cc.c38a5c","name":"button: open telnet to 1025 (windows)","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":890,"y":740,"wires":[["a2c48b6b.cabd08","b026565e.68d038"]]},{"id":"a2c48b6b.cabd08","type":"exec","z":"2128a3cc.c38a5c","command":"start","addpay":false,"append":"telnet localhost 1025","useSpawn":false,"timer":"","name":"start telnet","x":1130,"y":740,"wires":[[],[],[]]},{"id":"b026565e.68d038","type":"delay","z":"2128a3cc.c38a5c","name":"","pauseType":"delay","timeout":"1200","timeoutUnits":"milliseconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1140,"y":800,"wires":[["71897a7e.1b5d34"]]},{"id":"71897a7e.1b5d34","type":"exec","z":"2128a3cc.c38a5c","command":"msg","addpay":false,"append":"%username% \"start by pressing enter\"","useSpawn":false,"timer":"","name":"msg box","x":1120,"y":840,"wires":[[],[],[]]},{"id":"65bebabe.906914","type":"template","z":"2128a3cc.c38a5c","name":"no msg back, we do behave!","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"","x":620,"y":560,"wires":[["4ace7c3a.1379c4"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment