Browse Source

Fix several bugs

release/3.x.x
Christian Kratky 7 years ago
parent
commit
a7d0ad5f43
9 changed files with 134 additions and 30 deletions
  1. +2
    -5
      Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs
  2. +2
    -5
      Frameworks/MQTTnet.NetStandard/MqttClientFactory.cs
  3. +1
    -1
      Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs
  4. +2
    -5
      Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs
  5. +1
    -1
      MQTTnet.Core/Client/IMqttClientFactory.cs
  6. +1
    -1
      Tests/MQTTnet.TestApp.NetCore/Program.cs
  7. +1
    -1
      Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs
  8. +24
    -9
      Tests/MQTTnet.TestApp.NetFramework/Program.cs
  9. +100
    -2
      Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs

+ 2
- 5
Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs View File

@@ -1,15 +1,12 @@
using System;
using MQTTnet.Core.Client;
using MQTTnet.Core.Client;
using MQTTnet.Implementations; using MQTTnet.Implementations;


namespace MQTTnet namespace MQTTnet
{ {
public class MqttClientFactory : IMqttClientFactory 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()); return new MqttClient(new MqttCommunicationAdapterFactory());
} }
} }

+ 2
- 5
Frameworks/MQTTnet.NetStandard/MqttClientFactory.cs View File

@@ -1,15 +1,12 @@
using System;
using MQTTnet.Core.Client;
using MQTTnet.Core.Client;
using MQTTnet.Implementations; using MQTTnet.Implementations;


namespace MQTTnet namespace MQTTnet
{ {
public class MqttClientFactory : IMqttClientFactory 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()); return new MqttClient(new MqttCommunicationAdapterFactory());
} }
} }

+ 1
- 1
Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs View File

@@ -33,7 +33,7 @@ namespace MQTTnet.Implementations
public Stream ReceiveStream { get; private set; } public Stream ReceiveStream { get; private set; }
public Stream RawReceiveStream { 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() public async Task ConnectAsync()
{ {


+ 2
- 5
Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs View File

@@ -1,15 +1,12 @@
using System;
using MQTTnet.Core.Client;
using MQTTnet.Core.Client;
using MQTTnet.Implementations; using MQTTnet.Implementations;


namespace MQTTnet namespace MQTTnet
{ {
public class MqttClientFactory : IMqttClientFactory 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()); return new MqttClient(new MqttCommunicationAdapterFactory());
} }
} }

+ 1
- 1
MQTTnet.Core/Client/IMqttClientFactory.cs View File

@@ -2,6 +2,6 @@
{ {
public interface IMqttClientFactory public interface IMqttClientFactory
{ {
IMqttClient CreateMqttClient(MqttClientOptions options);
IMqttClient CreateMqttClient();
} }
} }

+ 1
- 1
Tests/MQTTnet.TestApp.NetCore/Program.cs View File

@@ -53,7 +53,7 @@ namespace MQTTnet.TestApp.NetCore
CleanSession = true CleanSession = true
}; };


var client = new MqttClientFactory().CreateMqttClient(options);
var client = new MqttClientFactory().CreateMqttClient();
client.ApplicationMessageReceived += (s, e) => client.ApplicationMessageReceived += (s, e) =>
{ {
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###"); Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");


+ 1
- 1
Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs View File

@@ -40,7 +40,7 @@ namespace MQTTnet.TestApp.NetFramework
DefaultCommunicationTimeout = TimeSpan.FromMinutes(10) DefaultCommunicationTimeout = TimeSpan.FromMinutes(10)
}; };


var client = new MqttClientFactory().CreateMqttClient(options);
var client = new MqttClientFactory().CreateMqttClient();
client.ApplicationMessageReceived += (s, e) => client.ApplicationMessageReceived += (s, e) =>
{ {
}; };


+ 24
- 9
Tests/MQTTnet.TestApp.NetFramework/Program.cs View File

@@ -9,6 +9,7 @@ using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.Packets; using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol; using MQTTnet.Core.Protocol;
using MQTTnet.Core.Server; using MQTTnet.Core.Server;
using MQTTnet.Implementations;


