Schwebe-Animation

Verwendung von Websocket zum Senden und Empfangen von Nachrichten auf Edge Gateways der EG-Serie

Partners who use EG series edge computing gateways often encounter the need to use websocket to send and receive messages, WEBSOCKET is a protocol on TCP that can help users realize information push, using WEBSOCKET needs to create a connection first, and then the client and the server will be able to send data at any time, and the server can actively push the data to the client without the client constantly sending requests, realizing true two-way communication. This protocol is often used in real-time data monitoring in the Internet of Things (IoT), which allows IoT applications to be more interactive and real-time.

Today, let’s learn how the EG Series Edge Computing Gateway use websocket to send and receive messages! Before we get started, we need to make sure that we have done the preparatory work, including the connection and configuration of the Edge-Gateway, and familiarize ourselves with Node-RED.

How to use websocket to send and receive messages?

Die WEBSOCKET-Funktion ist entsprechend der Datenflußrichtung in zwei Knoten unterteilt: WEBSOCKET IN gibt die vom Gateway empfangenen Daten an und WEBSOCKET OUT zeigt die vom Gateway gesendeten Daten an.

Zweitens fungiert das Gateway im Abhörmodus als WEBSOCKET-Server und das Gateway im Verbindungsmodus als WEBSOCKET-Client.

Eingabe

msg.payload
# Standardmäßig wird msg.payload über das eingestellte WEBSOCKET gesendet. Wenn die an diesem Knoten ankommende Nachricht vom WEBSOCKET IN-Knoten stammt, wird die Nachricht an den ursprünglichen Client zurückgeschickt. Andernfalls wird die Nachricht an alle Clients gesendet.

Ausgabe

msg.payload
# Standardmäßig sind die von WEBSOCKET empfangenen Daten in msg.payload enthalten.

Funktionen

1. Abhören

Listening mode, WEBSOCKET works in server mode, it will create WEBSOCKET server according to the specified path.

2. Verbindung

Im Verbindungsmodus arbeitet WEBSOCKET im Client-Modus und initiiert eine WEBSOCKET-Verbindung zur eingestellten URL.

Beispiel

1. Builds Server to connect the Client.

WEBSOCKET builds its own Server and Client interconnection/use websocket to send and receive messages

[{"id":"13410716.69c9d9","type":"websocket in","z":"b5e5e7b9b71494ea","name":"","server":"89db22b6.9aa36","client":"","x":500,"y":380,"wires":[["c2541f10.59544"]]},{"id":"c2541f10.59544","type":"debug","z":"b5e5e7b9b71494ea","name":"调试","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":380,"wires":[]},{"id":"6788839e.04576c","type":"inject","z":"b5e5e7b9b71494ea","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hello world","payloadType":"str","x":340,"y":320,"wires":[["438c232a.06c2cc"]]},{"id":"438c232a.06c2cc","type":"websocket out","z":"b5e5e7b9b71494ea","name":"","server":"","client":"63620788.bda128","x":560,"y":320,"wires":[]},{"id":"c88f97a9.4410f8","type":"comment","z":"b5e5e7b9b71494ea","name":"WEBSOCKET自建Server和Client互连","info":"","x":410,"y":260,"wires":[]},{"id":"759c0b2b.8a0484","type":"websocket in","z":"b5e5e7b9b71494ea","name":"","server":"","client":"1d80bd86.93f372","x":560,"y":500,"wires":[["1f7a7454.cb65ec"]]},{"id":"1f7a7454.cb65ec", "type": "debug", "z": "b5e5e7b9b71494ea", "name": "调试", "active":true, "tosidebar":true, "console":false, "tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":500,"wires":[]},{"id":"aa2fe781.e92b28","type":"inject","z":"b5e5e7b9b71494ea","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"goodbye","payloadType":"str","x":340,"y":440,"wires":[["f8bdbc9b.d82dd"]]},{"id":"f8bdbc9b.d82dd","type":"websocket out","z":"b5e5e7b9b71494ea","name":"","server":"40bd4295.3e4ecc","client":"","x":500,"y":440,"wires":[]},{"id":"89db22b6.9aa36","type":"websocket-listener","path":"/ws1","wholemsg":"false"},{"id":"63620788.bda128","type":"websocket-client","path":"ws://localhost:1880/ws1","tls":"","wholemsg":"false","hb":"0"},{"id":"1d80bd86.93f372","type":"websocket-client","path":"ws://localhost:1880/ws2","tls":"","wholemsg":"false"},{"id":"40bd4295.3e4ecc","type":"websocket-listener","path":"/ws2","wholemsg":"false"}]

2. Builds Server to send and receive information.

WEBSOCKET self-built Server implementation of what received what back/use websocket to send and receive messages

[{"id":"9c86d286f135539e","type":"group","z":"b5e5e7b9b71494ea","name":"服务端-数据原路返回","style":{"label":true},"nodes":["4cb19ae6d5bc97bb","06f56bbc8337db06","587f22bd305d76ce"],"x":314,"y":219,"w":492,"h":82},{"id":"4cb19ae6d5bc97bb","type":"websocket in","z":"b5e5e7b9b71494ea","g":"9c86d286f135539e","name":"","server":"89db22b6.9aa36","client":"","x":400,"y":260,"wires":[["06f56bbc8337db06"]]},{"id":"06f56bbc8337db06","type":"function","z":"b5e5e7b9b71494ea","g":"9c86d286f135539e","name":"函数计算 1","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":260,"wires":[["587f22bd305d76ce"]]},{"id":"587f22bd305d76ce","type":"websocket out","z":"b5e5e7b9b71494ea","g":"9c86d286f135539e","name":"","server":"89db22b6.9aa36","client":"","x":720,"y":260,"wires":[]},{"id":"89db22b6.9aa36","type":"websocket-listener","path":"/ws1","wholemsg":"false"},{"id":"d07e89f720b33486","type":"group","z":"b5e5e7b9b71494ea","name":"客户端","style":{"label":true},"nodes":["1af13bea201b6062","c327cadb0afa0aef","33bf2db84056de7c","cc47f6b7286a57d1"],"x":334,"y":399,"w":472,"h":142},{"id":"1af13bea201b6062","type":"websocket out","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"","server":"","client":"63620788.bda128","x":660,"y":440,"wires":[]},{"id":"c327cadb0afa0aef","type":"inject","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":430,"y":440,"wires":[["1af13bea201b6062"]]},{"id":"33bf2db84056de7c","type":"websocket in","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"","server":"","client":"63620788.bda128","x":480,"y":500,"wires":[["cc47f6b7286a57d1"]]},{"id":"cc47f6b7286a57d1","type":"debug","z":"b5e5e7b9b71494ea","g":"d07e89f720b33486","name":"调试 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":500,"wires":[]},{"id":"63620788.bda128","type":"websocket-client","path":"ws://localhost:1880/ws1","tls":"","wholemsg":"false","hb":"0"}]

All is today’s sharing, anyway I hope that through this article, readers can be clear about how to use websocket to send and receive messages on EG Series Edge Gateways.

IOTRouter Technology Co., Ltd. combines traditional industry infrastructure with new technologies such as IoT, edge computing, AI, and provides the edge gateway, IOT data center, Industrial Computer, remote IO, AI edge box, and other products and solutions for the industry digital transformation integrators.

Neueste Artikel

Kontakt