mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-26 16:24:10 +08:00
improved server usage in aspnet so no exceptions are thrown on startup
This commit is contained in:
@@ -8,6 +8,7 @@ namespace MQTTnet.Core.Server
|
|||||||
{
|
{
|
||||||
event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
|
event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
|
||||||
event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
|
event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
|
||||||
|
event EventHandler<MqttServerStartedEventArgs> Started;
|
||||||
|
|
||||||
IList<ConnectedMqttClient> GetConnectedClients();
|
IList<ConnectedMqttClient> GetConnectedClients();
|
||||||
void Publish(IEnumerable<MqttApplicationMessage> applicationMessages);
|
void Publish(IEnumerable<MqttApplicationMessage> applicationMessages);
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ namespace MQTTnet.Core.Server
|
|||||||
return _clientSessionsManager.GetConnectedClients();
|
return _clientSessionsManager.GetConnectedClients();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public event EventHandler<MqttServerStartedEventArgs> Started;
|
||||||
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
|
public event EventHandler<MqttClientConnectedEventArgs> ClientConnected;
|
||||||
public event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
|
public event EventHandler<MqttClientDisconnectedEventArgs> ClientDisconnected;
|
||||||
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
|
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
|
||||||
@@ -96,6 +97,8 @@ namespace MQTTnet.Core.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Started.");
|
_logger.LogInformation("Started.");
|
||||||
|
|
||||||
|
Started?.Invoke(this, new MqttServerStartedEventArgs());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StopAsync()
|
public async Task StopAsync()
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MQTTnet.Core.Server
|
||||||
|
{
|
||||||
|
public class MqttServerStartedEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Builder;
|
|||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using MQTTnet.AspNetCore;
|
using MQTTnet.AspNetCore;
|
||||||
using MQTTnet.Core;
|
using MQTTnet.Core;
|
||||||
|
|
||||||
@@ -21,21 +20,34 @@ namespace MQTTnet.TestApp.AspNetCore2
|
|||||||
}
|
}
|
||||||
|
|
||||||
// In class _Startup_ of the ASP.NET Core 2.0 project.
|
// 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.UseMqttEndpoint();
|
||||||
app.UseMqttServer(async server =>
|
app.UseMqttServer(server =>
|
||||||
{
|
{
|
||||||
var msg = new MqttApplicationMessageBuilder()
|
server.Started += async (sender, args) =>
|
||||||
.WithPayload("Mqtt is awesome")
|
|
||||||
.WithTopic("message");
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
{
|
||||||
server.PublishAsync(msg.Build()).Wait();
|
var msg = new MqttApplicationMessageBuilder()
|
||||||
await Task.Delay(TimeSpan.FromSeconds(2));
|
.WithPayload("Mqtt is awesome")
|
||||||
msg.WithPayload("Mqtt is still awesome at " + DateTime.Now);
|
.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) =>
|
app.Use((context, next) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user