diff --git a/MQTTnet.Core/Server/IMqttServer.cs b/MQTTnet.Core/Server/IMqttServer.cs index 71af0ee..13c614f 100644 --- a/MQTTnet.Core/Server/IMqttServer.cs +++ b/MQTTnet.Core/Server/IMqttServer.cs @@ -8,6 +8,7 @@ namespace MQTTnet.Core.Server { event EventHandler ClientConnected; event EventHandler ClientDisconnected; + event EventHandler Started; IList GetConnectedClients(); void Publish(IEnumerable applicationMessages); diff --git a/MQTTnet.Core/Server/MqttServer.cs b/MQTTnet.Core/Server/MqttServer.cs index 1e05764..bd06a3a 100644 --- a/MQTTnet.Core/Server/MqttServer.cs +++ b/MQTTnet.Core/Server/MqttServer.cs @@ -49,6 +49,7 @@ namespace MQTTnet.Core.Server return _clientSessionsManager.GetConnectedClients(); } + public event EventHandler Started; public event EventHandler ClientConnected; public event EventHandler ClientDisconnected; public event EventHandler ApplicationMessageReceived; @@ -96,6 +97,8 @@ namespace MQTTnet.Core.Server } _logger.LogInformation("Started."); + + Started?.Invoke(this, new MqttServerStartedEventArgs()); } public async Task StopAsync() diff --git a/MQTTnet.Core/Server/MqttServerStartedEventArgs.cs b/MQTTnet.Core/Server/MqttServerStartedEventArgs.cs new file mode 100644 index 0000000..8f46bde --- /dev/null +++ b/MQTTnet.Core/Server/MqttServerStartedEventArgs.cs @@ -0,0 +1,8 @@ +using System; + +namespace MQTTnet.Core.Server +{ + public class MqttServerStartedEventArgs : EventArgs + { + } +} diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs b/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs index d321fd1..55c57a1 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs +++ b/Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs @@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; -using Microsoft.Extensions.Logging; using MQTTnet.AspNetCore; using MQTTnet.Core; @@ -21,21 +20,34 @@ namespace MQTTnet.TestApp.AspNetCore2 } // In class _Startup_ of the ASP.NET Core 2.0 project. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) + public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMqttEndpoint(); - app.UseMqttServer(async server => + app.UseMqttServer(server => { - var msg = new MqttApplicationMessageBuilder() - .WithPayload("Mqtt is awesome") - .WithTopic("message"); - - while (true) + server.Started += async (sender, args) => { - server.PublishAsync(msg.Build()).Wait(); - await Task.Delay(TimeSpan.FromSeconds(2)); - msg.WithPayload("Mqtt is still awesome at " + DateTime.Now); - } + var msg = new MqttApplicationMessageBuilder() + .WithPayload("Mqtt is awesome") + .WithTopic("message"); + + while (true) + { + try + { + await server.PublishAsync(msg.Build()); + msg.WithPayload("Mqtt is still awesome at " + DateTime.Now); + } + catch (Exception e) + { + Console.WriteLine(e); + } + finally + { + await Task.Delay(TimeSpan.FromSeconds(2)); + } + } + }; }); app.Use((context, next) =>