mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Refactor options
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using MQTTnet.Core.Adapter;
|
||||
using MQTTnet.Core.Client;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
public class MqttCommunicationAdapterFactory : IMqttCommunicationAdapterFactory
|
||||
{
|
||||
private readonly IMqttCommunicationAdapter _adapter;
|
||||
|
||||
public MqttCommunicationAdapterFactory(IMqttCommunicationAdapter adapter)
|
||||
{
|
||||
_adapter = adapter;
|
||||
}
|
||||
|
||||
public IMqttCommunicationAdapter CreateMqttCommunicationAdapter(MqttClientOptions options)
|
||||
{
|
||||
return _adapter;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Core.Adapter;
|
||||
using MQTTnet.Core.Channel;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Serializer;
|
||||
@@ -411,7 +410,7 @@ namespace MQTTnet.Core.Tests
|
||||
_stream.Position = 0;
|
||||
}
|
||||
|
||||
public Task ConnectAsync(MqttClientOptions options)
|
||||
public Task ConnectAsync()
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Core.Tests
|
||||
{
|
||||
|
||||
@@ -4,7 +4,6 @@ using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Adapter;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Serializer;
|
||||
|
||||
@@ -18,7 +17,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
public IMqttPacketSerializer PacketSerializer { get; } = new MqttPacketSerializer();
|
||||
|
||||
public Task ConnectAsync(TimeSpan timeout, MqttClientOptions options)
|
||||
public Task ConnectAsync(TimeSpan timeout)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
@@ -17,10 +17,11 @@ namespace MQTTnet.Core.Tests
|
||||
adapterA.Partner = adapterB;
|
||||
adapterB.Partner = adapterA;
|
||||
|
||||
var client = new MqttClient(new MqttClientOptions() { ClientId = clientId }, adapterA);
|
||||
var client = new MqttClient(new MqttCommunicationAdapterFactory(adapterA));
|
||||
var connected = WaitForClientToConnect(server, clientId);
|
||||
|
||||
FireClientAcceptedEvent(adapterB);
|
||||
await client.ConnectAsync(willMessage);
|
||||
await client.ConnectAsync(new MqttClientTcpOptions { ClientId = clientId, WillMessage = willMessage });
|
||||
await connected;
|
||||
|
||||
return client;
|
||||
@@ -30,17 +31,16 @@ namespace MQTTnet.Core.Tests
|
||||
{
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
|
||||
EventHandler<MqttClientConnectedEventArgs> handler = null;
|
||||
handler = (sender, args) =>
|
||||
void Handler(object sender, MqttClientConnectedEventArgs args)
|
||||
{
|
||||
if (args.Client.ClientId == clientId)
|
||||
{
|
||||
s.ClientConnected -= handler;
|
||||
s.ClientConnected -= Handler;
|
||||
tcs.SetResult(null);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
s.ClientConnected += handler;
|
||||
s.ClientConnected += Handler;
|
||||
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
@@ -46,12 +46,11 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
try
|
||||
{
|
||||
var options = new MqttClientOptions
|
||||
var options = new MqttClientWebSocketOptions
|
||||
{
|
||||
Server = "localhost",
|
||||
Uri = "localhost",
|
||||
ClientId = "XYZ",
|
||||
CleanSession = true,
|
||||
ConnectionType = MqttConnectionType.Ws
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttClient(options);
|
||||
@@ -84,7 +83,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -94,7 +93,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
{
|
||||
try
|
||||
{
|
||||
var options = new MqttClientOptions
|
||||
var options = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "localhost",
|
||||
ClientId = "XYZ",
|
||||
@@ -64,7 +64,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -74,7 +74,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
@@ -52,13 +52,18 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
|
||||
try
|
||||
{
|
||||
var options = new MqttClientOptions
|
||||
var options = new MqttClientWebSocketOptions
|
||||
{
|
||||
Server = "localhost",
|
||||
ClientId = "XYZ",
|
||||
CleanSession = true
|
||||
Uri = "broker.hivemq.com:8000/mqtt"
|
||||
};
|
||||
|
||||
////var options = new MqttClientOptions
|
||||
////{
|
||||
//// Server = "localhost",
|
||||
//// ClientId = "XYZ",
|
||||
//// CleanSession = true
|
||||
////};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttClient(options);
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
@@ -87,7 +92,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -97,7 +102,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync();
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
@@ -37,15 +37,32 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
|
||||
private async void Connect(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var options = new MqttClientOptions
|
||||
MqttClientOptions options = null;
|
||||
if (UseTcp.IsChecked == true)
|
||||
{
|
||||
Server = Server.Text,
|
||||
UserName = User.Text,
|
||||
Password = Password.Text,
|
||||
ClientId = ClientId.Text,
|
||||
TlsOptions = { UseTls = UseTls.IsChecked == true },
|
||||
ConnectionType = UseTcp.IsChecked == true ? MqttConnectionType.Tcp : MqttConnectionType.Ws
|
||||
};
|
||||
options = new MqttClientTcpOptions
|
||||
{
|
||||
Server = Server.Text
|
||||
};
|
||||
}
|
||||
|
||||
if (UseWs.IsChecked == true)
|
||||
{
|
||||
options = new MqttClientWebSocketOptions
|
||||
{
|
||||
Uri = Server.Text
|
||||
};
|
||||
}
|
||||
|
||||
if (options == null)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
options.UserName = User.Text;
|
||||
options.Password = Password.Text;
|
||||
options.ClientId = ClientId.Text;
|
||||
options.TlsOptions.UseTls = UseTls.IsChecked == true;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -56,7 +73,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
|
||||
var factory = new MqttClientFactory();
|
||||
_mqttClient = factory.CreateMqttClient(options);
|
||||
await _mqttClient.ConnectAsync();
|
||||
await _mqttClient.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user