使用BeetleX.MQTT構建服務

????已經有很長一段時間沒有寫代碼397ae693637a4e33564c02946e6a762c.png,為了不讓自己的代碼技能有所下降所以針對BeetleX擴展了一個MQTT協議來保持自己的代碼設計和編寫能力。接下來簡單介紹一下如何使用BeetleX.MQTT來構建對應的TCP或WebSocket服務。
????以下實現是針對MQTT 3.1.1版本,協議的實現也并不復雜就不介紹了,可以通過關注 https://github.com/beetlex-io/mqtt了解具體代碼。接下來分享使用BeetleX.MQTT實現TCP和Websocket服務

TCP服務

using BeetleX.MQTT.Messages;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace BeetleX.MQTT.Server
{class Program{private static ServerBuilder<MQTTApplication, MQTTUser, MQTTPacket> server;static void Main(string[] args){server = new ServerBuilder<MQTTApplication, MQTTUser, MQTTPacket>();server.ConsoleOutputLog = true;server.SetOptions(option =>{option.DefaultListen.Port = 9090;option.DefaultListen.Host = "127.0.0.1";option.LogLevel = EventArgs.LogType.Trace;}).OnMessageReceive<CONNECT>(e =>{e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.NetSession.RemoteEndPoint} connect name:{e.Message.UserName} password:{e.Message.Password}");e.Session.UserName = e.Message.UserName;e.Session.ID = e.Message.ClientID;CONNACK ack = new CONNACK();e.Return(ack);}).OnMessageReceive<SUBSCRIBE>(e =>{e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.Session.ID} subscribe {e.Message}");SUBACK ack = new SUBACK();ack.Identifier = e.Message.Identifier;ack.Status = QoSType.MostOnce;e.Return(ack);e.Application.RegisterSubscribe(e.Message, e.Session);}).OnMessageReceive<UNSUBSCRIBE>(e =>{e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.Session.ID} unsubscribe {e.Message}");UNSUBACK ack = new UNSUBACK();e.Return(ack);e.Application.UnRegisterSubscribe(e.Message, e.Session);}).OnMessageReceive<PUBLISH>(e =>{var data = Encoding.UTF8.GetString(e.Message.PayLoadData.Array, e.Message.PayLoadData.Offset, e.Message.PayLoadData.Count);e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.Session.ID} publish {e.Message.Topic}@ {e.Message.Identifier} data:{data}");PUBACK ack = new PUBACK();ack.Identifier = e.Message.Identifier;e.Return(ack);e.Application.Publish(e.Message);}).OnMessageReceive<PINGREQ>(e =>{PINGRESP resp = new PINGRESP();e.Return(resp);}).OnMessageReceive(e =>{}).Run();Console.Read();}}}

WebSocket服務

