如何像使用AspNetCore中的Controllers 和 Actions一樣處理MQTT消息

在物聯網項目中,?處理MQTT的topic時費工費力, 代碼一團亂, 什么才是最好的姿勢?這里面我們極力介紹 MQTTnet.AspNetCore.Routing ?項目,MQTTnet AspNetCore Routing 是https://github.com/Atlas-LiftTech/MQTTnet.AspNetCore.AttributeRouting 的分支項目。這個組件是通過MQTTnet 實現了定義控制類和屬性來路由處理消息的topic , 就像Asp.Net Core 的WebApi 寫法一樣簡單容易。?

為什么要用這個庫?

這個庫完全是一個MQTTnet的可選擴展?,但如果你恰好有如下需求:

  • 你的主要需求是在服務器上驗證和處理MQTT消息。

  • 你的服務器并不主要用于向客戶端發送消息。?

  • 您喜歡以類似于 AspNetCore 和 WebAPI 的方式將消息處理邏輯封裝在控制器中。

您可以自己使用 MQTTnet 委托直接執行此插件執行的所有操作。但是,隨著您為驗證或處理傳入消息而編寫的邏輯量增加,將邏輯組織到控制器中的能力開始變得更加有意義。該庫有助于組織該代碼,并將依賴項注入框架整合到MQTTnet中。

功能

  • 將傳入消息邏輯封裝在控制器中

  • 在 MQTT 邏輯中使用 AspNetCore 中熟悉的范例(Controllers 和?Actions)

  • 在 AspNetCore 項目中使用現有的 ServiceProvider 實現對依賴注入的一流支持

  • 支持控制器上的同步和async/await操作

  • 與任何其他 MQTTnet 選項一起使用

性能說明

此庫尚未針對非常高負載的環境進行測試。確保在生產中使用之前執行自己的負載測試。歡迎所有性能改進 PR。

支持的框架

  • .NET Standard 2.0+

  • .NET Core 3.1+

支持的MQTT版本

  • 5.0.0

  • 3.1.1

  • 3.1.0

Nuget 和倉庫地址

Nutget:?https://www.nuget.org/packages/MQTTnet.AspNetCore.Routing/

https://gitee.com/IoTSharp/MQTTnet.AspNetCore.Routing

https://github.com/IoTSharp/MQTTnet.AspNetCore.Routing

使用方法

從nuget安裝此包和MQTTnet。dotnet CLI:

dotnet add package MQTTnet.AspNetCore.Routing

?ASP.NET Core 6 MVC 配置示例

