浏览代码

Fix several bugs

release/3.x.x
Christian Kratky 7 年前
父节点
当前提交
a7d0ad5f43
共有 9 个文件被更改,包括 134 次插入30 次删除
  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 查看文件

@@ -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
- 5
Frameworks/MQTTnet.NetStandard/MqttClientFactory.cs 查看文件

@@ -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
- 1
Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs 查看文件

@@ -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()
{


+ 2
- 5
Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs 查看文件

@@ -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
- 1
MQTTnet.Core/Client/IMqttClientFactory.cs 查看文件

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

+ 1
- 1
Tests/MQTTnet.TestApp.NetCore/Program.cs 查看文件

@@ -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 ###");


+ 1
- 1
Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs 查看文件

@@ -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) =>
{
};


+ 24
- 9
Tests/MQTTnet.TestApp.NetFramework/Program.cs 查看文件

@@ -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;
};
}
}
}

+ 100
- 2
Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs 查看文件

@@ -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];
};

}
}
}

正在加载...
取消
保存