using BeetleX.MQTT.Messages;
using System;
using System.Text;namespace BeetleX.MQTT.WSServer
{class Program{private static MQTTWebsocketServer<MQTTApplication, MQTTUser> mServer;static void Main(string[] args){mServer = new MQTTWebsocketServer<MQTTApplication, MQTTUser>(8081);mServer.Setting((service, options) => {options.LogLevel = EventArgs.LogType.Trace;options.LogToConsole = true;options.WebSocketFrameSerializer = new MQTTFormater();});mServer.OnMessageReceive<CONNECT>(e =>{e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.NetSession.RemoteEndPoint} connect name:{e.Message.UserName} password:{e.Message.Password}");e.Session.UserName = e.Message.UserName;e.Session.ID = e.Message.ClientID;CONNACK ack = new CONNACK();e.Return(ack);}).OnMessageReceive<SUBSCRIBE>(e =>{e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.Session.ID} subscribe {e.Message}");SUBACK ack = new SUBACK();ack.Identifier = e.Message.Identifier;ack.Status = QoSType.MostOnce;e.Return(ack);e.Application.RegisterSubscribe(e.Message, e.Session);}).OnMessageReceive<UNSUBSCRIBE>(e =>{e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.Session.ID} unsubscribe {e.Message}");UNSUBACK ack = new UNSUBACK();e.Return(ack);e.Application.UnRegisterSubscribe(e.Message, e.Session);}).OnMessageReceive<PUBLISH>(e =>{var data = Encoding.UTF8.GetString(e.Message.PayLoadData.Array, e.Message.PayLoadData.Offset, e.Message.PayLoadData.Count);e.GetLoger(EventArgs.LogType.Info)?.Log(EventArgs.LogType.Info, e.NetSession, $"{e.Session.ID} publish {e.Message.Topic}@ {e.Message.Identifier} data:{data}");PUBACK ack = new PUBACK();ack.Identifier = e.Message.Identifier;e.Return(ack);e.Application.Publish(e.Message);}).OnMessageReceive<PINGREQ>(e =>{PINGRESP resp = new PINGRESP();e.Return(resp);}).OnMessageReceive(e =>{}).Run();Console.Read();}}
}
BeetleX

開源跨平臺通訊框架(支持TLS)

提供HTTP,Websocket,MQTT,Redis,RPC和服務網關開源組件

fd55c49ce23d51550cb991c47a598652.jpeg

https://beetlex-io.com

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

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

相關文章

vlc 視頻流跳數_如何解決在播放高清晰度視頻文件的VLC中跳過和滯后的問題

vlc 視頻流跳數VLC is the king of all media… it plays almost anything on any platform, any time, any place. It’s great. Lately, however, I’ve been having issues with VLC skipping whenever I’m playing high-def media streaming over a network. VLC是所有媒體…

求助:關于sql如何統計時間的問題

三、現在我們假設應用計時分為app應用和web應用&#xff0c;需要考慮如下幾個方面&#xff1a; &#xff08;1&#xff09;多時間段&#xff08;2&#xff09;表中有冗雜數據 &#xff08;3&#xff09;用戶是在web端和app端都登陸&#xff0c;這種類型的重復時間段只能取其一 …

onlyoffice中文字體下載

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/778 下劃線開頭的是頁面顯示中文的字體&#xff0c;不帶下劃線的是頁面顯示英文的字體 calibri.ttf -CalibriTimes New Roman.ttf _FANGSONG.otf -仿宋FANGSONG.otf_FS_GB2312.otf -仿宋_GB2312_HWZS.otf -華文…

onlyoffice 20并發限制處理

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/31 開源版本連續打開20個頁面就會彈出該提示。 一.方案一 修改六個app.js文件 find ./ -name app.js 目錄下所有app.js文件&#xff0c;對代碼行進行修改&#xff0c;只是經過簡單的測試&#xff0c;希望發現…

[Linux環境]-centos7下安裝jdk1.8.0_141流程.

1.查看當前虛擬機java環境版本: java -version [rootcentos-linux-7 bin]# java -version java version "1.7.0_111" OpenJDK Runtime Environment (rhel-2.6.7.2.el7_2-x86_64 u111-b01) OpenJDK 64-Bit Server VM (build 24.111-b01, mixed mode) 2.查看當前安裝版…

石橋圖里的一個故事

唐朝詭事錄有一個石橋圖章節&#xff0c;里面記載了一個故事。十年前&#xff0c;南州鄉賢謝員外的兩個兒子&#xff0c;要求謝家的老仆人帶自己去湖里游玩。哪知游玩到湖中間&#xff0c;突然下起了瓢潑大雨&#xff0c;員外家的一個兒子被驚嚇掉落水里。老仆人的兒子趕忙去救…

onlyoffice更新中文字體總結

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/760 1. 通過字體修改軟件FontCreator修改字體名稱 &#xff08;1&#xff09;下載fontcreat.exe &#xff08;2&#xff09;將Windows/fonts 下的字體文件拷貝至 我的文檔/fonts (3) 使用fontcreat打開字體…

ubuntu 鍵盤快捷鍵_如何使用鍵盤快捷鍵在Ubuntu中提高生產力

ubuntu 鍵盤快捷鍵diceareawesome1/Shutterstock.comdiceareawesome1 / Shutterstock.comWe’re always looking for new ways to speed up everyday tasks in Ubuntu. We’ll show you some keyboard shortcuts you might not have known about, and show you how to make you…

核心編程之十一章的11-9

def add(x,y): a x y def average(): list1 [] list1.append(a/2) print(list1) return a #為什么return a 放在這里呢&#xff0c;因為如果放在外面的話&#xff0c;根據return的屬性&#xff0c;會結束嵌套數函數 return average() …

Ubuntu 桌面系統升級

本文介紹 Ubuntu 桌面系統升級的兩種方式&#xff0c;通過 UI 或命令行的方式&#xff0c;演示為 20.04 升級為 22.04。并介紹了 windows 的 Linux 子系統 wsl 的升級注意事項。背景之前在學習 ROS2 時&#xff0c;安裝 ros-humble-desktop 出現依賴錯誤&#xff1a;無法修正錯…

onlyoffice修改左上角的logo

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/770 1. 商用版config里配置就能修改logo "editorConfig": {"customization": {"logo": {"image": "https://example.com/logo.png","imageEmbedded&q…

pidgin qq_Pidgin入門指南,通用消息客戶端

pidgin qqIf you find chatting with multiple chat clients troublesome, then Pidgin is the tool for you. In today’s article, we’ll show you how to connect to popular chat networks, encrypt your conversations, and render mathematical formula in Pidgin. 如果…

NumPy學習_00 ndarray的創建

1.使用array()函數創建數組 參數可以為&#xff1a;單層或嵌套列表&#xff1b;嵌套元組或元組列表&#xff1b;元組或列表組成的列表 # 導入numpy庫import numpy as np # 由單層列表創建a np.array([1,2,3])print(a) [1 2 3] # 由嵌套列表創建b np.array([[1.3,2.4], [0.3,4…

記一次 .NET 某自動化采集軟件 崩潰分析

一&#xff1a;背景 1.講故事前段時間有位朋友找到我&#xff0c;說他的程序在客戶的機器上跑著跑著會出現偶發卡死&#xff0c;然后就崩掉了&#xff0c;但在本地怎么也沒復現&#xff0c;dump也抓到了&#xff0c;讓我幫忙看下到底怎么回事&#xff0c;其實崩潰類的dump也有簡…

onlyoffice修改字號

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/758 :/var/www/onlyoffice/documentserver/web-apps/apps/documenteditor/main/app.js 里找到{value:22,displayValue:"22"} 把displayValue對應的值換成漢字字體 小二等 其實中文數字&#xff08;字…

大數據

大數據技術的快速發展&#xff0c;對現如今人們的思維方式產生了巨大的改變。 首先&#xff0c;大數據的發展&#xff0c;改善了人們思維的局限性。在過去&#xff0c;數據流通速度慢&#xff0c;人們獲取的數據資源有限&#xff0c;所以在看待事物方面&#xff0c;基于過去固有…

如何在 .NET MAUI 中加載 json 文件?

引言:按.NET core傳統方式添加 AddJsonFile("appsettings.json") 在windows平臺和ssr工作正常,但是在 ios 和 android 無法用這種方式,因為資源生成方式不一樣. 使用內置資源方式不夠靈活而且 ios 平臺會提示不能復制 json 文件到目錄,于是進行了幾天的研究,終于能正…

onlyOfice取消上傳文件大小的限制

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/757 使用onlyOfice的時候&#xff0c;在打開的文件中&#xff0c;對文件的大小有限制的&#xff0c;可以在服務中修改被限制的大小&#xff0c;在服務上有/etc/onlyoffice/documentserver/default.json的文件&a…

SSH整合注解版(Spring+Struts2+Hibernate)

整體架構&#xff1a; pom.xml 引入maven節點&#xff1a; <dependencies><!--單測--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.3</version><scope>test</scope><…

定時插座動一下就斷_使用插座定時器在某些時候自動將您的Amazon Echo靜音

定時插座動一下就斷The Amazon Echo is an always-listening voice-controlled virtual assistant, but if there are times you’d rather not listen (or be listened to) by the Echo, here’s how to automatically mute it at certain times of the day. Amazon Echo是一個…