0

我有一个代码可以将表单中的任何用户输入订阅到 MQTT 代理。一切正常,数据被发布并显示在代理中。但是,我不能订阅子主题。

这是代码的 MQTT 部分:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://www.hivemq.com/demos/websocket-client/js/mqttws31.js" ></script>
    <script type="text/javascript">

        //Create a new Client object with your broker's hostname, port and your own clientId
        var client = new Messaging.Client("broker.mqttdashboard.com", 8000, "clientId-9HL7JvIJNb");
        var publish = function (payload, topic, qos) {
                //Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
                var message = new Messaging.Message(payload);
                message.destinationName = "sssmarthome/";
                message.qos = qos;
                client.send(message);
        }

        var options = {

             //connection attempt timeout in seconds
             timeout: 3,

             //Gets Called if the connection has successfully been established
             onSuccess: function () {
                 alert("Connected");
             },

             //Gets Called if the connection could not be established
             onFailure: function (message) {
                 alert("Connection failed: " + message.errorMessage);
             }

         };

        //Attempt to connect
        client.connect(options);

        </script>

如您所见,目标名称是“sssmarthome/”,这是我可以订阅的唯一主题。这是表单代码:

<form id="settingsForm" method="GET">
                            Temperature(low, °C): <input type="text" name="templow" id="templow">
                            Temperature(high, °C): <input type="text" name="temphigh" id="temphigh">
                            Humidity(low, %): <input type="text" name="humlow" id="humlow">
                            Humidity(high, %): <input type="text" name="humhigh" id="humhigh"><br>
                            <input type="button" value="Change" onclick="submitForm()">
                            </form>
                            <script>
                            function submitForm(){
                                publish(document.getElementById("templow").value, 'sssmarthome/templow',2)
                                publish(document.getElementById("temphigh").value, 'sssmarthome/temphigh',2)
                                publish(document.getElementById("humlow").value, 'sssmarthome/humlow',2)
                                publish(document.getElementById("humhigh").value, 'sssmarthome/humhigh',2)

所以每个输入都有自己的子主题。但是,如果我去网站填写表格,这就是我在经纪人那里得到的:

HiveMQ 代理

正如您在图片上看到的,它没有显示子主题的名称,只显示了主要主题。我尝试订阅代理中的子主题,但不会发布任何内容,唯一有效的主题是默认目标名称。知道如何能够订阅主题并在其上发布实际的内容,以及正确的主题和子主题吗?谢谢!

4

1 回答 1

0

Just to put this out here as an answer, per my comment:

The code below this line:

var publish = function(payload, topic, qos)

never uses 'topic'....so since you set message.destinationName = "sssmarthome/"; and then publish, your GUI is showing just that. The outcome is exactly what your code is asking for.

The fix is to change that one line to:

message.destinationName = "sssmarthome/" + topic;

This is assuming that topic is a string.

于 2020-12-03T14:50:16.990 回答