ActiveMQ与mqtt.js的结合应用

项目结构

ActiveMQ与mqtt.js的结合应用


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>

运行结果如图:

ActiveMQ与mqtt.js的结合应用


ActiveMQ与mqtt.js的结合应用


ActiveMQ与mqtt.js的结合应用

上一篇:Net高并发解决思路


下一篇:使用PostGreSQL数据库进行text录入和text检索