> 文章列表 > ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组

ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组

ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组

ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组

1. 问题描述

** C#中的代码: **

internal class Program{static void Main(string[] args){ConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");IConnection connection = factory.CreateConnection();connection.Start();ISession session = connection.CreateSession();IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("test_producer"));consumer.Listener += new MessageListener(consumerListener);// 等待运行Console.ReadLine();}static void consumerListener(IMessage message){Console.WriteLine(message.GetType());}}
  1. 使用浏览器客户端发送消息
    ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组
    ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组
  2. javascript中发送消息
    ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组
    ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组

2. 解决

  1. 思路描述:
    可以发现JavaScript发送的是BytesMessage,只要修改发送的数据为TextMessage类型就可以了,但是我没找到这种思路的解决方法
  2. 字符串解码
    通过判断数据类型,如果是TestMessage就直接取出text就是utf-8形式的字符串,如果是BytesMessage就取出字节数组,将字节数组转成utf-8字符串就可以了
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace Consumer
{internal class Program{static void Main(string[] args){ConnectionFactory factory = new ConnectionFactory("tcp://localhost:61616");IConnection connection = factory.CreateConnection();connection.Start();ISession session = connection.CreateSession();IMessageConsumer consumer = session.CreateConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("test_producer"));consumer.Listener += new MessageListener(consumerListener);// 等待运行Console.ReadLine();}static void consumerListener(IMessage message){Console.WriteLine(message.GetType());string msg = StringUtils.ActiveMQMessageParse(message);Console.WriteLine("接收到->{0}", msg);}}public class StringUtils{/// <summary>/// 将字符串进行分割并获得字节数组/// </summary>/// <param name="str">待处理字符串</param>/// <param name="split">分割的字符串</param>/// <returns>字节数组</returns>public static byte[] StringToBytes(string str, char split){string[] strArr = str.Split(split);byte[] byteArr = new byte[strArr.Length];for (int i = 0; i < strArr.Length; i++){byteArr[i] = (byte)int.Parse(strArr[i]);}return byteArr;}/// <summary>/// 将字节数组转成字符串/// </summary>/// <param name="byteArr">字节数组</param>/// <param name="charSet">编码字符集</param>/// <returns></returns>public static string BytesToString(byte[] byteArr, CharSet charSet){return Encoding.UTF8.GetString(byteArr, 0, byteArr.Length);}/// <summary>/// 将字符串根据分隔符转成字节数组,然后转成指定字符集的字符串/// </summary>/// <param name="str">待处理字符串</param>/// <param name="split">分割字符串</param>/// <param name="charSet">字符集</param>/// <returns>处理后的字符串</returns>public static string StringToString(string str, char split, CharSet charSet){return BytesToString(StringToBytes(str, split), charSet);}/// <summary>/// 将ActiveMQ接收到的消息转换为UTF-8字符串/// </summary>/// <param name="message">接收到的数据</param>/// <returns></returns>public static string ActiveMQMessageParse(IMessage message){string str = null;if (message is ITextMessage){ITextMessage textMessage = (ITextMessage)message;try{str = textMessage.Text;}catch (Exception e){Console.WriteLine(e.StackTrace);}}else if (message is IBytesMessage){IBytesMessage bytesMessage = (IBytesMessage)message;byte[] byteArr = new byte[0];try{byteArr = new byte[(int)bytesMessage.BodyLength];int flag = bytesMessage.ReadBytes(byteArr);str = BytesToString(byteArr, CharSet.Unicode);}catch (Exception ex){Console.WriteLine(ex.StackTrace);}}return str;}}
}

3. 测试

3.1 浏览器发送

ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组
ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组

3.2 javascript发送

ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组
ActiveMQ使用(五):在JavaScript中发送的MQTT消息在C#中变为字节数组

X. 参考

如何从ActiveMqMessage获取消息文本

activemq - 接收到的 ActiveMQBytesMessage 内容为空

https://activemq.apache.org/maven/apidocs/org/apache/activemq/command/ActiveMQBytesMessage.html

byte数组与字符串之间相互转换
C#字节数组转换成字符串

广东高尔夫网