mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 01:03:01 +08:00
73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using MQTTnet.Core.Adapter;
|
|
using MQTTnet.Core.Server;
|
|
using MQTTnet.Core.Client;
|
|
|
|
namespace MQTTnet.Core.Tests
|
|
{
|
|
public class TestMqttServerAdapter : IMqttServerAdapter
|
|
{
|
|
public event EventHandler<MqttServerAdapterClientAcceptedEventArgs> ClientAccepted;
|
|
|
|
public async Task<MqttClient> ConnectTestClient(IMqttServer server, string clientId, MqttApplicationMessage willMessage = null)
|
|
{
|
|
var adapterA = new TestMqttCommunicationAdapter();
|
|
var adapterB = new TestMqttCommunicationAdapter();
|
|
adapterA.Partner = adapterB;
|
|
adapterB.Partner = adapterA;
|
|
|
|
var client = new MqttClient(new MqttCommunicationAdapterFactory(adapterA), new TestLogger<MqttClient>(), new MqttPacketDispatcher(new TestLogger<MqttPacketDispatcher>()));
|
|
var connected = WaitForClientToConnect(server, clientId);
|
|
|
|
FireClientAcceptedEvent(adapterB);
|
|
|
|
var options = new MqttClientOptions
|
|
{
|
|
ClientId = clientId,
|
|
WillMessage = willMessage,
|
|
ChannelOptions = new MqttClientTcpOptions()
|
|
};
|
|
|
|
options.ChannelOptions = new MqttClientTcpOptions();
|
|
|
|
await client.ConnectAsync(options);
|
|
await connected;
|
|
|
|
return client;
|
|
}
|
|
|
|
private static Task WaitForClientToConnect(IMqttServer s, string clientId)
|
|
{
|
|
var tcs = new TaskCompletionSource<object>();
|
|
|
|
void Handler(object sender, Server.MqttClientConnectedEventArgs args)
|
|
{
|
|
if (args.Client.ClientId == clientId)
|
|
{
|
|
s.ClientConnected -= Handler;
|
|
tcs.SetResult(null);
|
|
}
|
|
}
|
|
|
|
s.ClientConnected += Handler;
|
|
|
|
return tcs.Task;
|
|
}
|
|
|
|
private void FireClientAcceptedEvent(IMqttCommunicationAdapter adapter)
|
|
{
|
|
ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(adapter));
|
|
}
|
|
|
|
public Task StartAsync(MqttServerOptions options)
|
|
{
|
|
return Task.FromResult(0);
|
|
}
|
|
|
|
public Task StopAsync()
|
|
{
|
|
return Task.FromResult(0);
|
|
}
|
|
}
|
|
} |