hongmao.HmExtension.Mqtt 1.0.0

dotnet add package hongmao.HmExtension.Mqtt --version 1.0.0                
NuGet\Install-Package hongmao.HmExtension.Mqtt -Version 1.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="hongmao.HmExtension.Mqtt" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add hongmao.HmExtension.Mqtt --version 1.0.0                
#r "nuget: hongmao.HmExtension.Mqtt, 1.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 hongmao.HmExtension.Mqtt as a Cake Addin
#addin nuget:?package=hongmao.HmExtension.Mqtt&version=1.0.0

// Install hongmao.HmExtension.Mqtt as a Cake Tool
#tool nuget:?package=hongmao.HmExtension.Mqtt&version=1.0.0                

MQTT扩展模块

本模块基于MQTTnet进行扩展,提供了简易的使用方法

简单使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HmExtension.Mqtt;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            MqttTest();
            Console.ReadKey();
        }

        private static async Task MqttTest()
        {
            // 1. 创建客户端对象
            var client = new HmMqttClient()
            {
                IsPrintLogger = true // 开启消息日志
            };
            // 2. 连接MQTT服务器
            // 请自行替换对应参数
            await client.ConnectAsync([Host], [Username], [Password], [Port],[ClientId]);
            // 3. 订阅主题
            var topic = await client.SubscribeAsync("/test");

            topic.OnApplicationMessageReceived += e =>
            {
                // 接收消息
                var content = Encoding.UTF8.GetString(e.ApplicationMessage.PayloadSegment.Array);
                Console.WriteLine("接收到消息: "+content);
            };
            // 发送消息
            topic.Push("消息内容");
        }
    }
}

结果如下:

[发送] 2024-08-14 11:58:04 [1c558c0a-f460-47ef-aa71-11d2b299b607]       [/test] [ContentType=] [Payload=消息内容]
[接收] 2024-08-14 11:58:04 [1c558c0a-f460-47ef-aa71-11d2b299b607]       [/test] [ContentType=] [Payload=消息内容]
接收到消息: 消息内容
[接收] 2024-08-14 11:58:06 [1c558c0a-f460-47ef-aa71-11d2b299b607]       [/test] [ContentType=] [Payload=内容测试]
接收到消息: 内容测试
Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.1

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
1.0.0 111 8/14/2024