From eaf27c1e1268a9417d96fd4d8925969aaaf19fcb Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Thu, 9 Nov 2017 00:03:22 +0100 Subject: [PATCH] Update documentation --- MQTTnet.Core/Server/IMqttServer.cs | 1 - MQTTnet.Core/Server/MqttServer.cs | 1 - Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs | 10 ++-- Tests/MQTTnet.TestApp.NetCore/ServerTest.cs | 23 +++++-- .../MainPage.xaml.cs | 60 ++++++++++++++++++- 5 files changed, 84 insertions(+), 11 deletions(-) diff --git a/MQTTnet.Core/Server/IMqttServer.cs b/MQTTnet.Core/Server/IMqttServer.cs index 7d76c23..71af0ee 100644 --- a/MQTTnet.Core/Server/IMqttServer.cs +++ b/MQTTnet.Core/Server/IMqttServer.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using MQTTnet.Core.Client; namespace MQTTnet.Core.Server { diff --git a/MQTTnet.Core/Server/MqttServer.cs b/MQTTnet.Core/Server/MqttServer.cs index f5a9527..1e05764 100644 --- a/MQTTnet.Core/Server/MqttServer.cs +++ b/MQTTnet.Core/Server/MqttServer.cs @@ -6,7 +6,6 @@ using MQTTnet.Core.Adapter; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System.Linq; -using Microsoft.Extensions.DependencyInjection; namespace MQTTnet.Core.Server { diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs b/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs index 18d121c..d321fd1 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs +++ b/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs @@ -8,17 +8,19 @@ using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; using MQTTnet.AspNetCore; using MQTTnet.Core; -using MQTTnet.Core.Client; namespace MQTTnet.TestApp.AspNetCore2 { public class Startup { + // In class _Startup_ of the ASP.NET Core 2.0 project. + public void ConfigureServices(IServiceCollection services) { services.AddHostedMqttServer(); } + // In class _Startup_ of the ASP.NET Core 2.0 project. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseMqttEndpoint(); @@ -49,11 +51,11 @@ namespace MQTTnet.TestApp.AspNetCore2 app.UseStaticFiles(); - app.UseStaticFiles( new StaticFileOptions() + app.UseStaticFiles(new StaticFileOptions { RequestPath = "/node_modules", - FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, "node_modules" ) ) - } ); + FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "node_modules")) + }); } } } diff --git a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs index 0a7d557..f4a18cd 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs @@ -35,14 +35,29 @@ namespace MQTTnet.TestApp.NetCore options.Storage = new RetainedMessageHandler(); + // Extend the timestamp for all messages from clients. options.ApplicationMessageInterceptor = context => { if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#")) { - // Replace the payload with the timestamp. But also extending a JSON - // based payload with the timestamp is a suitable use case. - context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")); - } + // Replace the payload with the timestamp. But also extending a JSON + // based payload with the timestamp is a suitable use case. + context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O")); + } + }; + // Protect several topics from being subscribed from every client. + options.SubscriptionsInterceptor = context => + { + if (context.TopicFilter.Topic.StartsWith("admin/foo/bar") && context.ClientId != "theAdmin") + { + context.AcceptSubscription = false; + } + + if (context.TopicFilter.Topic.StartsWith("the/secret/stuff") && context.ClientId != "Imperator") + { + context.AcceptSubscription = false; + context.CloseConnection = true; + } }; }); diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index 47ee258..1a12f95 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -264,7 +264,7 @@ namespace MQTTnet.TestApp.UniversalWindows var mqttClient = serviceProvider.GetRequiredService(); } - + { // Create a new MQTT client. var factory = new MqttFactory(); @@ -396,6 +396,64 @@ namespace MQTTnet.TestApp.UniversalWindows return new ChainValidationResult[0]; }; + { + // Start a MQTT server. + var mqttServer = new MqttFactory().CreateMqttServer(); + await mqttServer.StartAsync(); + Console.WriteLine("Press any key to exit."); + Console.ReadLine(); + await mqttServer.StopAsync(); + } + + { + // Configure MQTT server. + var mqttServer = new MqttFactory().CreateMqttServer(options => + { + options.ConnectionBacklog = 100; + options.DefaultEndpointOptions.Port = 1884; + options.ConnectionValidator = packet => + { + if (packet.ClientId != "Highlander") + { + return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; + } + + return MqttConnectReturnCode.ConnectionAccepted; + }; + }); + } + + { + // Setup client validator. + var mqttServer = new MqttFactory().CreateMqttServer(options => + { + options.ConnectionValidator = c => + { + if (c.ClientId.Length < 10) + { + return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; + } + + if (c.Username != "mySecretUser") + { + return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; + } + + if (c.Password != "mySecretPassword") + { + return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; + } + + return MqttConnectReturnCode.ConnectionAccepted; + }; + }); + } + + { + // Create a new MQTT server. + var mqttServer = new MqttFactory().CreateMqttServer(); + } + } private async void StartServer(object sender, RoutedEventArgs e)