ActiveMQ与mqtt.js的结合应用

项目结构

这里写图片描述


这里写图片描述

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;
using Apache.NMS.Util;
using System.Threading;

namespace ActiveMqDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("按回车开始!");
            Console.ReadLine();
            IConnectionFactory factory =new ConnectionFactory("tcp://localhost:61616");
            using (IConnection conn = factory.CreateConnection("wu", "123456"))
            {
                using (ISession session = conn.CreateSession())
                {
                    ActiveMQTopic topic = new ActiveMQTopic("test");
                    using (IMessageProducer producer = session.CreateProducer(topic))
                    {
                        conn.Start();
                        //可以写入字符串,也可以是一个xml字符串等
                        for (int i = 0; i < 10; i++)
                        {
                            ITextMessage message = session.CreateTextMessage("新messsage" + i);
                            producer.Send(message,MsgDeliveryMode.NonPersistent,MsgPriority.Normal,TimeSpan.MinValue);
                            Console.WriteLine("发送新消息:" + i);
                            Thread.Sleep(1000);

                        }

                    }
                }
            }
            Console.ReadKey();

        }
    }
}

ActiveMqPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ActiveMqPage.aspx.cs" Inherits="MyDemo.ActiveMqPage" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/mqttws31.js"></script>
    <script type="text/javascript">
        var host = "localhost";
        var port = 61614;
        var clientID = "myWeb";
        var destination = "test";
        window.onload = function () {
            document.getElementById("btn-start").onclick = function () {
                window.client = new Messaging.Client(host, Number(port), clientID);
                client.onMessageArrived = function (e) {

                    alert(e.payloadString);
                };
                client.onConnectionLost = function (e) { alert("lost"); };

                client.connect({

                    onSuccess: function () {
                        client.subscribe(destination);
                    },
                    onFailure: function () {

                        alert("connect fail!");
                    }

                });

                document.getElementById("btn-send").onclick = function () {
                    var txt = document.getElementById("txt-value").value;
                    if (txt != "") {
                        var message = new Messaging.Message(txt);
                        message.destinationName = destination;
                        client.send(message);
                    }

                };

            };

        }

    </script>
</head>
<body>
    <div>
        <input type="button" id="btn-start" value="连接到ActiveMQ" />
        <input id="txt-value" type="text" />
        <input id="btn-send" type="button" value="发送" />
    </div>
</body>
</html>

运行结果如图:

这里写图片描述


这里写图片描述


这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值