mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-17 01:33:03 +08:00
51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
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<MqttConnectionContext> Context { get; } = new TaskCompletionSource<MqttConnectionContext>();
|
|
public Func<IMqttChannelAdapter, Task> 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()
|
|
{
|
|
}
|
|
}
|
|
}
|