- XiaoFeng.MQTT 目录
- MQTT技术教学
-
- XiaoFeng.MQTT 目录
- MQTT技术教学
-
MQTT使用实例作者 : Jacky 发布于 2024-09-10 18:02:27 浏览 263 次XiaoFeng.Mqtt中间件,支持.NET框架、.NET内核和.NET标准库,一种非常方便操作的客户端工具。实现了MQTT客户端,MQTT服务端,同时支持TCP,WebSocket连接。支持协议版本3.0.0,3.1.0,5.0.0
Install
.NET CLI
$ dotnet add package XiaoFeng.Mqtt --version 1.0.4
Package Manager
PM> Install-Package XiaoFeng.Mqtt --Version 1.0.4
PackageReference
<PackageReference Include="XiaoFeng.Mqtt" Version="1.0.4" />
Paket CLI
> paket add XiaoFeng.Mqtt --version 1.0.4
Script & Interactive
> #r "nuget: XiaoFeng.Mqtt, 1.0.4"
Cake
// Install XiaoFeng.Mqtt as a Cake Addin #addin nuget:?package=XiaoFeng.Mqtt&version=1.0.4 // Install XiaoFeng.Mqtt as a Cake Tool #tool nuget:?package=XiaoFeng.Mqtt&version=1.0.4
XiaoFeng.Mqtt
Mqtt提供了友好的访问API。
基本使用方法
Mqtt连接串
mqtt://username:7092734@127.0.0.1:1883?ConnectionTimeout=3000&ReadTimeout=3000&WriteTimeout=3000 [<protocol>]://[[<username>:<password>@]<host>:<port>][?<p1>=<v1>[&<p2>=<v2>]] |----------|---|-----------|-----------|------|------|-----------------------| | protocol | | username | password | host | port | params |
protocol 支持mqtt,mqtts,ws,wss,tcp
username 用户名
7092734 密码
127.0.0.1 主机
1883 端口
ConnectionTimeout 连接超时时长
ReadTimeout 读取数据超时时长
WriteTimeout 发送数据超时时长
最小的连接串是:mqtt://127.0.0.1
实例化一个Mqtt对象
MQTT 客户端
下边代码直接拷贝到程序中即可使用,修改一下服务器连接串或账号密码等即可.
var client = new MqttClient("mqtt://admin:123456@127.0.0.1:1883"); client.ClientOptions = new MqttClientOptions { UserName = "admin", Password = "123456", ProtocolVersion = MqttProtocolVersion.V500//可指定相关版本 }; client.OnStarted += o => { w("已连接."); }; client.OnStoped += o => { w("已停止."); }; client.OnError += (o, e) => { //错误消息 w(e); }; client.OnMessage += e => { //除订阅消息以外的所有消息 var msg = "Unknown error."; if (e.ResultType == ResultType.Success) { if (e.Message.IsNullOrEmpty()) { if (e.MqttPacket != null) msg = e.MqttPacket.ToString(); else msg = "Unknown packet."; } else msg = e.Message; } else { msg = e.Message.IsNullOrEmpty() ? "" : e.Message; } w(msg); }; client.OnPublishMessage += e => { //专一接收所订阅的消息 var c = client; w($"Publish Message: {e}"); }; //开启连接服务端 await client.ConnectAsync().ConfigureAwait(false); //订阅主题 await client.SubscributeAsync("a/b").ConfigureAwait(false); //向指定的主题发送消息 await client.PublishAsync("a/b", "Hello World").ConfigureAwait(false); static void w(string msg) => Console.WriteLine($"{DateTime.Now.ToTimeStamp()}: {msg}");
MQTT 服务端
下边代码直接拷贝到程序中即可使用,修改一下账号密码及端口
var server = new MqttServer(new IPEndPoint(IPAddress.Any, 1883)); //添加服务器凭证 server.AddCredential("admin", "123456", ""); server.OnStarted += o => { //服务器启动消息 w("ELF (MQTT) server version 1.0.0 starting."); w("Config loaded from elfmqtt.conf."); w($"Listen {server.ServerOptions.EndPoint} mqtt://{server.ServerOptions.EndPoint}"); }; server.OnStoped += o => { //服务器停止消息 w("ELF MQTT Server Version 1.0.0 stoped."); }; server.OnConnected += o => { //新连接消息 w($"New connection from {o.EndPoint} on port {server.ServerOptions.EndPoint.Port}"); }; server.OnError += (o, m) => { //错误消息 w($"Error: {m}"); }; server.OnDisconnected += o => { //客户端断开连接消息 var data = o.GetClientData(); var address = ""; if (data != null && data.ConnectPacket != null) address = data.ConnectPacket.ClientId; address += $"[{o.EndPoint}]"; w($"Client {address} disconnected."); }; server.OnMessage += (o, e) => { //接收消息 var msg = "Unknown error."; if (e.ResultType == ResultType.Success) { if (e.Message.IsNullOrEmpty()) { if (e.MqttPacket != null) msg = e.MqttPacket.ToString(); else msg = "Unknown packet."; } else msg = e.Message; } else { msg = e.Message.IsNullOrEmpty() ? "" : e.Message; } w(msg); }; server.Start(); static void w(string msg) => Console.WriteLine($"{DateTime.Now.ToTimeStamp()}: {msg}");
所有评论(0)