From e33495010e185de7e77c95b12db50c554c25f425 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Wed, 13 Jul 2022 15:29:26 +0200 Subject: [PATCH] Update samples. --- Samples/Client/Client_Connection_Samples.cs | 4 + Samples/Client/Client_Publish_Samples.cs | 4 + Samples/Client/Client_Subscribe_Samples.cs | 4 + Samples/Diagnostics/Logger_Samples.cs | 4 + .../Diagnostics/PackageInspection_Samples.cs | 4 + .../Managed_Client_Simple_Samples.cs | 8 ++ Samples/RpcClient/RpcClient_Samples.cs | 4 + Samples/Server/Server_Asp_Net_Samples.cs | 93 +++++++++++++++++++ .../Server_Retained_Messages_Samples.cs | 8 ++ Samples/Server/Server_Simple_Samples.cs | 4 + 10 files changed, 137 insertions(+) create mode 100644 Samples/Server/Server_Asp_Net_Samples.cs diff --git a/Samples/Client/Client_Connection_Samples.cs b/Samples/Client/Client_Connection_Samples.cs index 4f6149a3..75ef6e61 100644 --- a/Samples/Client/Client_Connection_Samples.cs +++ b/Samples/Client/Client_Connection_Samples.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using System.Security.Authentication; using MQTTnet.Client; using MQTTnet.Formatter; diff --git a/Samples/Client/Client_Publish_Samples.cs b/Samples/Client/Client_Publish_Samples.cs index 7bfe72bb..ed0e367d 100644 --- a/Samples/Client/Client_Publish_Samples.cs +++ b/Samples/Client/Client_Publish_Samples.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using MQTTnet.Client; namespace MQTTnet.Samples.Client; diff --git a/Samples/Client/Client_Subscribe_Samples.cs b/Samples/Client/Client_Subscribe_Samples.cs index 7cedf056..fdde2719 100644 --- a/Samples/Client/Client_Subscribe_Samples.cs +++ b/Samples/Client/Client_Subscribe_Samples.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using MQTTnet.Client; using MQTTnet.Samples.Helpers; diff --git a/Samples/Diagnostics/Logger_Samples.cs b/Samples/Diagnostics/Logger_Samples.cs index 313d3413..6cd0aa3e 100644 --- a/Samples/Diagnostics/Logger_Samples.cs +++ b/Samples/Diagnostics/Logger_Samples.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using System.Text; using MQTTnet.Diagnostics; diff --git a/Samples/Diagnostics/PackageInspection_Samples.cs b/Samples/Diagnostics/PackageInspection_Samples.cs index 58c7a546..c4d4b7ac 100644 --- a/Samples/Diagnostics/PackageInspection_Samples.cs +++ b/Samples/Diagnostics/PackageInspection_Samples.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using MQTTnet.Diagnostics; namespace MQTTnet.Samples.Diagnostics; diff --git a/Samples/ManagedClient/Managed_Client_Simple_Samples.cs b/Samples/ManagedClient/Managed_Client_Simple_Samples.cs index 39a6892e..723a33a6 100644 --- a/Samples/ManagedClient/Managed_Client_Simple_Samples.cs +++ b/Samples/ManagedClient/Managed_Client_Simple_Samples.cs @@ -1,3 +1,11 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using MQTTnet.Client; using MQTTnet.Extensions.ManagedClient; diff --git a/Samples/RpcClient/RpcClient_Samples.cs b/Samples/RpcClient/RpcClient_Samples.cs index c895014d..8a182fc7 100644 --- a/Samples/RpcClient/RpcClient_Samples.cs +++ b/Samples/RpcClient/RpcClient_Samples.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using MQTTnet.Client; using MQTTnet.Extensions.Rpc; using MQTTnet.Protocol; diff --git a/Samples/Server/Server_Asp_Net_Samples.cs b/Samples/Server/Server_Asp_Net_Samples.cs new file mode 100644 index 00000000..a343159e --- /dev/null +++ b/Samples/Server/Server_Asp_Net_Samples.cs @@ -0,0 +1,93 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using MQTTnet.AspNetCore; + +namespace MQTTnet.Samples.Server; + +public static class Server_Asp_Net_Samples +{ + public static Task Start_Server_With_WebSockets_Support() + { + /* + * This sample starts a minimal ASP.NET Webserver including a hosted MQTT server. + */ + var host = Host.CreateDefaultBuilder(Array.Empty()) + .ConfigureWebHostDefaults( + webBuilder => + { + webBuilder.UseKestrel( + o => + { + // This will allow MQTT connections based on TCP port 1883. + o.ListenAnyIP(1883, l => l.UseMqtt()); + + // This will allow MQTT connections based on HTTP WebSockets with URI "localhost:5000/mqtt" + // See code below for URI configuration. + o.ListenAnyIP(5000); // Default HTTP pipeline + }); + + webBuilder.UseStartup(); + }); + + return host.RunConsoleAsync(); + } + + sealed class Startup + { + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseRouting(); + + app.UseEndpoints( + endpoints => + { + endpoints.MapConnectionHandler( + "/mqtt", + httpConnectionDispatcherOptions => httpConnectionDispatcherOptions.WebSockets.SubProtocolSelector = + protocolList => protocolList.FirstOrDefault() ?? string.Empty); + }); + + app.UseMqttServer( + server => + { + /* + * Attach event handlers etc. if required. + */ + + server.ValidatingConnectionAsync += e => + { + Console.WriteLine($"Client '{e.ClientId}' wants to connect. Accepting!"); + return Task.CompletedTask; + }; + + server.ClientConnectedAsync += e => + { + Console.WriteLine($"Client '{e.ClientId}' connected."); + return Task.CompletedTask; + }; + }); + } + + public void ConfigureServices(IServiceCollection services) + { + services.AddHostedMqttServer( + optionsBuilder => + { + optionsBuilder.WithDefaultEndpoint(); + }); + + services.AddMqttConnectionHandler(); + services.AddConnections(); + } + } +} \ No newline at end of file diff --git a/Samples/Server/Server_Retained_Messages_Samples.cs b/Samples/Server/Server_Retained_Messages_Samples.cs index e0f2d83a..e392460f 100644 --- a/Samples/Server/Server_Retained_Messages_Samples.cs +++ b/Samples/Server/Server_Retained_Messages_Samples.cs @@ -1,3 +1,11 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using System.Text.Json; namespace MQTTnet.Samples.Server; diff --git a/Samples/Server/Server_Simple_Samples.cs b/Samples/Server/Server_Simple_Samples.cs index 14f7234a..e4d63eb4 100644 --- a/Samples/Server/Server_Simple_Samples.cs +++ b/Samples/Server/Server_Simple_Samples.cs @@ -2,6 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +// ReSharper disable UnusedType.Global +// ReSharper disable UnusedMember.Global +// ReSharper disable InconsistentNaming + using MQTTnet.Diagnostics; using MQTTnet.Protocol; using MQTTnet.Server;