使用MQTT (paho) + activeMQ接收消息的正确方法是什么?

How is the correct way to receive messages using MQTT (paho) + activeMQ?

本文关键字:消息 方法 是什么 MQTT paho activeMQ 使用      更新时间:2023-09-26

我有一个Java Swing应用程序,使用activeMQ 5.9.1与主题和队列。

现在,我的意图是将该swing应用程序迁移到web,所以我正在使用activeMQ + MQTT (paho) javascript库进行一些验证。

我已经在activemq.xml中启用了:

<transportConnector name="mqtt+ws" uri="ws://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600&amp;transport.defaultKeepAlive=30000"/>

我用MQTT (paho - http://eclipse.org/paho/clients/js/)实现了一些示例,用于收听一些主题。

function ramdomID(length) {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for(var i=0; i<length; i++) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
}
var client = new Messaging.Client('192.168.240.17', 1883, ramdomID(20));
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
    console.log("onConnect");
    client.subscribe("/KeepAlive");
}
function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0) {
        console.log("onConnectionLost: "+responseObject.errorMessage);
    }
}
function onMessageArrived(message) {
    console.log("onMessageArrived: "+message.payloadString);
}

正确接收到对主题的响应,但恢复的消息是Java String对象引用:

onMessageArrived: ﭭsr<com.my.project.bp.jms.MyImplementedMessageLmessagetLjava/lang/String;Ltypeq~xppt  KeepAlivey 

有人知道正确的方式来接收友好的消息,如果一个java应用程序写在activeMQ主题直接?

如果你想从ActiveMQ转到浏览器,看看在ActiveMQ中只启用Websockets并使用STOMP over Websockets javascript库。消息的主体将是一个友好的文本对象。在网页中显示动态数据的最佳方式