using Microsoft.AspNetCore.Connections; using MQTTnet.Adapter; using MQTTnet.Formatter; using MQTTnet.Server; using System; using System.Threading.Tasks; namespace MQTTnet.AspNetCore.Tests.Mockups { public class ConnectionHandlerMockup: IMqttServerAdapter { public TaskCompletionSource Context { get; } = new TaskCompletionSource(); public Func ClientHandler { get; set; } public ConnectionHandlerMockup() { } public async Task OnConnectedAsync(ConnectionContext connection) { try { var writer = new SpanBasedMqttPacketWriter(); var formatter = new MqttPacketFormatterAdapter(writer); var context = new MqttConnectionContext(formatter, connection); Context.TrySetResult(context); await ClientHandler(context); } catch (Exception ex) { Context.TrySetException(ex); } } public Task StartAsync(IMqttServerOptions options) { return Task.CompletedTask; } public Task StopAsync() { return Task.CompletedTask; } public void Dispose() { } } }