• MQTT 消息服务器 EMQ简单对接DEMO


    MQTT 消息服务器 EMQ简单对接DEMO

    下载启动EMQ

    EMQ 官方下载地址及部署文档
     选择开源类型 --> 确定版本 --> 下载安装
    官方示例启动方法

    部署完成查看看板

    1. 启动后可以通过访问服务端18083端口(http://+服务器ip+:端口号 --> http://192.168.0.1:18083)访问
          默认用户名:admin  
          默认密码:public
          配置可在\etc\emqx.conf中修改
        文件目录结构
        EMQX文件目录结构

    2. 打开看板登录进看板页,如有需要可将看板语言在设置中修改为中文
      中文语言修改

    3. 在功能配置中查看监听器,如下默认监听地址(注:也可在配置文件中查看修改)
      监听器地址

    4. 简单连接EMQ示例

    internal class Program
        {
            static MqttClient ConnectMQTT(string broker, int port, string clientId, string username, string password)
            {
                MqttClient client = new MqttClient(broker, port, false, MqttSslProtocols.None, null, null);
                client.Connect(clientId, username, password);
                if (client.IsConnected)
                {
                    Console.WriteLine("Connected to MQTT Broker");
                }
                else
                {
                    Console.WriteLine("Failed to connect");
                }
                return client;
            }
    
            static void Publish(MqttClient client, string topic)
            {
                int msg_count = 0;
                while (true)
                {
                    System.Threading.Thread.Sleep(1 * 1000);
                    string msg = "messages: " + msg_count.ToString();
                    client.Publish(topic, System.Text.Encoding.UTF8.GetBytes(msg));
                    Console.WriteLine("Send `{0}` to topic `{1}`", msg, topic);
                    msg_count++;
                }
            }
    
            static void Subscribe(MqttClient client, string topic)
            {
                client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
                client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
            }
            static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
            {
                string payload = System.Text.Encoding.Default.GetString(e.Message);
                Console.WriteLine("Received `{0}` from `{1}` topic", payload, e.Topic.ToString());
            }
    
            static void Main(string[] args)
            {
                string broker = "192.168.2.52";
                int port = 1883;
                string topic = "Csharp/mqtt";
                string clientId = Guid.NewGuid().ToString();
                string username = "admin";
                string password = "public";
                MqttClient client = ConnectMQTT(broker, port, clientId, username, password);
                Subscribe(client, topic);
                Publish(client, topic);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    如上就配置好EMQ的服务及测试demo程序

  • 相关阅读:
    牛客练习赛105 D.点分治分点(spfa&bfs)
    35个Redis企业级性能优化点与解决方案
    (附源码)计算机毕业设计SSM基于的二手车交易平台
    修改el-radio-group样式,自定义单选组件
    【LeetCode-940 hard】不同的子序列 II
    查看自己显卡支持的最高CUDA版本,和已安装的CUDA版本
    Vue.js 进阶技巧:keep-alive 缓存组件解析
    IEEE Transaction期刊修改过程记录
    The WebSocket session [x] has been closed and no method (apart from close())
    SpringBoot整合SpringSecurity
  • 原文地址:https://blog.csdn.net/qq_36535245/article/details/126175884