Hanson.Mqtt.Utils
2.0.0
dotnet add package Hanson.Mqtt.Utils --version 2.0.0
NuGet\Install-Package Hanson.Mqtt.Utils -Version 2.0.0
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="Hanson.Mqtt.Utils" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hanson.Mqtt.Utils --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hanson.Mqtt.Utils, 2.0.0"
#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 Hanson.Mqtt.Utils as a Cake Addin #addin nuget:?package=Hanson.Mqtt.Utils&version=2.0.0 // Install Hanson.Mqtt.Utils as a Cake Tool #tool nuget:?package=Hanson.Mqtt.Utils&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
README
Hanson.Mqtt.Utils
功能說明
簡化 MQTT 的使用方式
主要功能
Broker
- 支援 Client 上線及下線異動的處理事件
- 支援 Client 訂閱及解除訂閱異動的處理事件
- 支援 StartAsync 啟動 Broker
- 支援 取得所有線上 client id
Publish/Subscrib
- 支援 Pushlish MQTT 訊息
- 支援 Subscrib MQTT 訊息
限制
- 限使用 本機 IP 啟動 Broker Server
安裝方式
- 使用 Nuget 方式進行安裝 https://www.nuget.org/packages/hanson.mqtt.utils/
使用範例
- Broker Server 使用範例
private async Task RunBrokerServerAsync()
{
var server = MqttBroker.CreateInstance();
// 使用者清單有變更時觸發的事件
server.OnClientChangedEvent += ((clients) =>
{
Console.WriteLine("目前使用者清單");
clients?.ForEach(x => Console.WriteLine(x));
});
// 新訂閱 Topic 時觸發的事件
server.OnClientSubscribedTopicEvent += (async (clientId, topic) =>
{
Console.WriteLine($"使用者:{clientId} 已訂閱 Topic:{topic}");
// 廣播訊息到 Topic 中
await server.SendMessageAsync(new Payload
{
Topic = topic,
Value = $"Welcom user-{clientId}"
});
});
// 取消訂閱 Topic 時觸發的事件
server.OnClientUnsubscribedTopicEvent += ((clientId, topic) =>
{
Console.WriteLine($"使用者:{clientId} 取消訂閱 Topic:{topic}");
});
// 使用者上線時觸發的事件
server.OnClientConnectedEvent += ((x) =>
{
Console.WriteLine($"使用者:{x} 已上線");
});
// 使用者下線時觸發的事件
server.OnClientDisconnectedEvent += ((x) =>
{
Console.WriteLine($"使用者:{x} 已下線");
});
// 接收到使用者 Publish 資料時觸發
server.OnInterceptingPublishEvent += ((client, payload) =>
{
Console.WriteLine($"接收使用者:{client} 上傳的Topic:{payload.Topic}, message:{payload.Value}");
});
Console.WriteLine("Start MQTTnet Server ...");
// 啟動 MQTT Broker 的服務
await server.StartAsync();
Console.ReadKey();
}
- Publish / Subscribe 使用範例
private static async Task PublishMessageAsync()
{
var topic = "test";
var host = "127.0.0.1";
var port = 1883;
var clientId = "clientId";
using (var client = MqttUtils.CreateInstance(host,port,clientId))
{
client.OnConnectedEvent += ((x) =>
{
Console.WriteLine("Connected");
});
client.OnConnectingFailedEvent += ((x) =>
{
Console.WriteLine($"ConnectingFailed error:{x.Message}");
});
// 訂閱服務接收的事件
client.OnMessageReceivedEvent += ((x) =>
{
Console.WriteLine($"Topic:{x.Topic}, Message value: {x.Value}");
});
client.OnPublishAcknowledgedEvent += ((x) =>
{
Console.WriteLine($"Send topic:{x.Topic} is success");
});
client.OnPublishMessageTimeoutEvent += ((x) =>
{
Console.WriteLine($"Send topic:{x.Topic} is timeout");
});
client.OnDisconnectedEvent += (() =>
{
Console.WriteLine("Disonnected");
});
// 啟動訂閱服務
await client.SubscribeAsync(new string[]{topic});
// Publish 的資料格式
var payload = new Payload
{
Topic = topic,
Value = "hello world",
QoS = MqttQualityOfServiceLevel.AtMostOnce
};
// 使用 Publish 方式送出資料
await client.PublishAsync(payload);
Console.ReadKey();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Hanson.Common.Extensions (>= 1.0.0)
- MQTTnet (>= 4.2.1.781)
- MQTTnet.Extensions.ManagedClient (>= 4.2.1.781)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.