FCARDCloud.Framework.Common.MQTTNet 1.8.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FCARDCloud.Framework.Common.MQTTNet --version 1.8.0
                    
NuGet\Install-Package FCARDCloud.Framework.Common.MQTTNet -Version 1.8.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="FCARDCloud.Framework.Common.MQTTNet" Version="1.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FCARDCloud.Framework.Common.MQTTNet" Version="1.8.0" />
                    
Directory.Packages.props
<PackageReference Include="FCARDCloud.Framework.Common.MQTTNet" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FCARDCloud.Framework.Common.MQTTNet --version 1.8.0
                    
#r "nuget: FCARDCloud.Framework.Common.MQTTNet, 1.8.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.
#:package FCARDCloud.Framework.Common.MQTTNet@1.8.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FCARDCloud.Framework.Common.MQTTNet&version=1.8.0
                    
Install as a Cake Addin
#tool nuget:?package=FCARDCloud.Framework.Common.MQTTNet&version=1.8.0
                    
Install as a Cake Tool

FCARDCloud.Framework.Common.MQTTNet

介绍

net6 版本的MQTT客户端连接库

封装连接和自动重连功能

软件架构

net6

配置

Startup.cs 的 ConfigureServices 函数 中注入配置;

//配置阿里MQTT服务
Services.AddMQTTClientByAli(config.GetSection("AliMQTTSetting"));
//配置标准MQTT服务
Services.AddMQTTClient(config.GetSection("MQTTSetting"));

appsettings.json 中的配置项

{
  "AliMQTTSetting": { //标准MQTT服务器
    "ServerAddr": "192.168.1.15",
    "ServerPort": 8883,
    "SSL": true,

    "ClientID": "test001",
    "UserName": "user001",
    "Password": "abcdefg"
   },

  "AliMQTTSetting": { //阿里张家口实例示例
    "AccessKeyID": "",
    "SecretKey": "",

    "ServerAddr": "post-cn-6ja1s60ki1b.mqtt.aliyuncs.com",
    "ServerPort": 8883,
    "SSL": true,

    "Group": "GID_MQWebTest",
    "ClientCode": "Test001",

    "InstanceID": "post-cn-6ja1s60ki1b"
   }

订阅MQTT主题调用示例

Startup.cs 的 ConfigureServices 函数 中注入配置

//配置订阅处理服务
services.AddHostedService<MyMQTTHandleService>();
    public class MyMQTTHandleService : IHostedService
    {
        private readonly ILogger<MyMQTTHandleService> _logger;
        private MQTTClient MQTTClient;

        public MyMQTTHandleService(
            ILogger<MyMQTTHandleService> log,
            MQTTClient client)
        {
            this.MQTTClient = client;
            _logger = log;
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            //全局订阅某主题
            await MQTTClient.Subscribe("testTopic1");
            await MQTTClient.Subscribe("testTopic2");
            await MQTTClient.Subscribe("testTopic3");

            MQTTClient.MqttMsgPublishReceived = MqttMsgPublishReceived;


            _logger.LogInformation($"MQTT服务器已启动!");

        }

        /// <summary>
        /// 订阅的主题有新消息时的回调处理函数
        /// </summary>
        /// <param name="e"></param>
        private void MqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs  e)
        {
            var msg = e.ApplicationMessage;
            _logger.LogInformation($"MQTT 接收到消息,Topic:[ {msg.Topic}],Body={Encoding.UTF8.GetString(msg.Payload)}");
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            return Task.CompletedTask;
        }
    }

发布 MQTT 消息调用示例

    [ApiController]
    [Route("[controller]")]
    public class MqttController : ControllerBase
    {
        private readonly ILogger<MqttController> _logger;
        private MQTTClient MQTTClient;

        public MqttController(
            ILogger<MqttController> log,
            MQTTClient client)
        {
            this.MQTTClient = client;
            _logger = log;
        }

        [HttpPost("Publish")]
        public JsonResult Publish(string msg)
        {
            MQTTClient.Publish("testTopic1", msg);

            return new JsonResult("Publish Over");
        }
    }
Product 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.9.0 270 6/8/2023
1.8.0 325 2/20/2023
1.7.0 322 2/16/2023
1.6.0 512 6/16/2022
1.5.0 512 6/11/2022
1.4.0 492 6/11/2022
1.3.0 511 6/11/2022
1.2.0 484 5/27/2022
1.1.0 475 5/27/2022