赞
踩
EMQ 官方下载地址及部署文档
选择开源类型 --> 确定版本 --> 下载安装,下载完成后启动emqx
启动后可以通过访问服务端18083端口(http://+服务器ip+:端口号 --> http://192.168.0.1:18083)访问看板页面
默认用户名:admin
默认密码:public
相关配置可在\etc\emqx.conf配置文件中修改
文件目录结构
打开看板登录进看板页,如有需要可将看板语言在设置中修改为中文
在功能配置中查看监听器,如下默认监听地址(注:也可在配置文件中查看修改)
简单连接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 = "本机ip"; 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); } }
如上就配置好EMQ的服务及测试demo程序
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。