Преглед изворни кода

improved server usage in aspnet so no exceptions are thrown on startup

release/3.x.x
Jan Eggers пре 7 година
родитељ
комит
ad77dc0bea
4 измењених фајлова са 36 додато и 12 уклоњено
  1. +1
    -0
      MQTTnet.Core/Server/IMqttServer.cs
  2. +3
    -0
      MQTTnet.Core/Server/MqttServer.cs
  3. +8
    -0
      MQTTnet.Core/Server/MqttServerStartedEventArgs.cs
  4. +24
    -12
      Tests/MQTTnet.TestApp.AspNetCore2/Startup.cs

+ 1
- 0
MQTTnet.Core/Server/IMqttServer.cs Прегледај датотеку

@@ -8,6 +8,7 @@ namespace MQTTnet.Core.Server
{
event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
event EventHandler<MqttServerStartedEventArgs> Started;

IList<ConnectedMqttClient> GetConnectedClients();
void Publish(IEnumerable<MqttApplicationMessage> applicationMessages);


+ 3
- 0
MQTTnet.Core/Server/MqttServer.cs Прегледај датотеку

@@ -49,6 +49,7 @@ namespace MQTTnet.Core.Server
return _clientSessionsManager.GetConnectedClients();
}

public event EventHandler<MqttServerStartedEventArgs> Started;
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
public event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
@@ -96,6 +97,8 @@ namespace MQTTnet.Core.Server
}

_logger.LogInformation("Started.");

Started?.Invoke(this, new MqttServerStartedEventArgs());
}

public async Task StopAsync()


+ 8
- 0
MQTTnet.Core/Server/MqttServerStartedEventArgs.cs Прегледај датотеку

@@ -0,0 +1,8 @@
using System;

namespace MQTTnet.Core.Server
{
public class MqttServerStartedEventArgs : EventArgs
{
}
}

+ 24
- 12
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) =>


Loading…
Откажи
Сачувај