namespace MQTTnet.TestApp.NetFramework namespace MQTTnet.TestApp.NetFramework
{ {
@@ -54,7 +55,7 @@ namespace MQTTnet.TestApp.NetFramework
{ {
var options = new MqttClientWebSocketOptions var options = new MqttClientWebSocketOptions
{ {
Uri = "broker.hivemq.com:8000/mqtt"
Uri = "broker.hivemq.com:8000/mqtt"
}; };


////var options = new MqttClientOptions ////var options = new MqttClientOptions
@@ -64,8 +65,8 @@ namespace MQTTnet.TestApp.NetFramework
//// CleanSession = true //// 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("### RECEIVED APPLICATION MESSAGE ###");
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}"); Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
@@ -75,24 +76,24 @@ namespace MQTTnet.TestApp.NetFramework
Console.WriteLine(); Console.WriteLine();
}; };


client.Connected += async (s, e) =>
mqttClient.Connected += async (s, e) =>
{ {
Console.WriteLine("### CONNECTED WITH SERVER ###"); Console.WriteLine("### CONNECTED WITH SERVER ###");


await client.SubscribeAsync(new List<TopicFilter>
await mqttClient.SubscribeAsync(new List<TopicFilter>
{ {
new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce) new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
}); });
}; };


client.Disconnected += async (s, e) =>
mqttClient.Disconnected += async (s, e) =>
{ {
Console.WriteLine("### DISCONNECTED FROM SERVER ###"); Console.WriteLine("### DISCONNECTED FROM SERVER ###");
await Task.Delay(TimeSpan.FromSeconds(5)); await Task.Delay(TimeSpan.FromSeconds(5));


try try
{ {
await client.ConnectAsync(options);
await mqttClient.ConnectAsync(options);
} }
catch catch
{ {
@@ -102,7 +103,7 @@ namespace MQTTnet.TestApp.NetFramework


try try
{ {
await client.ConnectAsync(options);
await mqttClient.ConnectAsync(options);
} }
catch (Exception exception) catch (Exception exception)
{ {
@@ -117,7 +118,7 @@ namespace MQTTnet.TestApp.NetFramework
Console.ReadLine(); Console.ReadLine();


var applicationMessage = messageFactory.CreateApplicationMessage("myTopic", "Hello World", MqttQualityOfServiceLevel.AtLeastOnce); var applicationMessage = messageFactory.CreateApplicationMessage("myTopic", "Hello World", MqttQualityOfServiceLevel.AtLeastOnce);
await client.PublishAsync(applicationMessage);
await mqttClient.PublishAsync(applicationMessage);
} }
} }
catch (Exception exception) catch (Exception exception)
@@ -170,5 +171,19 @@ namespace MQTTnet.TestApp.NetFramework


Console.ReadLine(); 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;
};
}
} }
} }

+ 100
- 2
Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs View File

@@ -1,5 +1,7 @@
using System; using System;
using System.Text; using System.Text;
using System.Threading.Tasks;
using Windows.Security.Cryptography.Certificates;
using Windows.UI.Core; using Windows.UI.Core;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using MQTTnet.Core; using MQTTnet.Core;
@@ -7,6 +9,8 @@ using MQTTnet.Core.Client;
using MQTTnet.Core.Diagnostics; using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.Packets; using MQTTnet.Core.Packets;
using MQTTnet.Core.Protocol; using MQTTnet.Core.Protocol;
using MQTTnet.Core.Server;
using MQTTnet.Implementations;


namespace MQTTnet.TestApp.UniversalWindows namespace MQTTnet.TestApp.UniversalWindows
{ {
@@ -66,7 +70,7 @@ namespace MQTTnet.TestApp.UniversalWindows
options.TlsOptions.IgnoreCertificateChainErrors = true; options.TlsOptions.IgnoreCertificateChainErrors = true;
options.TlsOptions.IgnoreCertificateRevocationErrors = true; options.TlsOptions.IgnoreCertificateRevocationErrors = true;
options.TlsOptions.AllowUntrustedCertificates = true; options.TlsOptions.AllowUntrustedCertificates = true;
try try
{ {
if (_mqttClient != null) if (_mqttClient != null)
@@ -75,7 +79,7 @@ namespace MQTTnet.TestApp.UniversalWindows
} }


var factory = new MqttClientFactory(); var factory = new MqttClientFactory();
_mqttClient = factory.CreateMqttClient(options);
_mqttClient = factory.CreateMqttClient();
await _mqttClient.ConnectAsync(options); await _mqttClient.ConnectAsync(options);
} }
catch (Exception exception) catch (Exception exception)
@@ -190,5 +194,99 @@ namespace MQTTnet.TestApp.UniversalWindows
Trace.Text += exception + Environment.NewLine; 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];
};

}
} }
} }

Loading…
Cancel
Save