mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-28 01:02:37 +08:00
Move events to server level instead of options.
This commit is contained in:
@@ -36,9 +36,9 @@ namespace MQTTnet.AspNetCore.Extensions
|
||||
return app;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<IMqttServer> configure)
|
||||
public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<MqttServer> configure)
|
||||
{
|
||||
var server = app.ApplicationServices.GetRequiredService<IMqttServer>();
|
||||
var server = app.ApplicationServices.GetRequiredService<MqttServer>();
|
||||
|
||||
configure(server);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace MQTTnet.AspNetCore.Extensions
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, IMqttServerOptions options)
|
||||
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, MqttServerOptions options)
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace MQTTnet.AspNetCore.Extensions
|
||||
|
||||
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure)
|
||||
{
|
||||
services.AddSingleton<IMqttServerOptions>(s =>
|
||||
services.AddSingleton<MqttServerOptions>(s =>
|
||||
{
|
||||
var builder = new MqttServerOptionsBuilder();
|
||||
configure(builder);
|
||||
@@ -38,7 +38,7 @@ namespace MQTTnet.AspNetCore.Extensions
|
||||
|
||||
public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)
|
||||
{
|
||||
services.AddSingleton<IMqttServerOptions>(s =>
|
||||
services.AddSingleton<MqttServerOptions>(s =>
|
||||
{
|
||||
var builder = new AspNetMqttServerOptionsBuilder(s);
|
||||
configure(builder);
|
||||
@@ -50,17 +50,17 @@ namespace MQTTnet.AspNetCore.Extensions
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHostedMqttServer<TOptions>(this IServiceCollection services)
|
||||
where TOptions : class, IMqttServerOptions
|
||||
{
|
||||
services.AddSingleton<IMqttServerOptions, TOptions>();
|
||||
// public static IServiceCollection AddHostedMqttServer<TOptions>(this IServiceCollection services)
|
||||
// where TOptions : MqttServerOptions
|
||||
// {
|
||||
// services.AddSingleton<MqttServerOptions, TOptions>();
|
||||
//
|
||||
// services.AddHostedMqttServer();
|
||||
//
|
||||
// return services;
|
||||
// }
|
||||
|
||||
services.AddHostedMqttServer();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddHostedMqttServer(this IServiceCollection services)
|
||||
static IServiceCollection AddHostedMqttServer(this IServiceCollection services)
|
||||
{
|
||||
var logger = new MqttNetEventLogger();
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace MQTTnet.AspNetCore
|
||||
}
|
||||
}
|
||||
|
||||
public Task StartAsync(IMqttServerOptions options)
|
||||
public Task StartAsync(MqttServerOptions options)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -1,32 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Client.Publishing;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Server;
|
||||
using MQTTnet.Server.Status;
|
||||
|
||||
namespace MQTTnet.AspNetCore
|
||||
{
|
||||
public sealed class MqttHostedServer : IMqttServer, IHostedService
|
||||
{
|
||||
readonly MqttServer _mqttServer;
|
||||
readonly IMqttServerOptions _options;
|
||||
|
||||
public MqttHostedServer(IMqttServerOptions options, IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger)
|
||||
public MqttHostedServer(MqttServerOptions options, IEnumerable<IMqttServerAdapter> adapters, IMqttNetLogger logger)
|
||||
{
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
|
||||
_mqttServer = new MqttServer(adapters, logger);
|
||||
_mqttServer = new MqttServer(options, adapters, logger);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_ = _mqttServer.StartAsync(_options);
|
||||
_ = _mqttServer.StartAsync();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -34,22 +28,10 @@ namespace MQTTnet.AspNetCore
|
||||
{
|
||||
return _mqttServer.StopAsync();
|
||||
}
|
||||
|
||||
public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler
|
||||
{
|
||||
get => _mqttServer.ApplicationMessageReceivedHandler;
|
||||
set => _mqttServer.ApplicationMessageReceivedHandler = value;
|
||||
}
|
||||
|
||||
public event Func<MqttApplicationMessageReceivedEventArgs, Task> ApplicationMessageReceivedAsync
|
||||
{
|
||||
add => _mqttServer.ApplicationMessageReceivedAsync += value;
|
||||
remove => _mqttServer.ApplicationMessageReceivedAsync -= value;
|
||||
}
|
||||
|
||||
public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
|
||||
public Task<MqttClientPublishResult> PublishAsync(string senderClientId, MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
|
||||
{
|
||||
return _mqttServer.PublishAsync(applicationMessage, cancellationToken);
|
||||
return _mqttServer.PublishAsync(senderClientId, applicationMessage, cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@@ -58,45 +40,7 @@ namespace MQTTnet.AspNetCore
|
||||
}
|
||||
|
||||
public bool IsStarted => _mqttServer.IsStarted;
|
||||
|
||||
public IMqttServerStartedHandler StartedHandler
|
||||
{
|
||||
get => _mqttServer.StartedHandler;
|
||||
set => _mqttServer.StartedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerStoppedHandler StoppedHandler
|
||||
{
|
||||
get => _mqttServer.StoppedHandler;
|
||||
set => _mqttServer.StoppedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientConnectedHandler ClientConnectedHandler
|
||||
{
|
||||
get => _mqttServer.ClientConnectedHandler;
|
||||
set => _mqttServer.ClientConnectedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientDisconnectedHandler ClientDisconnectedHandler
|
||||
{
|
||||
get => _mqttServer.ClientDisconnectedHandler;
|
||||
set => _mqttServer.ClientDisconnectedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientSubscribedTopicHandler ClientSubscribedTopicHandler
|
||||
{
|
||||
get => _mqttServer.ClientSubscribedTopicHandler;
|
||||
set => _mqttServer.ClientSubscribedTopicHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientUnsubscribedTopicHandler ClientUnsubscribedTopicHandler
|
||||
{
|
||||
get => _mqttServer.ClientUnsubscribedTopicHandler;
|
||||
set => _mqttServer.ClientUnsubscribedTopicHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerOptions Options => _mqttServer.Options;
|
||||
|
||||
|
||||
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
|
||||
{
|
||||
return _mqttServer.GetClientStatusAsync();
|
||||
@@ -127,9 +71,9 @@ namespace MQTTnet.AspNetCore
|
||||
return _mqttServer.UnsubscribeAsync(clientId, topicFilters);
|
||||
}
|
||||
|
||||
public Task StartAsync(IMqttServerOptions options)
|
||||
public Task StartAsync()
|
||||
{
|
||||
return _mqttServer.StartAsync(options);
|
||||
return _mqttServer.StartAsync();
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace MQTTnet.AspNetCore
|
||||
|
||||
public Func<IMqttChannelAdapter, Task> ClientHandler { get; set; }
|
||||
|
||||
public Task StartAsync(IMqttServerOptions options)
|
||||
public Task StartAsync(MqttServerOptions options)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user