NetPro.MQTTClient
6.0.12
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package NetPro.MQTTClient --version 6.0.12
NuGet\Install-Package NetPro.MQTTClient -Version 6.0.12
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.12" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetPro.MQTTClient --version 6.0.12
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetPro.MQTTClient, 6.0.12"
#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.12 // Install NetPro.MQTTClient as a Cake Tool #tool nuget:?package=NetPro.MQTTClient&version=6.0.12
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.12)
-
net6.0
- MQTTnet (>= 3.1.2)
- NetPro.Startup (>= 6.0.12)
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 | 204 | 7/24/2023 |
6.0.15 | 435 | 7/19/2022 |
6.0.14 | 421 | 7/10/2022 |
6.0.13 | 437 | 6/15/2022 |
6.0.12 | 422 | 6/15/2022 |
6.0.11 | 423 | 6/15/2022 |
6.0.10 | 429 | 6/11/2022 |
6.0.9 | 436 | 6/8/2022 |
6.0.8 | 435 | 5/26/2022 |
6.0.8-beta.3 | 149 | 5/24/2022 |
6.0.8-beta.2 | 123 | 5/24/2022 |
6.0.7 | 445 | 5/18/2022 |
6.0.6 | 459 | 4/28/2022 |
6.0.5 | 436 | 3/30/2022 |
6.0.5-beta.20 | 119 | 4/27/2022 |
6.0.5-beta.19 | 126 | 4/25/2022 |
6.0.5-beta.18 | 120 | 4/22/2022 |
6.0.5-beta.17 | 126 | 4/16/2022 |
6.0.5-beta.16 | 190 | 4/8/2022 |
6.0.5-beta.15 | 125 | 4/8/2022 |
6.0.5-beta.14 | 136 | 4/7/2022 |
6.0.5-beta.13 | 137 | 4/7/2022 |
6.0.5-beta.12 | 126 | 4/6/2022 |
6.0.5-beta.11 | 127 | 4/6/2022 |
6.0.5-beta.10 | 129 | 3/31/2022 |
6.0.5-beta.9 | 133 | 3/26/2022 |
6.0.5-beta.8 | 133 | 3/22/2022 |
6.0.5-beta.7 | 124 | 3/21/2022 |
6.0.5-beta.6 | 135 | 3/14/2022 |
6.0.5-beta.5 | 130 | 3/2/2022 |
6.0.5-beta.4 | 128 | 2/22/2022 |
6.0.5-beta.3 | 136 | 2/18/2022 |
6.0.5-beta.2 | 130 | 2/18/2022 |
6.0.5-beta.1 | 132 | 2/16/2022 |
6.0.4 | 465 | 2/10/2022 |
6.0.3 | 459 | 2/9/2022 |
6.0.3-beta.9 | 133 | 2/10/2022 |
6.0.3-beta.7 | 150 | 1/27/2022 |
6.0.3-beta.6 | 157 | 1/19/2022 |
6.0.3-beta.5 | 145 | 1/17/2022 |
6.0.3-beta.4 | 149 | 1/16/2022 |
6.0.3-beta.3 | 146 | 1/14/2022 |
6.0.3-beta.2 | 135 | 1/13/2022 |
6.0.3-beta.1 | 171 | 1/11/2022 |
6.0.2 | 347 | 1/6/2022 |