新闻中心

PRESS CENTER 纵横智控
你的位置: 首页 新闻 产品资讯
纵横智控

【入门教程】EG系列边缘网关如何使用websocket收发信息

2024-05-28 11:26:26 阅读: 发布人:纵横智控

使用EG系列边缘计算网关的小伙伴,常常会遇到需要使用websocket来收发信息的情况,websocket是一种在TCP上的协议,可以帮助用户实现信息推送,使用websocket需要先创建连接,然后客户端与服务器就能随时发送数据了,服务器可以主动将数据推送给客户端而不需要客户端不断发送请求,实现真正的双向通信。在物联网的实时数据监控中,就常常用到这个协议,它让物联网应用可以更加具有交互性和实时性。

今天,我们一起来学习一下,EG系列边缘计算网关是如何使用这个协议实现信息收发的吧!在正式开始前,我们需要确保已经做好了准备工作,包括边缘网关的连接与配置,并且熟悉使用node-red进行流程编辑等,具体可参考:快速入门。然后,让我们一起进入今天的正题。


概述

WEBSOCKET功能根据数据流向分为两个节点,分别是:WEBSOCKET IN表示网关收到的数据,WEBSOCKET OUT表示网关发送的数据。

监听模式网关作为WEBSOCKET服务端,连接模式网关作为WEBSOCKET客户端。

输入

msg.payload
#默认情况下,msg.payload将通过设置好的WEBSOCKET发送。如果到达此节点的消息来自WEBSOCKET IN节点,则该消息将发送回原客户端。否则,该消息将广播给所有客户端。

输出

msg.payload
#默认情况下,从WEBSOCKET接收的数据存在msg.payload中

功能

1. 监听

监听模式,WEBSOCKET工作在服务器模式,会根据指定的路径创建WEBSOCKET服务器。

2. 连接

连接模式,WEBSOCKET工作在客户端模式,会向设定的URL发起WEBSOCKET连接。

示例

1. WEBSOCKET自建Server和Client互连


边缘计算网关/物联网平台/网关模块/纵横智控


[{"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. WEBSOCKET自建Server实现收到什么回什么


边缘计算网关/物联网平台/网关模块/纵横智控


[{"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"}]


以上就是今天的分享,希望通过本文,读者能了解边缘计算网关是如何通过websocket协议节点进行信息收发的,如果有不清楚的地方欢迎随时联系我们!


纵横智控将传统行业基础设施与物联网、边缘计算、AI等新技术相结合,为行业数字化转型集成商提供边缘网关、IOT数据中台、HMI、远程IO、AI边缘盒子等产品及解决方案,提质增效、优化流程,推动行业数字化转型

热门产品