如何将jms消息发送到activeMQ并使用mqttJS正确解码javascript [英] How to send a jms message to activeMQ and decode it in javascript with mqttJS properly

查看:385
本文介绍了如何将jms消息发送到activeMQ并使用mqttJS正确解码javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java后端,我可以通过

I have a java backend, where I can send messages to topics via

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

在我的javascript前端中,我使用mqttJS连接到activeMQ并接受按摩:

In my javascript frontend I use mqttJS to connect to activeMQ and recieve the massage:

    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!

我的留言Hello World!在那儿。但也有一堆难以理解的,我会从头部猜测。

My message "Hello World!" is there. But also a bunch of unreadable into, I would guess from the header.

我在后端尝试了不同的MessageConverters,在前端尝试了不同的解析器。什么都行不通。

I tried different MessageConverters on the backend side and different parsers on the frontend side. Nothing works.

我需要做什么才能获得Hello World!作为消息?或者有更好的方法来发送消息,使用jms,这是必需的。

What do I need to do, to get just "Hello World!" as message? Or is there a better way to send the message, using jms, which is required.

推荐答案

因为我没有找到任何过滤邮件正文或发送没有标题的邮件的解决方案(相关的,未答复的问题在这里:如何使用空标头发送纯文本JmsMessage )解决方案是发送一个JSON对象并使用正则表达式对前端的JSON语法进行过滤。

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.

后端:

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 );
}

前端:

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);
});

结果将是:

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

这篇关于如何将jms消息发送到activeMQ并使用mqttJS正确解码javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