简体   繁体   English

如何使用MQTT(Paho)+ activeMQ接收消息的正确方法?

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

I have a Java Swing application that uses activeMQ 5.9.1 with topics and queues. 我有一个Java Swing应用程序,它使用带有主题和队列的activeMQ 5.9.1。

Now, my intention is to migrate that swing application to web, so I'm making some proves using activeMQ + MQTT (paho) javascript library. 现在,我的意图是将那个swing应用程序迁移到Web,因此我使用activeMQ + MQTT(paho)JavaScript库进行了证明。

I have enabled that in activemq.xml: 我已经在activemq.xml中启用了它:

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

And I have implemented some examples with MQTT (paho - http://eclipse.org/paho/clients/js/ ) for listening some topics. 我已经使用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);
}

The responses to a topic are received correctly, but the recovered message is a Java String object reference: 可以正确接收对主题的响应,但是恢复的消息是Java String对象引用:

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

Anyone knows the correct way to receive a friendly message if a java application writes on activeMQ topic directly? 如果Java应用程序直接在activeMQ主题上编写,那么有人知道接收友好消息的正确方法吗?

If you want to go from ActiveMQ to a browser, take a look at enabling just Websockets within ActiveMQ and using the STOMP over Websockets javascript library. 如果要从ActiveMQ转到浏览器,请看一下在ActiveMQ中仅启用Websocket并使用Stomp over Websockets javascript库。 The body of the message will be a friendly text object. 邮件的正文将是一个友好的文本对象。 The links are at my answer here: Best way to display dynamic data in a webpage 这些链接是我的答案: 在网页中显示动态数据的最佳方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM