![]() |
VOOZH | about |
dotnet add package DH.NMQTT --version 4.18.2026.210
NuGet\Install-Package DH.NMQTT -Version 4.18.2026.210
<PackageReference Include="DH.NMQTT" Version="4.18.2026.210" />
<PackageVersion Include="DH.NMQTT" Version="4.18.2026.210" />Directory.Packages.props
<PackageReference Include="DH.NMQTT" />Project file
paket add DH.NMQTT --version 4.18.2026.210
#r "nuget: DH.NMQTT, 4.18.2026.210"
#:package DH.NMQTT@4.18.2026.210
#addin nuget:?package=DH.NMQTT&version=4.18.2026.210Install as a Cake Addin
#tool nuget:?package=DH.NMQTT&version=4.18.2026.210Install as a Cake Tool
MQTT协议是物联网领域最流行的通信协议!
DH.NMQTT包含了MQTT的完整实现,并实现了客户端MqttClient,以及服务端MqttServer。
其中MqttServer仅实现基本网络框架,支持消息收发,完整的消息交换功能位于商用版IoT平台中。
最流行的物联网通信协议MQTT,包括客户端、服务端和Web管理平台。
提供订阅/发布模式,更为简约、轻量,易于使用,针对受限环境(带宽低、网络延迟高、网络通信不稳定),可以简单概括为物联网打造,官方总结特点如下:
发布时,指定消息Qos,broker保存的消息包含了Qos;
订阅时,指定这次订阅要求的Qos,broker回复授权使用的Qos,一般就是申请那个;
消费时,消息的Qos取发布订阅中较小者!
详细场景:
PubAck,表示已经收到消息;PubRec,客户端再次发送PubRel,broker答复PubComp,消息才算发布完成;PubRec,broker再次发送PubRel,客户端答复PubComp,消息才算消费完成;打开源码解决方案,把Test设为启动项目,启动即可。
默认先后启动TestServer和TestClient。
Nuget引用DH.NMQTT,使用以下代码启动服务端:
var services = ObjectContainer.Current;
services.AddSingleton<ILog>(XTrace.Log);
services.AddTransient<IMqttHandler, MqttHandler>();
services.AddSingleton<MqttExchange, MqttExchange>();
var server = new MqttServer
{
Port = 1883,
ServiceProvider = services.BuildServiceProvider(),
Log = XTrace.Log,
SessionLog = XTrace.Log,
};
server.Start();
通过指定端口1883,默认处理器MqttHandler,默认交换机MqttExchange,启动服务端。
Nuget引用DH.NMQTT,使用以下代码连接服务端:
var client = new MqttClient
{
Log = XTrace.Log,
Server = "tcp://127.0.0.1:1883",
//UserName = "admin",
//Password = "admin",
ClientId = Guid.NewGuid() + "",
};
await client.ConnectAsync();
// 订阅“/test”主题
var rt = await client.SubscribeAsync("/test", (e) =>
{
XTrace.WriteLine("收到消息:" + "/test/# =>" + e.Topic + ":" + e.Payload.ToStr());
});
// 每2秒向“/test”主题发布一条消息
while (true)
{
try
{
var msg = "学无先后达者为师" + Rand.NextString(8);
await client.PublishAsync("/test", msg);
}
catch (Exception ex)
{
XTrace.WriteException(ex);
}
await Task.Delay(2000);
}
客户端连接服务端有几个要素:服务端地址、用户名、密码、客户端标识,然后通过ConnectAsync连接服务端。
客户端可以是消费者角色,通过SubscribeAsync订阅指定Topic。
客户端也可以是生产者角色,通过PublishAsync发布消息到指定Topic。
需要在服务端处理客户端连接和消息交互逻辑时,就需要自定义服务端。例如IoT平台,在收到设备上报MQTT数据以后,直接接收落库,而不需要再次消费。
自定义处理器示例如下:
private class MyHandler : MqttHandler
{
private readonly ILog _log;
public MyHandler(ILog log) => _log = log;
protected override ConnAck OnConnect(ConnectMessage message)
{
_log.Info("客户端[{0}]连接 user={1} pass={2} clientId={3}", Session.Remote.EndPoint, message.Username, message.Password, message.ClientId);
return base.OnConnect(message);
}
protected override MqttMessage OnDisconnect(DisconnectMessage message)
{
_log.Info("客户端[{0}]断开", Session.Remote);
return base.OnDisconnect(message);
}
protected override MqttIdMessage OnPublish(PublishMessage message)
{
_log.Info("客户端[{0}]发布[{1}:qos={2}]: {3}", Session.Remote, message.Topic, (Int32)message.QoS, message.Payload.ToStr());
return base.OnPublish(message);
}
}
稍微修改一下服务端注入处理器的代码即可使用自定义处理器:
var services = ObjectContainer.Current;
services.AddSingleton<ILog>(XTrace.Log);
services.AddTransient<IMqttHandler, MyHandler>();
services.AddSingleton<MqttExchange, MqttExchange>();
var server = new MqttServer
{
Port = 1883,
ServiceProvider = services.BuildServiceProvider(),
Log = XTrace.Log,
SessionLog = XTrace.Log,
};
server.Start();
集群管理,Join、Ping、Lease。
Join加入集群,告诉对方我是集群节点之一,启动时调用N-1次。
每15秒Ping一次所有节点,更新活跃时间。
Lease离开集群,调用N-1次。
每个节点根据最后活跃时间,定时剔除超时节点。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net45 net45 is compatible. net451 net451 was computed. net452 net452 was computed. net46 net46 was computed. net461 net461 is compatible. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.18.2026.527-beta1101 | 95 | 5/27/2026 |
| 4.18.2026.527-beta0846 | 98 | 5/27/2026 |
| 4.18.2026.516-beta0822 | 87 | 5/16/2026 |
| 4.18.2026.516-beta0818 | 90 | 5/16/2026 |
| 4.18.2026.516-beta0813 | 89 | 5/16/2026 |
| 4.18.2026.516-beta0801 | 92 | 5/16/2026 |
| 4.18.2026.516-beta0745 | 96 | 5/16/2026 |
| 4.18.2026.516-beta0438 | 105 | 5/16/2026 |
| 4.18.2026.421-beta1221 | 119 | 4/21/2026 |
| 4.18.2026.421-beta0913 | 90 | 4/21/2026 |
| 4.18.2026.421-beta0858 | 100 | 4/21/2026 |
| 4.18.2026.412-beta1133 | 99 | 4/12/2026 |
| 4.18.2026.412-beta0601 | 97 | 4/12/2026 |
| 4.18.2026.326-beta0917 | 108 | 3/26/2026 |
| 4.18.2026.326-beta0915 | 103 | 3/26/2026 |
| 4.18.2026.326-beta0907 | 101 | 3/26/2026 |
| 4.18.2026.326-beta0900 | 103 | 3/26/2026 |
| 4.18.2026.305-beta0922 | 106 | 3/5/2026 |
| 4.18.2026.225-beta1238 | 129 | 2/25/2026 |
| 4.18.2026.210 | 144 | 2/10/2026 |
更新依赖包到最新版本