using MQTTnet.AspNetCore;
using MQTTnet.AspNetCore.Routing;var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(o =>{o.ListenAnyIP(iotaboardMqttSettings.Port, l => l.UseMqtt());o.ListenAnyIP(iotaboardHttpSettings.Port);}
);//配置MQTT服務
builder.Services.AddHostedMqttServerWithServices(o =>{// other configurationso.WithoutDefaultEndpoint();}).AddMqttConnectionHandler().AddConnections().AddMqttControllers( // <== NOTICE THIS PART/*默認情況下,所有控制類在這里都會被自動發現, 那么這里就是空的, 但是這里也提供了加入其他程序集的方法。*/); var app = builder.Build();app.MapControllers();
app.UseMqttServer(server => {  // other MqttServer configurations, for example client connect interceptsserver.WithAttributeRouting(app.Services, allowUnmatchedRoutes: false);
});
app.Run();

創建你的控制器, 集成?MqttBaseController 并添加方法像下面這樣:

[MqttController]
[MqttRoute("[controller]")] // 指定控制類topic 。
public class MqttWeatherForecastController : MqttBaseController 
{private readonly ILogger<MqttWeatherForecastController> _logger;// Controllers支持完整的注入依賴, 就像AspNetCore 控制器一樣。public MqttWeatherForecastController(ILogger<MqttWeatherForecastController> logger){_logger = logger;}//支持模板路由和類型定義就像AspNetCore一樣。// Action 路由與控制器級別的路由前綴一起組成[MqttRoute("{zipCode:int}/temperature")]public Task WeatherReport(int zipCode){// 我們這里可以直接訪問MqttContextif (zipCode != 90210) { MqttContext.CloseConnection = true; }// 我們可以直接訪問原始數據var temperature = BitConverter.ToDouble(Message.Payload);_logger.LogInformation($"It's {temperature} degrees in Hollywood");// 規則判斷if (temperature <= 0 || temperature >= 130){return BadMessage();}return Ok();}//我們也支持 FromPayload , 就像 AspNetCore中的FromBody 一樣。[MqttRoute("viewmodel/{sender}")]public Task DeserializeViewModel(string sender, [FromPayload] SamplePayload payload){_logger.LogInformation("{Sender} says {Message}", sender, payload.Message);? ? ? ? ????return Accepted();}
}
public class SamplePayload
{public string Message { get; set; }
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281190.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281190.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281190.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

chrome 懸停大圖插件_Google Chrome瀏覽器的懸停卡:我不想要的我最喜歡的新東西

chrome 懸停大圖插件If you only have a handful of open tabs in Google Chrome, it’s easy to tell what they are. But as you start to collect more tabs (or make the window smaller), it gets harder. That’s where Hover Cards come in. 如果您在Google Chrome瀏覽器…

GitHub Codespaces 安裝 .NET 7

本文主要介紹如何在 GitHub Codespaces 這個云上 IDE 環境中安裝 .NET 7背景GitHub 的 Codespaces 可以讓我們隨時隨地編寫代碼&#xff0c;一些簡單的修改也非常方便快捷。特別是 .NET 7 發布后&#xff0c;一些可以直接升級的小項目只需要更改配置就可以了&#xff0c;我們可…

chrome怎么隱藏瀏覽器_如何使用Google Chrome的隱藏閱讀器模式

chrome怎么隱藏瀏覽器Chrome 75 has a hidden “Reader” mode that strips web pages down to the bare minimum to make them easier to, well, read. But it’s not enabled by default—here’s how to get it now. Chrome 75具有隱藏的“閱讀器”模式&#xff0c;可將網頁…

angularjs中使用swiper時不起作用,最后出現空白位

controller.js中定義swipers指令&#xff1a; var moduleCtrl angular.module(newscontroller,[infinite-scroll,ngTouch,news.service]) .directive(swipers,swipers); swipers.$inject [$timeout]; function swipers($timeout) {return {restrict: "EA",scope: {…

使用Jupyter記事本記錄和制作.NET可視化筆記

前言&#xff1a;對于記錄筆記的工具特別多&#xff0c;不過對于程序員來說&#xff0c;記錄筆記程序代碼運行結果演示可以同時存在&#xff0c;無疑會極大增加我們的筆記的可讀性和體驗感。以前在寫python的時候&#xff0c;使用jupyter的體驗很好&#xff0c;所以此處做一個基…

火狐上如何使用谷歌翻譯插件_將Google翻譯功能添加到Firefox

火狐上如何使用谷歌翻譯插件Are you looking for a quick no-fuss way to translate webpages? Then you will want to take a good look at the Translate extension for Firefox. 您是否正在尋找一種快速簡便的方法來翻譯網頁&#xff1f; 然后&#xff0c;您將需要很好地了…

Android 4.X 系統加載 so 失敗的原因分析

1 so 加載過程 so 加載的過程可以參考小米的系統工程師的文章loadLibrary動態庫加載過程分析 2 問題分析 2.1 問題 年前項目里新加了一個 so庫&#xff0c;但發現native 方法的找不到的 crash 好多&#xff0c;好些都是報了java.lang.unsatisfiedlinkerror native method not f…

桌面顯示激活windows_愚蠢的怪胎技巧:如何在桌面上顯示Windows版本

桌面顯示激活windowsHave you ever noticed during all the beta releases of Windows, there’s always a Windows version on the desktop in the lower right-hand corner? Here’s how that “feature” is enabled or disabled. 您是否曾經在Windows的所有beta版本中都注…

服務網格:限流保護 (上)

背景限流是服務治理中保護服務的重要手段之一&#xff0c;也是最直接有效的手段&#xff0c;它可以保護服務不被瞬間的大流量沖垮&#xff0c;類似電路中的“保險絲”。在服務上線前&#xff0c;我們都會對服務進行基準測試&#xff0c;來了解可通過的最大“電流”。上面所說的…

博弈論進階之Anti-SG游戲與SJ定理

前言 在上一節中&#xff0c;我們初步了解了一下SG函數與SG定理。 今天我們來分析一下SG游戲的變式——Anti-SG游戲以及它所對應的SG定理 首先從最基本的Anti-Nim游戲開始 Anti-Nim游戲是這樣的 有兩個頂尖聰明的人在玩游戲&#xff0c;游戲規則是這樣的&#xff1a; 有\(n\)堆…

怎樣取消outlook約會_快速提示:在Outlook 2010中設置和取消約會

怎樣取消outlook約會Getting everyone in one place at the same time for appointments can be daunting at times. Outlook makes it easy to setup appointments and invite attendees as well, and here we look at doing it in Outlook 2010. 同時讓每個人都集中在一個地方…

重視和解決 ABP 分布式事件亂序問題

ABP Framework 5.0 實現了單體應用場景下&#xff0c;收件箱和發件箱的事件嚴格順序性。但在微服務或多數據庫場景下&#xff0c;由于網絡時延和設施效率的限制&#xff0c; 分布式事件將不再是 Linearizability [1] 的&#xff0c;因此必然會存在物理時間上的收件亂序。借用 D…

個人博客建站方案推薦

1.服務器選擇 正值雙十一來臨之際各大服務器提供商又大量的優惠活動&#xff0c;各位要步入個人站長行列的小哥們時機要把握好了&#xff0c;我個人使用過阿里云的服務器&#xff0c;騰訊云的服務器&#xff0c;華為云的服務器。其實&#xff0c;個人感覺就放個博客&#xff0c…

linux系統下nginx安裝目錄和nginx.conf配置文件目錄

linux系統下nginx安裝目錄和nginx.conf配置文件目錄 1、查看nginx安裝目錄 輸入命令 # ps -ef | grep nginx 返回結果包含安裝目錄 root 2662 1 0 07:12 ? 00:00:00 nginx: master process /usr/sbin/nginx 2、查看nginx.conf配置文件目錄 輸入命令 # nginx…

android啟用hdcp_如何在Android上啟用優先收件箱(和設置僅重要通知)

android啟用hdcpYesterday Google released an updated Gmail application for Android 2.2 phones that supports the Priority Inbox feature—and more importantly, allows you to change your notifications to only alert you for important email. Let’s take a look. …

.Net CLR GC plan_phase二叉樹和Brick_table

楔子Plan_Phase(GC的計劃階段)很早就接觸了&#xff0c;但是后面一直沒用到&#xff0c;忘記了&#xff0c;此次又用到了&#xff0c;幾乎忘光了&#xff0c;費了很大力氣理解它&#xff0c;記錄下&#xff0c;以免又忘記了。主題計劃階段(plan_phase)主要就兩個部分&#xff0…

Vijos p1484 ISBN號碼

描述每一本正式出版的圖書都有一個ISBN號碼與之對應&#xff0c;ISBN碼包括9位數字、1位識別碼和3位分隔符&#xff0c;其規定格式如“x-xxx-xxxxx-x”&#xff0c;其中符號“-”就是分隔符&#xff08;鍵盤上的減號&#xff09;&#xff0c;最后一位是識別碼&#xff0c;例如0…

scrapy爬蟲啟示錄-小伙子老夫看你血氣方剛這本《爬蟲秘錄》就傳給你了

文章來源&#xff1a; IT源點 第一章 誤入歧途 每個學習爬蟲的人都有一顆愛美的心&#xff0c;俺也是一樣的。那么多的美眉圖片&#xff0c;不薅下來&#xff0c;沒了誰負責。于是夜里孤枕難眠的老男孩開始了他的擼碼之旅。從此在學習爬蟲&#xff0c;學習Python的道路上越走…

自己設置假期的日歷控件_在假期旅行時使用PC娛樂自己

自己設置假期的日歷控件Staying connected may be hard no matter what network you are on, and in flight Wi-Fi isn’t pervasive enough to count on. Here are tips and tricks to keep yourself entertained when unplugged and traveling. 無論您使用什么網絡&#xff0…

.Net CLR異常和windows C++ 異常調用棧簡析

楔子前面一篇研究了下C異常的&#xff0c;這篇來看下&#xff0c;CLR的異常內存模型&#xff0c;實際上都是一個模型&#xff0c;承繼自windows異常處理機制。不同的是&#xff0c;有VC編譯器(vcruntime.dll&#xff09;接管的部分&#xff0c;被CLR里面的函數ProcessCLRExcept…