简体   繁体   English

如何将jms消息发送到activeMQ并使用mqttJS正确解码javascript

[英]How to send a jms message to activeMQ and decode it in javascript with mqttJS properly

I have a java backend, where I can send messages to topics via 我有一个java后端,我可以通过它发送消息到主题

jmsTemplate.convertAndSend("topic", "Hello World!");

In my javascript frontend I use mqttJS to connect to activeMQ and recieve the massage: 在我的javascript前端,我使用mqttJS连接到activeMQ并接受按摩:

    let mqtt = require('mqtt')
    let options ={
        clientId:"test",
        username:"username",
        useSSL: true,
        password:"password",
        clean:true};
    let client  = mqtt.connect(
        'wss://someUrl.com:61619',
        options);

    client.on('connect', function () {
        client.subscribe('myTopic', function (err) {
            if (!err) {
                console.log("successfully connected to myTopic'");
            }
        })
    });

    client.on('message', function (topic, message) {
        console.log(message.toString());
    });

The message I recieve from the backend is something like this: 我从后端收到的消息是这样的:

S�A S�)�x-opt-jms-destQ�x-opt-jms-msg-typeQ Ss�   f    
�/ID:myID@�topic://myTopic@@@@�  j��< St�
e Sw�  Hello World!

My message "Hello World!" 我的留言“Hello World!” is there. 在那儿。 But also a bunch of unreadable into, I would guess from the header. 但也有一堆难以理解的,我会从头部猜测。

I tried different MessageConverters on the backend side and different parsers on the frontend side. 我在后端尝试了不同的MessageConverters,在前端尝试了不同的解析器。 Nothing works. 什么都行不通。

What do I need to do, to get just "Hello World!" 我需要做什么才能获得“Hello World!” as message? 作为消息? Or is there a better way to send the message, using jms, which is required. 或者是否有更好的方法来发送消息,使用jms,这是必需的。

If tou are using mqtt 3.0.0 you should add extra parameters: 如果tou正在使用mqtt 3.0.0,你应该添加额外的参数:

If you are connecting to a broker that supports only MQTT 3.1 (not 3.1.1 compliant), you should pass these additional options: 如果要连接到仅支持MQTT 3.1(不符合3.1.1)的代理,则应传递以下附加选项:

 { protocolId: 'MQIsdp', protocolVersion: 3 } 

Update: 更新:

After switching to ActiveMQ Artemis, with supports JMS 2, it works perfectly fine without any regex. 切换到ActiveMQ Artemis后,支持JMS 2,它完全正常,没有任何正则表达式。

Old post: 老帖子:

Since I didn't find any solution to filter the message's body or to send the message without a header (related, unanswered question is here: How to send plain text JmsMessage with empty header ) the solution was to send an JSON object and filter on the JSON syntax in the frontend with a regex. 由于我没有找到任何解决方案来过滤消息的正文或发送没有标题的消息(相关的,未解答的问题在这里: 如何发送带有空标题的纯文本JmsMessage )解决方案是发送一个JSON对象并过滤正则表达式前端的JSON语法。

Backend: 后端:

private void sendHelloWorld() {
    Map<String, String> subPayload = new HashMap<>();
    subPayload.put("test1", "value2");
    subPayload.put("test2", "value3");
    Map<String, Object> payload = new HashMap<>();
    payload.put("message", "Hello World!");
    payload.put("context", "Something");
    payload.put("map", subPayload);
    jmsTemplate.setMessageConverter(new MappingJackson2MessageConverter());
    jmsTemplate.convertAndSend( "notification/prediction", payload );
}

Frontend: 前端:

client.on('message', function (topic, message, packet) {
    const regex = /\{(.*)\}$/g;
    const match = message.toString().match(regex);
    if(null === match) {
        console.error("Could not parse message");
        console.log('message', message.toString());
    }
    const json = JSON.parse(match[0]);
    console.log('message', json);
});

The result would be: 结果将是:

{
  "message":"Hello World!", 
  "context":"Something",
  "map": {
     "test1":"value2",
     "test2":"value3"
   }
}

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

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