NetPro.MQTTClient
6.0.16
dotnet add package NetPro.MQTTClient --version 6.0.16
NuGet\Install-Package NetPro.MQTTClient -Version 6.0.16
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetPro.MQTTClient" Version="6.0.16" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetPro.MQTTClient --version 6.0.16
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetPro.MQTTClient, 6.0.16"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetPro.MQTTClient as a Cake Addin
#addin nuget:?package=NetPro.MQTTClient&version=6.0.16
// Install NetPro.MQTTClient as a Cake Tool
#tool nuget:?package=NetPro.MQTTClient&version=6.0.16
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NetPro.MQTTClient 使用
MQTT客户端类库,支持多服务端,完整示例
使用
安装引用NetPro.MQTTClient
先增加如下配置
clientid不指定自动生成,如手动指定必须保证在broker中唯一!
"MQTTClientOption": {
"ConnectionString": [
{
"Key": "1", //连接串key别名,唯一
"Value": "clientid=netpro;host=mqtt://127.0.0.1:1883;username=netpro;password=netpro;timeout=5000;keepalive=120;cleansession=true;" //别名key对应的连接串
}
]
}
1、基于NetPro.Startup基座
增加环境变量 ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=NetPro.Satrtup
2、无依赖使用
public void ConfigureServices(IServiceCollection services)
{
services.AddMQTTClient(configuration);
}
当想自定义连接字符串获取方式时,无论是否基于NetPro.Web.Api, 都能通过传入委托来自定义连接字符串获取方式:
public void ConfigureServices(IServiceCollection services)
{
services.AddMQTTClient(GetConnectionString);
}
public IList<ConnectionString> GetConnectionString(IServiceProvider serviceProvider)
{
var connector = new List<ConnectionString>();
connector.Add(new ConnectionString { Key = "2", Value = "clientid=netpro;host=mqtt://192.168.100.187:1883;username=netpro;password=netpro;timeout=5000;keepalive=2;cleansession=true;" });
return connector;
}
服务注入使用
发布者
public class HttpProxyController : ControllerBase
{
private readonly ILogger _logger;
private readonly IMqttClientMulti _mqttClientMulti;
//构造函数注入
public HttpProxyController(
ILogger<DatabaseCurdController> logger
IMqttClientMulti _mqttClientMulti)
{
_logger = logger;
_exampleProxy = exampleProxy;
}
[HttpGet("getorcreate")]
[PostResponseCache(Duration = 2)]
[ProducesResponseType(200, Type = typeof(ResponseResult))]
public async Task<IActionResult> GetOrCreateAsync(uint id)
{
var _mqttClient = _mqttClientMulti["1"];
var messagePayload = new MqttApplicationMessageBuilder()
.WithTopic("netpro/local")
.WithPayload($"发布消息-{DateTime.Now}")
.WithAtMostOnceQoS()//WithAtMostOnceQoS:Level0ithAtLeastOnceQoS:Level1;WithExactlyOnceQoS:Level2
.WithRetainFlag(true)//服务器保持消息,有客户端连接此主题后一条消息;一个主题保留一条消息;
.Build(); //删除消息既发送一条Payload为0的消息即可。
_mqttPublishClient.PublishAsync(messagePayload);
return Ok();
}
}
消费者
- 注意:MQTT5.0支持共享订阅实现订阅者负载,利用共享标识$queue和$share即可
public class MQTTClientTask : IStartupTask
{
private readonly IMqttClientMulti _mqttClientMulti;
public MQTTClientTask()
{
_mqttClientMulti = EngineContext.Current.Resolve<IMqttClientMulti>();
}
public int Order => 2;
public void Execute()
{
var filter = new MqttTopicFilter()
{
//https://www.hivemq.com/blog/mqtt-client-load-balancing-with-shared-subscriptions/
//共享标识$queue和$share;
Topic = "$share/g/netpro",//"家/客厅/空调/#",#:通配符;topic通过/分割主题层级,一般层级由高到低 ;共享标识$queue和$share
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
};
_Subscribe();
void _Subscribe()
{
var _mqttClient = _mqttClientMulti["1"];
var result = _mqttClient.SubscribeAsync(filter).ConfigureAwait(false).GetAwaiter().GetResult();
//消费消息
_mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(arg =>
{
string payload = System.Text.Encoding.UTF8.GetString(arg.ApplicationMessage.Payload);
System.Console.WriteLine("Message received, topic [" + arg.ApplicationMessage.Topic + "], payload [" + payload + "]");
});
//重连
_mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(async arg =>
{
//只是重连,但是消息需要重新订阅;也可设置CleanSession为false,重连依旧启用之前的订阅。
var reconnectResult = await _mqttClient.ReconnectAsync();
_Subscribe(); //CleanSession设置为false后,可不必重复订阅。
});
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 3.1
- MQTTnet (>= 3.1.2)
- NetPro.Startup (>= 6.0.16)
-
net6.0
- MQTTnet (>= 3.1.2)
- NetPro.Startup (>= 6.0.16)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.0.16 | 155 | 7/24/2023 |
6.0.15 | 407 | 7/19/2022 |
6.0.14 | 401 | 7/10/2022 |
6.0.13 | 407 | 6/15/2022 |
6.0.12 | 395 | 6/15/2022 |
6.0.11 | 395 | 6/15/2022 |
6.0.10 | 402 | 6/11/2022 |
6.0.9 | 408 | 6/8/2022 |
6.0.8 | 408 | 5/26/2022 |
6.0.8-beta.3 | 133 | 5/24/2022 |
6.0.8-beta.2 | 111 | 5/24/2022 |
6.0.7 | 418 | 5/18/2022 |
6.0.6 | 432 | 4/28/2022 |
6.0.5 | 410 | 3/30/2022 |
6.0.5-beta.20 | 105 | 4/27/2022 |
6.0.5-beta.19 | 113 | 4/25/2022 |
6.0.5-beta.18 | 102 | 4/22/2022 |
6.0.5-beta.17 | 113 | 4/16/2022 |
6.0.5-beta.16 | 175 | 4/8/2022 |
6.0.5-beta.15 | 111 | 4/8/2022 |
6.0.5-beta.14 | 118 | 4/7/2022 |
6.0.5-beta.13 | 123 | 4/7/2022 |
6.0.5-beta.12 | 114 | 4/6/2022 |
6.0.5-beta.11 | 114 | 4/6/2022 |
6.0.5-beta.10 | 108 | 3/31/2022 |
6.0.5-beta.9 | 121 | 3/26/2022 |
6.0.5-beta.8 | 119 | 3/22/2022 |
6.0.5-beta.7 | 110 | 3/21/2022 |
6.0.5-beta.6 | 121 | 3/14/2022 |
6.0.5-beta.5 | 114 | 3/2/2022 |
6.0.5-beta.4 | 115 | 2/22/2022 |
6.0.5-beta.3 | 122 | 2/18/2022 |
6.0.5-beta.2 | 118 | 2/18/2022 |
6.0.5-beta.1 | 121 | 2/16/2022 |
6.0.4 | 436 | 2/10/2022 |
6.0.3 | 433 | 2/9/2022 |
6.0.3-beta.9 | 122 | 2/10/2022 |
6.0.3-beta.7 | 136 | 1/27/2022 |
6.0.3-beta.6 | 145 | 1/19/2022 |
6.0.3-beta.5 | 133 | 1/17/2022 |
6.0.3-beta.4 | 137 | 1/16/2022 |
6.0.3-beta.3 | 134 | 1/14/2022 |
6.0.3-beta.2 | 124 | 1/13/2022 |
6.0.3-beta.1 | 160 | 1/11/2022 |
6.0.2 | 321 | 1/6/2022 |