在Node-Red中处理HTTP请求中的MQTT通信 [英] Handling MQTT communication inside HTTP request in Node-Red

查看:1308
本文介绍了在Node-Red中处理HTTP请求中的MQTT通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Node-Red中,我希望能够在http输入节点和响应节点之间使用MQTT发送消息并等待其响应。我认为一张图片更能说明这一点:

In Node-Red, I would like to be able to send a message and wait for its response using MQTT, in between a http input node and a response one. I think a picture is more relevant to explain this:

问题是http响应节点必须以某种方式直接链接到输入,所以我想在收到http输入后将整个消息保存在上下文中,然后在发送响应之前将其恢复。问题是,显然,消息的结构是循环的,因此我无法直接将其转换为字符串,以将其存储在上下文中。

The problem is an http response node has to be directly linked to an input one somehow, so I thought of saving the whole message in context after receiving an http input, then restore it before sending the response. The problem is that appearently, the structure of the message is circular, hence I can't directly convert it to a string, to store it in context.

我是尝试了不同的方法来解决这个问题(我看过这个: JSON.stringify,避免TypeError:将循环结构转换为JSON 并尝试使用circular-json模块,这两个模块都不起作用。)

I've tried different things to solve this issue (I had a look at this: JSON.stringify, avoid TypeError: Converting circular structure to JSON and I tried using the circular-json module, which both don't work).

你有没有想过这个问题的其他解决方案?

Do you think of any other solution to this problem?

谢谢!

推荐答案

这有点打破了MQTT的异步性质,而Pub / Sub可以使用它不是它的优点。

This is somewhat breaking the asynchronous nature of MQTT, while Pub/Sub can be used it's not it's strong point.

你必须考虑如果有的话会发生什么没有响应消息,因为无法知道其他MQTT客户端是否已连接。

You have to consider what happens if there is no response message, since there is no way to know if the other MQTT client is connected.

HTTP请求来自何处?如果它来自一个网页,那么你就可以使用MQTT over Websockets直接发出请求,或者使用普通的Websockets,从而不再需要HTTP调用。

Where is the HTTP request coming from? If it's from a web page then you could use MQTT over Websockets to make the request directly or plain Websockets , removing the need for the HTTP call all together.

说完了您应该能够在上下文中存储 msg 对象,不需要将其转换为字符串,因为默认上下文存储引擎只是一个内存对象库。因此,只需将对象添加到上下文中,并在响应进入时检索它。

Having said that you should be able to store the msg object in the context, there is no need to convert it to a string as the default context storage engine is just a in memory object store. So just add object to the context and retrieve it when the response comes in.

您可以使用触发器节点执行某些操作来处理超时问题。

You may be able to do something with the trigger node to deal with the time out issue.

这篇关于在Node-Red中处理HTTP请求中的MQTT通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