mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 02:13:00 +08:00
Fix several bugs
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Implementations;
|
||||
|
||||
namespace MQTTnet
|
||||
{
|
||||
public class MqttClientFactory : IMqttClientFactory
|
||||
{
|
||||
public IMqttClient CreateMqttClient(MqttClientOptions options)
|
||||
public IMqttClient CreateMqttClient()
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
return new MqttClient(new MqttCommunicationAdapterFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Implementations;
|
||||
|
||||
namespace MQTTnet
|
||||
{
|
||||
public class MqttClientFactory : IMqttClientFactory
|
||||
{
|
||||
public IMqttClient CreateMqttClient(MqttClientOptions options)
|
||||
public IMqttClient CreateMqttClient()
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
return new MqttClient(new MqttCommunicationAdapterFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace MQTTnet.Implementations
|
||||
public Stream ReceiveStream { get; private set; }
|
||||
public Stream RawReceiveStream { get; private set; }
|
||||
|
||||
public Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
|
||||
public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
|
||||
|
||||
public async Task ConnectAsync()
|
||||
{
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
using System;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Implementations;
|
||||
|
||||
namespace MQTTnet
|
||||
{
|
||||
public class MqttClientFactory : IMqttClientFactory
|
||||
{
|
||||
public IMqttClient CreateMqttClient(MqttClientOptions options)
|
||||
public IMqttClient CreateMqttClient()
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
return new MqttClient(new MqttCommunicationAdapterFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
{
|
||||
public interface IMqttClientFactory
|
||||
{
|
||||
IMqttClient CreateMqttClient(MqttClientOptions options);
|
||||
IMqttClient CreateMqttClient();
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttClient(options);
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
DefaultCommunicationTimeout = TimeSpan.FromMinutes(10)
|
||||
};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttClient(options);
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
};
|
||||
|
||||
@@ -9,6 +9,7 @@ using MQTTnet.Core.Diagnostics;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using MQTTnet.Implementations;
|
||||
|
||||
namespace MQTTnet.TestApp.NetFramework
|
||||
{
|
||||
@@ -54,7 +55,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
{
|
||||
var options = new MqttClientWebSocketOptions
|
||||
{
|
||||
Uri = "broker.hivemq.com:8000/mqtt"
|
||||
Uri = "broker.hivemq.com:8000/mqtt"
|
||||
};
|
||||
|
||||
////var options = new MqttClientOptions
|
||||
@@ -64,8 +65,8 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
//// CleanSession = true
|
||||
////};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttClient(options);
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
var mqttClient = new MqttClientFactory().CreateMqttClient();
|
||||
mqttClient.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
||||
@@ -75,24 +76,24 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
client.Connected += async (s, e) =>
|
||||
mqttClient.Connected += async (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### CONNECTED WITH SERVER ###");
|
||||
|
||||
await client.SubscribeAsync(new List<TopicFilter>
|
||||
await mqttClient.SubscribeAsync(new List<TopicFilter>
|
||||
{
|
||||
new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
|
||||
});
|
||||
};
|
||||
|
||||
client.Disconnected += async (s, e) =>
|
||||
mqttClient.Disconnected += async (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### DISCONNECTED FROM SERVER ###");
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync(options);
|
||||
await mqttClient.ConnectAsync(options);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -102,7 +103,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync(options);
|
||||
await mqttClient.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -117,7 +118,7 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
Console.ReadLine();
|
||||
|
||||
var applicationMessage = messageFactory.CreateApplicationMessage("myTopic", "Hello World", MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
await client.PublishAsync(applicationMessage);
|
||||
await mqttClient.PublishAsync(applicationMessage);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
@@ -170,5 +171,19 @@ namespace MQTTnet.TestApp.NetFramework
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
private static async Task WikiCode()
|
||||
{
|
||||
// For .NET Framwork & netstandard apps:
|
||||
MqttTcpChannel.CustomCertificateValidationCallback = (x509Certificate, x509Chain, sslPolicyErrors, mqttClientTcpOptions) =>
|
||||
{
|
||||
if (mqttClientTcpOptions.Server == "server_with_revoked_cert")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Security.Cryptography.Certificates;
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml;
|
||||
using MQTTnet.Core;
|
||||
@@ -7,6 +9,8 @@ using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using MQTTnet.Implementations;
|
||||
|
||||
namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
@@ -66,7 +70,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
options.TlsOptions.IgnoreCertificateChainErrors = true;
|
||||
options.TlsOptions.IgnoreCertificateRevocationErrors = true;
|
||||
options.TlsOptions.AllowUntrustedCertificates = true;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
if (_mqttClient != null)
|
||||
@@ -75,7 +79,7 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
|
||||
var factory = new MqttClientFactory();
|
||||
_mqttClient = factory.CreateMqttClient(options);
|
||||
_mqttClient = factory.CreateMqttClient();
|
||||
await _mqttClient.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
@@ -190,5 +194,99 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
Trace.Text += exception + Environment.NewLine;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task WikiCode()
|
||||
{
|
||||
var mqttClient = new MqttClientFactory().CreateMqttClient();
|
||||
|
||||
// ----------------------------------
|
||||
|
||||
var tcpOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "broker.hivemq.org",
|
||||
ClientId = "TestClient"
|
||||
};
|
||||
|
||||
await mqttClient.ConnectAsync(tcpOptions);
|
||||
|
||||
// ----------------------------------
|
||||
|
||||
var secureTcpOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "broker.hivemq.org",
|
||||
ClientId = "TestClient",
|
||||
TlsOptions = new MqttClientTlsOptions
|
||||
{
|
||||
UseTls = true,
|
||||
IgnoreCertificateChainErrors = true,
|
||||
IgnoreCertificateRevocationErrors = true,
|
||||
AllowUntrustedCertificates = true
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------
|
||||
|
||||
var wsOptions = new MqttClientWebSocketOptions
|
||||
{
|
||||
Uri = "broker.hivemq.com:8000/mqtt",
|
||||
ClientId = "TestClient"
|
||||
};
|
||||
|
||||
await mqttClient.ConnectAsync(wsOptions);
|
||||
|
||||
// ----------------------------------
|
||||
{
|
||||
var options = new MqttServerOptions();
|
||||
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
||||
await mqttServer.StartAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
|
||||
await mqttServer.StopAsync();
|
||||
}
|
||||
|
||||
// ----------------------------------
|
||||
|
||||
{
|
||||
var options = new MqttServerOptions
|
||||
{
|
||||
ConnectionValidator = c =>
|
||||
{
|
||||
if (c.ClientId.Length < 10)
|
||||
{
|
||||
return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected;
|
||||
}
|
||||
|
||||
if (c.Username != "mySecretUser")
|
||||
{
|
||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||
}
|
||||
|
||||
if (c.Password != "mySecretPassword")
|
||||
{
|
||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||
}
|
||||
|
||||
return MqttConnectReturnCode.ConnectionAccepted;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// ----------------------------------
|
||||
|
||||
// For UWP apps:
|
||||
MqttTcpChannel.CustomIgnorableServerCertificateErrorsResolver = o =>
|
||||
{
|
||||
if (o.Server == "server_with_revoked_cert")
|
||||
{
|
||||
return new[] { ChainValidationResult.Revoked };
|
||||
}
|
||||
|
||||
return new ChainValidationResult[0];
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user