1
我有一個使用主題和隊列的activeMQ 5.9.1的Java Swing應用程序。如何使用MQTT(paho)+ activeMQ接收消息的正確方法?
現在,我的意圖是將該應用程序遷移到Web,所以我正在使用activeMQ + MQTT(paho)JavaScript庫進行一些證明。
我已經啓用,在activemq.xml中:
<transportConnector name="mqtt+ws" uri="ws://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600&transport.defaultKeepAlive=30000"/>
而且我已經實現了與MQTT(PAHO - http://eclipse.org/paho/clients/js/)一些例子聽一些議題。
function ramdomID(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
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主題直接?
感謝您的答覆。但是我通過Websockets使用MQTT。我發現我的錯誤:在服務器端,我有一些使用JMS的函數,他們使用javax.jms.ObjectMessage對象來編寫主題。將該對象更改爲javax.jms.TextMessage並在JSON中設置消息一切正常! –