Unity使用ActiveMQ通信

Unity使用ActiveMQ通信

1、activemq安装与启动

安装使用教程

2、打开软件

在这里插入图片描述

3、打开这个链接,通信在这个上面完成

http://localhost:8161/admin/browse.jsp?JMSDestination=ActiveMQTest

4、Unity导入.dll文件,网上下载

Apache.NMS.ActiveMQ.dll和Apache.NMS.dll

5、Unity使用activemq接受、传输消息源码

using Apache.NMS;
using Apache.NMS.ActiveMQ;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class Test1 : MonoBehaviour
{
    public GameObject go;
    public InputField inputField;
    private void Awake()
    {
        Connect();
    }
    void Start()
    {
        //SendMq();
    }
    IMessageConsumer consumer;
    IConnectionFactory factory;
    void Connect()
    {
        try
        {
            factory = new ConnectionFactory("tcp://192.168.1.7:61616/");//192.168.1.7连接到activemq所在电脑的IP
            IConnection connection = factory.CreateConnection();
            connection.ClientId = "testing listener";
            connection.Start();
            ISession session = connection.CreateSession();
            consumer = session.CreateDurableConsumer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("ActiveMQTest"), "testing listener", null, false);
            consumer.Listener += new MessageListener(Consumer_Listener);
            Debug.LogError("服务器连接完成!!!!!!!!");
        }
        catch (System.Exception e)
        {
            Debug.LogError(e.Message);
        }
    }

    /// <summary>
    /// 监听
    /// </summary>
    /// <param name="message"></param>
    void Consumer_Listener(IMessage message)
    {
        try
        {
            ITextMessage msg = (ITextMessage)message;
            Debug.LogError("接收到MQ数据: " + msg.Text);
            MsgList.Add(msg.Text);
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
        }
    }

    public List<string> MsgList = new List<string>();
    void Update()
    {
        if (MsgList.Count > 0)
        {
            GameObject temp = GameObject.Instantiate(go);
            temp.name = MsgList[0];
            Destroy(temp, 3);
            MsgList.RemoveAt(0);
        }
    }
    public void OnButtonClick()//发送消息按钮事件
    {
        //建立工厂连接
        using (IConnection connection = factory.CreateConnection())
        {
            //通过工厂连接创建Session会话
            using (ISession session = connection.CreateSession())
            {
                //通过会话创建生产者,方法里new出来MQ的Queue
                IMessageProducer prod = session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic("ActiveMQTest"));
                //创建一个发送消息的对象
                ITextMessage message = prod.CreateTextMessage();
                message.Text = inputField.text;
                message.Properties.SetString("filter", "demo");
                prod.Send(message, MsgDeliveryMode.NonPersistent, MsgPriority.Normal, TimeSpan.MinValue);
                Debug.Log("发送成功");
            }
        }
    }
}


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ke-Di

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值