mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 11:45:43 +08:00
Move events to server level instead of options.
This commit is contained in:
@@ -33,7 +33,7 @@ namespace MQTTnet.AspNetCore.Tests.Mockups
|
||||
}
|
||||
}
|
||||
|
||||
public Task StartAsync(IMqttServerOptions options)
|
||||
public Task StartAsync(MqttServerOptions options)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -11,19 +11,20 @@ namespace MQTTnet.Benchmarks
|
||||
[MemoryDiagnoser]
|
||||
public class MessageProcessingBenchmark
|
||||
{
|
||||
IMqttServer _mqttServer;
|
||||
MqttServer _mqttServer;
|
||||
IMqttClient _mqttClient;
|
||||
MqttApplicationMessage _message;
|
||||
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
var serverOptions = new MqttServerOptionsBuilder().Build();
|
||||
|
||||
var factory = new MqttFactory();
|
||||
_mqttServer = factory.CreateMqttServer();
|
||||
_mqttServer = factory.CreateMqttServer(serverOptions);
|
||||
_mqttClient = factory.CreateMqttClient();
|
||||
|
||||
var serverOptions = new MqttServerOptionsBuilder().Build();
|
||||
_mqttServer.StartAsync(serverOptions).GetAwaiter().GetResult();
|
||||
_mqttServer.StartAsync().GetAwaiter().GetResult();
|
||||
|
||||
var clientOptions = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost").Build();
|
||||
|
||||
@@ -31,10 +31,11 @@ namespace MQTTnet.Benchmarks
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
_mqttServer = factory.CreateMqttServer(new[] { tcpServer }, new MqttNetEventLogger());
|
||||
|
||||
var serverOptions = new MqttServerOptionsBuilder().Build();
|
||||
_mqttServer.StartAsync(serverOptions).GetAwaiter().GetResult();
|
||||
_mqttServer = factory.CreateMqttServer(serverOptions, new[] { tcpServer }, new MqttNetEventLogger());
|
||||
|
||||
|
||||
_mqttServer.StartAsync().GetAwaiter().GetResult();
|
||||
|
||||
var clientOptions = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("localhost").Build();
|
||||
|
||||
@@ -149,8 +149,7 @@ namespace MQTTnet.Tests.Client
|
||||
|
||||
connected = GetConnectedTask(managedClient);
|
||||
|
||||
await testEnvironment.Server.StartAsync(new MqttServerOptionsBuilder()
|
||||
.WithDefaultEndpointPort(testEnvironment.ServerPort).Build());
|
||||
await testEnvironment.Server.StartAsync();
|
||||
|
||||
await connected;
|
||||
|
||||
|
||||
@@ -34,10 +34,11 @@ namespace MQTTnet.Tests.Client
|
||||
var client = await testEnvironment.ConnectLowLevelClient();
|
||||
|
||||
var i = 0;
|
||||
server.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(c =>
|
||||
server.InterceptingClientPublishAsync += c =>
|
||||
{
|
||||
i++;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await client.SendAsync(new MqttConnectPacket
|
||||
{
|
||||
@@ -223,7 +224,7 @@ namespace MQTTnet.Tests.Client
|
||||
await Task.Delay(500);
|
||||
Assert.IsFalse(client.IsConnected);
|
||||
|
||||
await server.StartAsync(new MqttServerOptionsBuilder().WithDefaultEndpointPort(testEnvironment.ServerPort).Build());
|
||||
await server.StartAsync();
|
||||
await Task.Delay(500);
|
||||
|
||||
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
|
||||
@@ -260,7 +261,7 @@ namespace MQTTnet.Tests.Client
|
||||
}
|
||||
}
|
||||
|
||||
await server.StartAsync(new MqttServerOptionsBuilder().WithDefaultEndpointPort(testEnvironment.ServerPort).Build());
|
||||
await server.StartAsync();
|
||||
await Task.Delay(500);
|
||||
|
||||
await client.ConnectAsync(new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", testEnvironment.ServerPort).Build());
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Extensions.ManagedClient;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.Tests.Factory
|
||||
{
|
||||
@@ -66,88 +67,88 @@ namespace MQTTnet.Tests.Factory
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var builder = factory.CreateApplicationMessageBuilder();
|
||||
|
||||
|
||||
Assert.IsNotNull(builder);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_ClientOptionsBuilder()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var builder = factory.CreateClientOptionsBuilder();
|
||||
|
||||
|
||||
Assert.IsNotNull(builder);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_ServerOptionsBuilder()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var builder = factory.CreateServerOptionsBuilder();
|
||||
|
||||
|
||||
Assert.IsNotNull(builder);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_SubscribeOptionsBuilder()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var builder = factory.CreateSubscribeOptionsBuilder();
|
||||
|
||||
|
||||
Assert.IsNotNull(builder);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_UnsubscribeOptionsBuilder()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var builder = factory.CreateUnsubscribeOptionsBuilder();
|
||||
|
||||
|
||||
Assert.IsNotNull(builder);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_TopicFilterBuilder()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var builder = factory.CreateTopicFilterBuilder();
|
||||
|
||||
|
||||
Assert.IsNotNull(builder);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_MqttServer()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var server = factory.CreateMqttServer();
|
||||
|
||||
var server = factory.CreateMqttServer(new MqttServerOptionsBuilder().Build());
|
||||
|
||||
Assert.IsNotNull(server);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_MqttClient()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
|
||||
Assert.IsNotNull(client);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_LowLevelMqttClient()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateLowLevelMqttClient();
|
||||
|
||||
|
||||
Assert.IsNotNull(client);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Create_ManagedMqttClient()
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
var client = factory.CreateManagedMqttClient();
|
||||
|
||||
|
||||
Assert.IsNotNull(client);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace MQTTnet.Tests.Mockups
|
||||
|
||||
public MqttFactory Factory { get; } = new MqttFactory();
|
||||
|
||||
public IMqttServer Server { get; private set; }
|
||||
public MqttServer Server { get; private set; }
|
||||
|
||||
public bool IgnoreClientLogErrors { get; set; }
|
||||
|
||||
@@ -173,46 +173,60 @@ namespace MQTTnet.Tests.Mockups
|
||||
return new MqttRpcClient(await ConnectClient(), options);
|
||||
}
|
||||
|
||||
public IMqttServer CreateServer()
|
||||
public MqttServer CreateServer(MqttServerOptions options)
|
||||
{
|
||||
if (Server != null)
|
||||
{
|
||||
throw new InvalidOperationException("Server already started.");
|
||||
}
|
||||
|
||||
Server = new TestServerWrapper(Factory.CreateMqttServer(ServerLogger), TestContext, this);
|
||||
Server = Factory.CreateMqttServer(options, ServerLogger);
|
||||
|
||||
Server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
if (TestContext != null)
|
||||
{
|
||||
// Null is used when the client id is assigned from the server!
|
||||
if (!string.IsNullOrEmpty(e.ClientId) && !e.ClientId.StartsWith(TestContext.TestName))
|
||||
{
|
||||
TrackException(new InvalidOperationException($"Invalid client ID used ({e.ClientId}). It must start with UnitTest name."));
|
||||
e.ReasonCode = Protocol.MqttConnectReasonCode.ClientIdentifierNotValid;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
return Server;
|
||||
}
|
||||
|
||||
public Task<IMqttServer> StartServer()
|
||||
public Task<MqttServer> StartServer()
|
||||
{
|
||||
return StartServer(Factory.CreateServerOptionsBuilder());
|
||||
}
|
||||
|
||||
public async Task<IMqttServer> StartServer(MqttServerOptionsBuilder options)
|
||||
public async Task<MqttServer> StartServer(MqttServerOptionsBuilder options)
|
||||
{
|
||||
var server = CreateServer();
|
||||
var server = CreateServer(options.Build());
|
||||
|
||||
options.WithDefaultEndpointPort(ServerPort);
|
||||
options.WithMaxPendingMessagesPerClient(int.MaxValue);
|
||||
|
||||
await server.StartAsync(options.Build());
|
||||
await server.StartAsync();
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
public async Task<IMqttServer> StartServer(Action<MqttServerOptionsBuilder> options)
|
||||
public async Task<MqttServer> StartServer(Action<MqttServerOptionsBuilder> options)
|
||||
{
|
||||
var server = CreateServer();
|
||||
|
||||
var optionsBuilder = Factory.CreateServerOptionsBuilder();
|
||||
optionsBuilder.WithDefaultEndpointPort(ServerPort);
|
||||
optionsBuilder.WithMaxPendingMessagesPerClient(int.MaxValue);
|
||||
|
||||
options?.Invoke(optionsBuilder);
|
||||
options.Invoke(optionsBuilder);
|
||||
|
||||
await server.StartAsync(optionsBuilder.Build());
|
||||
|
||||
var server = CreateServer(optionsBuilder.Build());
|
||||
await server.StartAsync();
|
||||
return server;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Client.Publishing;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Server;
|
||||
using MQTTnet.Server.Status;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MQTTnet.Tests.Mockups
|
||||
{
|
||||
public sealed class TestServerWrapper : IMqttServer
|
||||
{
|
||||
public TestServerWrapper(IMqttServer implementation, TestContext testContext, TestEnvironment testEnvironment)
|
||||
{
|
||||
Implementation = implementation;
|
||||
TestContext = testContext;
|
||||
TestEnvironment = testEnvironment;
|
||||
}
|
||||
|
||||
public IMqttServer Implementation { get; }
|
||||
public TestContext TestContext { get; }
|
||||
public TestEnvironment TestEnvironment { get; }
|
||||
|
||||
public bool IsStarted { get; }
|
||||
|
||||
public IMqttServerStartedHandler StartedHandler
|
||||
{
|
||||
get => Implementation.StartedHandler;
|
||||
set => Implementation.StartedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerStoppedHandler StoppedHandler
|
||||
{
|
||||
get => Implementation.StoppedHandler;
|
||||
set => Implementation.StoppedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientConnectedHandler ClientConnectedHandler
|
||||
{
|
||||
get => Implementation.ClientConnectedHandler;
|
||||
set => Implementation.ClientConnectedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientDisconnectedHandler ClientDisconnectedHandler
|
||||
{
|
||||
get => Implementation.ClientDisconnectedHandler;
|
||||
set => Implementation.ClientDisconnectedHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientSubscribedTopicHandler ClientSubscribedTopicHandler
|
||||
{
|
||||
get => Implementation.ClientSubscribedTopicHandler;
|
||||
set => Implementation.ClientSubscribedTopicHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerClientUnsubscribedTopicHandler ClientUnsubscribedTopicHandler
|
||||
{
|
||||
get => Implementation.ClientUnsubscribedTopicHandler;
|
||||
set => Implementation.ClientUnsubscribedTopicHandler = value;
|
||||
}
|
||||
|
||||
public IMqttServerOptions Options => Implementation.Options;
|
||||
|
||||
public IMqttApplicationMessageReceivedHandler ApplicationMessageReceivedHandler
|
||||
{
|
||||
get => Implementation.ApplicationMessageReceivedHandler;
|
||||
set => Implementation.ApplicationMessageReceivedHandler = value;
|
||||
}
|
||||
|
||||
public event Func<MqttApplicationMessageReceivedEventArgs, Task> ApplicationMessageReceivedAsync
|
||||
{
|
||||
add => Implementation.ApplicationMessageReceivedAsync += value;
|
||||
remove => Implementation.ApplicationMessageReceivedAsync -= value;
|
||||
}
|
||||
|
||||
public Task ClearRetainedApplicationMessagesAsync()
|
||||
{
|
||||
return Implementation.ClearRetainedApplicationMessagesAsync();
|
||||
}
|
||||
|
||||
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
|
||||
{
|
||||
return Implementation.GetClientStatusAsync();
|
||||
}
|
||||
|
||||
public Task<IList<MqttApplicationMessage>> GetRetainedApplicationMessagesAsync()
|
||||
{
|
||||
return Implementation.GetRetainedApplicationMessagesAsync();
|
||||
}
|
||||
|
||||
public Task<IList<IMqttSessionStatus>> GetSessionStatusAsync()
|
||||
{
|
||||
return Implementation.GetSessionStatusAsync();
|
||||
}
|
||||
|
||||
public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken)
|
||||
{
|
||||
return Implementation.PublishAsync(applicationMessage, cancellationToken);
|
||||
}
|
||||
|
||||
public Task StartAsync(IMqttServerOptions options)
|
||||
{
|
||||
if (TestContext != null)
|
||||
{
|
||||
var serverOptions = (MqttServerOptions)options;
|
||||
|
||||
if (serverOptions.ConnectionValidator == null)
|
||||
{
|
||||
serverOptions.ConnectionValidator = new MqttServerConnectionValidatorDelegate(ConnectionValidator);
|
||||
}
|
||||
}
|
||||
|
||||
return Implementation.StartAsync(options);
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
return Implementation.StopAsync();
|
||||
}
|
||||
|
||||
public Task SubscribeAsync(string clientId, ICollection<MqttTopicFilter> topicFilters)
|
||||
{
|
||||
return Implementation.SubscribeAsync(clientId, topicFilters);
|
||||
}
|
||||
|
||||
public Task UnsubscribeAsync(string clientId, ICollection<string> topicFilters)
|
||||
{
|
||||
return Implementation.UnsubscribeAsync(clientId, topicFilters);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Implementation.Dispose();
|
||||
}
|
||||
|
||||
void ConnectionValidator(MqttConnectionValidatorContext ctx)
|
||||
{
|
||||
if (!ctx.ClientId.StartsWith(TestContext.TestName))
|
||||
{
|
||||
TestEnvironment.TrackException(new InvalidOperationException($"Invalid client ID used ({ctx.ClientId}). It must start with UnitTest name."));
|
||||
ctx.ReasonCode = Protocol.MqttConnectReasonCode.ClientIdentifierNotValid;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ using MQTTnet.Client;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.Tests.Server
|
||||
{
|
||||
@@ -34,27 +33,31 @@ namespace MQTTnet.Tests.Server
|
||||
|
||||
// Arrange server
|
||||
var disconnectedMre = new ManualResetEventSlim();
|
||||
var serverOptions = new MqttServerOptionsBuilder()
|
||||
.WithConnectionValidator(context =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(context.ClientId))
|
||||
{
|
||||
context.AssignedClientIdentifier = assignedClientId;
|
||||
context.ReasonCode = MqttConnectReasonCode.Success;
|
||||
}
|
||||
});
|
||||
|
||||
await testEnvironment.StartServer(serverOptions);
|
||||
testEnvironment.Server.UseClientConnectedHandler(args =>
|
||||
var server = await testEnvironment.StartServer();
|
||||
server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(e.ClientId))
|
||||
{
|
||||
e.AssignedClientIdentifier = assignedClientId;
|
||||
e.ReasonCode = MqttConnectReasonCode.Success;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
testEnvironment.Server.ClientConnectedAsync += args =>
|
||||
{
|
||||
serverConnectedClientId = args.ClientId;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
testEnvironment.Server.UseClientDisconnectedHandler(args =>
|
||||
testEnvironment.Server.ClientDisconnectedAsync += args =>
|
||||
{
|
||||
serverDisconnectedClientId = args.ClientId;
|
||||
disconnectedMre.Set();
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
// Arrange client
|
||||
var client = testEnvironment.CreateClient();
|
||||
|
||||
@@ -20,10 +20,11 @@ namespace MQTTnet.Tests.Server
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
MqttServerClientConnectedEventArgs eventArgs = null;
|
||||
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e =>
|
||||
server.ClientConnectedAsync += e =>
|
||||
{
|
||||
eventArgs = e;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await testEnvironment.ConnectClient(o => o.WithCredentials("TheUser"));
|
||||
|
||||
@@ -46,10 +47,11 @@ namespace MQTTnet.Tests.Server
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
MqttServerClientDisconnectedEventArgs eventArgs = null;
|
||||
server.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(e =>
|
||||
server.ClientDisconnectedAsync += e =>
|
||||
{
|
||||
eventArgs = e;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var client = await testEnvironment.ConnectClient(o => o.WithCredentials("TheUser"));
|
||||
await client.DisconnectAsync();
|
||||
@@ -72,10 +74,11 @@ namespace MQTTnet.Tests.Server
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
MqttServerClientSubscribedTopicEventArgs eventArgs = null;
|
||||
server.ClientSubscribedTopicHandler = new MqttServerClientSubscribedTopicHandlerDelegate(e =>
|
||||
server.ClientSubscribedTopicAsync += e =>
|
||||
{
|
||||
eventArgs = e;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
await client.SubscribeAsync("The/Topic", MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
@@ -98,10 +101,11 @@ namespace MQTTnet.Tests.Server
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
MqttServerClientUnsubscribedTopicEventArgs eventArgs = null;
|
||||
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e =>
|
||||
server.ClientUnsubscribedTopicAsync += e =>
|
||||
{
|
||||
eventArgs = e;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
await client.UnsubscribeAsync("The/Topic");
|
||||
@@ -122,11 +126,12 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
MqttApplicationMessageReceivedEventArgs eventArgs = null;
|
||||
server.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
|
||||
InterceptingMqttClientPublishEventArgs eventArgs = null;
|
||||
server.InterceptingClientPublishAsync += e =>
|
||||
{
|
||||
eventArgs = e;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
await client.PublishAsync("The_Topic", "The_Payload");
|
||||
@@ -146,15 +151,16 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = testEnvironment.CreateServer();
|
||||
|
||||
var server = testEnvironment.CreateServer(new MqttServerOptions());
|
||||
|
||||
EventArgs eventArgs = null;
|
||||
server.StartedHandler = new MqttServerStartedHandlerDelegate(e =>
|
||||
server.StartedAsync += e =>
|
||||
{
|
||||
eventArgs = e;
|
||||
});
|
||||
|
||||
await server.StartAsync(new MqttServerOptionsBuilder().Build());
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await server.StartAsync();
|
||||
|
||||
await LongTestDelay();
|
||||
|
||||
@@ -170,10 +176,11 @@ namespace MQTTnet.Tests.Server
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
EventArgs eventArgs = null;
|
||||
server.StoppedHandler = new MqttServerStoppedHandlerDelegate(e =>
|
||||
server.StoppedAsync += e =>
|
||||
{
|
||||
eventArgs = e;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await server.StopAsync();
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace MQTTnet.Tests.Server
|
||||
[DataRow(null, null)]
|
||||
public async Task Use_Admissible_Credentials(string username, string password)
|
||||
{
|
||||
using (var testEnvironment = new TestEnvironment())
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer();
|
||||
|
||||
@@ -196,7 +196,7 @@ namespace MQTTnet.Tests.Server
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task Publish_From_Server()
|
||||
{
|
||||
@@ -210,9 +210,9 @@ namespace MQTTnet.Tests.Server
|
||||
client.UseApplicationMessageReceivedHandler(c => Interlocked.Increment(ref receivedMessagesCount));
|
||||
|
||||
var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
|
||||
await client.SubscribeAsync(new MqttTopicFilter { Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
|
||||
await client.SubscribeAsync(new MqttTopicFilter {Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce});
|
||||
|
||||
await server.PublishAsync(message);
|
||||
await server.PublishAsync("server", message);
|
||||
|
||||
await Task.Delay(1000);
|
||||
|
||||
@@ -237,8 +237,8 @@ namespace MQTTnet.Tests.Server
|
||||
|
||||
c3.UseApplicationMessageReceivedHandler(c => { Interlocked.Increment(ref receivedMessagesCount); });
|
||||
|
||||
await c2.SubscribeAsync(new MqttTopicFilter { Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce }).ConfigureAwait(false);
|
||||
await c3.SubscribeAsync(new MqttTopicFilter { Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce }).ConfigureAwait(false);
|
||||
await c2.SubscribeAsync(new MqttTopicFilter {Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce}).ConfigureAwait(false);
|
||||
await c3.SubscribeAsync(new MqttTopicFilter {Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce}).ConfigureAwait(false);
|
||||
|
||||
var message = new MqttApplicationMessageBuilder().WithTopic("a").WithAtLeastOnceQoS().Build();
|
||||
|
||||
@@ -318,11 +318,11 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(async e =>
|
||||
server.ClientConnectedAsync += async e =>
|
||||
{
|
||||
// Every client will automatically subscribe to this topic.
|
||||
await server.SubscribeAsync(e.ClientId, "topic1");
|
||||
});
|
||||
};
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
var receivedMessages = new List<MqttApplicationMessage>();
|
||||
@@ -379,13 +379,22 @@ namespace MQTTnet.Tests.Server
|
||||
var clientConnectedCalled = 0;
|
||||
var clientDisconnectedCalled = 0;
|
||||
|
||||
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(_ => Interlocked.Increment(ref clientConnectedCalled));
|
||||
server.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(_ => Interlocked.Increment(ref clientDisconnectedCalled));
|
||||
server.ClientConnectedAsync += e =>
|
||||
{
|
||||
Interlocked.Increment(ref clientConnectedCalled);
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
server.ClientDisconnectedAsync += e =>
|
||||
{
|
||||
Interlocked.Increment(ref clientDisconnectedCalled);
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var c1 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder());
|
||||
|
||||
await Task.Delay(1000);
|
||||
|
||||
|
||||
Assert.AreEqual(1, clientConnectedCalled);
|
||||
Assert.AreEqual(0, clientDisconnectedCalled);
|
||||
|
||||
@@ -490,9 +499,14 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(
|
||||
new MqttServerOptionsBuilder().WithApplicationMessageInterceptor(
|
||||
c => { c.ApplicationMessage = new MqttApplicationMessage { Topic = "new_topic" }; }));
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.InterceptingClientPublishAsync += e =>
|
||||
{
|
||||
e.ApplicationMessage = new MqttApplicationMessage {Topic = "new_topic"};
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
string receivedTopic = null;
|
||||
var c1 = await testEnvironment.ConnectClient();
|
||||
@@ -531,7 +545,15 @@ namespace MQTTnet.Tests.Server
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer();
|
||||
server.UseClientConnectedHandler(async e => { await server.PublishAsync("/test/1", "true", MqttQualityOfServiceLevel.ExactlyOnce, false); });
|
||||
server.ClientConnectedAsync += async e =>
|
||||
{
|
||||
await server.PublishAsync("server", new MqttApplicationMessage
|
||||
{
|
||||
Topic = "/test/1",
|
||||
Payload = Encoding.UTF8.GetBytes("true"),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
|
||||
});
|
||||
};
|
||||
|
||||
string receivedTopic = null;
|
||||
|
||||
@@ -553,21 +575,24 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Intercept_Message()
|
||||
{
|
||||
void Interceptor(MqttApplicationMessageInterceptorContext context)
|
||||
{
|
||||
context.ApplicationMessage.Payload = Encoding.ASCII.GetBytes("extended");
|
||||
}
|
||||
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithApplicationMessageInterceptor(Interceptor));
|
||||
var server = await testEnvironment.StartServer();
|
||||
server.InterceptingClientPublishAsync += e =>
|
||||
{
|
||||
e.ApplicationMessage.Payload = Encoding.ASCII.GetBytes("extended");
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var c1 = await testEnvironment.ConnectClient();
|
||||
var c2 = await testEnvironment.ConnectClient();
|
||||
await c2.SubscribeAsync(new MqttTopicFilterBuilder().WithTopic("test").Build());
|
||||
|
||||
var isIntercepted = false;
|
||||
c2.UseApplicationMessageReceivedHandler(c => { isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(c.ApplicationMessage.Payload), StringComparison.Ordinal) == 0; });
|
||||
c2.UseApplicationMessageReceivedHandler(c =>
|
||||
{
|
||||
isIntercepted = string.Compare("extended", Encoding.UTF8.GetString(c.ApplicationMessage.Payload), StringComparison.Ordinal) == 0;
|
||||
});
|
||||
|
||||
await c1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic("test").Build());
|
||||
await c1.DisconnectAsync();
|
||||
@@ -622,13 +647,17 @@ namespace MQTTnet.Tests.Server
|
||||
[TestMethod]
|
||||
public async Task Deny_Connection()
|
||||
{
|
||||
var serverOptions = new MqttServerOptionsBuilder().WithConnectionValidator(context => { context.ReasonCode = MqttConnectReasonCode.NotAuthorized; });
|
||||
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
|
||||
await testEnvironment.StartServer(serverOptions);
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
e.ReasonCode = MqttConnectReasonCode.NotAuthorized;
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var connectingFailedException = await Assert.ThrowsExceptionAsync<MqttConnectingFailedException>(() => testEnvironment.ConnectClient());
|
||||
Assert.AreEqual(MqttClientConnectResultCode.NotAuthorized, connectingFailedException.ResultCode);
|
||||
@@ -637,7 +666,7 @@ namespace MQTTnet.Tests.Server
|
||||
|
||||
Dictionary<string, bool> _connected;
|
||||
|
||||
private void ConnectionValidationHandler(MqttConnectionValidatorContext eventArgs)
|
||||
void ConnectionValidationHandler(ValidatingMqttClientConnectionEventArgs eventArgs)
|
||||
{
|
||||
if (_connected.ContainsKey(eventArgs.ClientId))
|
||||
{
|
||||
@@ -647,7 +676,6 @@ namespace MQTTnet.Tests.Server
|
||||
|
||||
_connected[eventArgs.ClientId] = true;
|
||||
eventArgs.ReasonCode = MqttConnectReasonCode.Success;
|
||||
return;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -658,27 +686,36 @@ namespace MQTTnet.Tests.Server
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
|
||||
_connected = new Dictionary<string, bool>();
|
||||
var options = new MqttServerOptionsBuilder();
|
||||
options.WithConnectionValidator(e => ConnectionValidationHandler(e));
|
||||
var server = await testEnvironment.StartServer(options);
|
||||
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
ConnectionValidationHandler(e);
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var events = new List<string>();
|
||||
|
||||
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(_ =>
|
||||
server.ClientConnectedAsync += e =>
|
||||
{
|
||||
lock (events)
|
||||
{
|
||||
events.Add("c");
|
||||
}
|
||||
});
|
||||
|
||||
server.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(_ =>
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
server.ClientDisconnectedAsync += e =>
|
||||
{
|
||||
lock (events)
|
||||
{
|
||||
events.Add("d");
|
||||
}
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var clientOptions = new MqttClientOptionsBuilder()
|
||||
.WithClientId("same_id");
|
||||
@@ -743,25 +780,29 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var server = await testEnvironment.StartServer(new MqttServerOptionsBuilder());
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
var events = new List<string>();
|
||||
|
||||
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(_ =>
|
||||
server.ClientConnectedAsync += e =>
|
||||
{
|
||||
lock (events)
|
||||
{
|
||||
events.Add("c");
|
||||
}
|
||||
});
|
||||
|
||||
server.ClientDisconnectedHandler = new MqttServerClientDisconnectedHandlerDelegate(_ =>
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
server.ClientDisconnectedAsync += e =>
|
||||
{
|
||||
lock (events)
|
||||
{
|
||||
events.Add("d");
|
||||
}
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var clientOptionsBuilder = new MqttClientOptionsBuilder()
|
||||
.WithClientId(Guid.NewGuid().ToString());
|
||||
@@ -863,7 +904,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
}
|
||||
|
||||
await server.StartAsync(new MqttServerOptionsBuilder().WithDefaultEndpointPort(testEnvironment.ServerPort).Build());
|
||||
await server.StartAsync();
|
||||
await testEnvironment.ConnectClient();
|
||||
}
|
||||
}
|
||||
@@ -873,14 +914,18 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithSubscriptionInterceptor(c =>
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.InterceptingClientSubscriptionAsync += e =>
|
||||
{
|
||||
// This should lead to no subscriptions for "n" at all. So also no sending of retained messages.
|
||||
if (c.TopicFilter.Topic == "n")
|
||||
if (e.TopicFilter.Topic == "n")
|
||||
{
|
||||
c.Response.ReasonCode = MqttSubscribeReasonCode.UnspecifiedError;
|
||||
e.Response.ReasonCode = MqttSubscribeReasonCode.UnspecifiedError;
|
||||
}
|
||||
}));
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
// Prepare some retained messages.
|
||||
var client1 = await testEnvironment.ConnectClient();
|
||||
@@ -904,7 +949,7 @@ namespace MQTTnet.Tests.Server
|
||||
}
|
||||
});
|
||||
|
||||
await client2.SubscribeAsync(new MqttTopicFilter { Topic = "y" }, new MqttTopicFilter { Topic = "n" });
|
||||
await client2.SubscribeAsync(new MqttTopicFilter {Topic = "y"}, new MqttTopicFilter {Topic = "n"});
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
@@ -964,7 +1009,7 @@ namespace MQTTnet.Tests.Server
|
||||
var c2 = await testEnvironment.ConnectClient(new MqttClientOptionsBuilder().WithClientId("sender"));
|
||||
await c2.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(new byte[0]).WithQualityOfServiceLevel(qualityOfServiceLevel).Build());
|
||||
await Task.Delay(500);
|
||||
|
||||
|
||||
await c2.DisconnectAsync().ConfigureAwait(false);
|
||||
|
||||
await Task.Delay(500);
|
||||
@@ -982,81 +1027,87 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var undeliverd = string.Empty;
|
||||
|
||||
var options = new MqttServerOptionsBuilder().WithUndeliveredMessageInterceptor(
|
||||
context => { undeliverd = context.ApplicationMessage.Topic; });
|
||||
|
||||
await testEnvironment.StartServer(options);
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
|
||||
await client.SubscribeAsync("b");
|
||||
|
||||
await client.PublishAsync("a", null, MqttQualityOfServiceLevel.ExactlyOnce);
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
Assert.AreEqual("a", undeliverd);
|
||||
throw new NotImplementedException();
|
||||
|
||||
// var options = new MqttServerOptionsBuilder().WithUndeliveredMessageInterceptor(
|
||||
// context => { undeliverd = context.ApplicationMessage.Topic; });
|
||||
//
|
||||
// await testEnvironment.StartServer(options);
|
||||
//
|
||||
// var client = await testEnvironment.ConnectClient();
|
||||
//
|
||||
// await client.SubscribeAsync("b");
|
||||
//
|
||||
// await client.PublishAsync("a", null, MqttQualityOfServiceLevel.ExactlyOnce);
|
||||
//
|
||||
// await Task.Delay(500);
|
||||
//
|
||||
// Assert.AreEqual("a", undeliverd);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Intercept_ClientMessageQueue()
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder()
|
||||
.WithClientMessageQueueInterceptor(c => c.ApplicationMessage.Topic = "a"));
|
||||
|
||||
var topicAReceived = false;
|
||||
var topicBReceived = false;
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
client.UseApplicationMessageReceivedHandler(c =>
|
||||
{
|
||||
if (c.ApplicationMessage.Topic == "a")
|
||||
{
|
||||
topicAReceived = true;
|
||||
}
|
||||
else if (c.ApplicationMessage.Topic == "b")
|
||||
{
|
||||
topicBReceived = true;
|
||||
}
|
||||
});
|
||||
|
||||
await client.SubscribeAsync("a");
|
||||
await client.SubscribeAsync("b");
|
||||
|
||||
await client.PublishAsync("b");
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
Assert.IsTrue(topicAReceived);
|
||||
Assert.IsFalse(topicBReceived);
|
||||
}
|
||||
throw new NotImplementedException();
|
||||
|
||||
// using (var testEnvironment = CreateTestEnvironment())
|
||||
// {
|
||||
// await testEnvironment.StartServer(new MqttServerOptionsBuilder()
|
||||
// .WithClientMessageQueueInterceptor(c => c.ApplicationMessage.Topic = "a"));
|
||||
//
|
||||
// var topicAReceived = false;
|
||||
// var topicBReceived = false;
|
||||
//
|
||||
// var client = await testEnvironment.ConnectClient();
|
||||
// client.UseApplicationMessageReceivedHandler(c =>
|
||||
// {
|
||||
// if (c.ApplicationMessage.Topic == "a")
|
||||
// {
|
||||
// topicAReceived = true;
|
||||
// }
|
||||
// else if (c.ApplicationMessage.Topic == "b")
|
||||
// {
|
||||
// topicBReceived = true;
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// await client.SubscribeAsync("a");
|
||||
// await client.SubscribeAsync("b");
|
||||
//
|
||||
// await client.PublishAsync("b");
|
||||
//
|
||||
// await Task.Delay(500);
|
||||
//
|
||||
// Assert.IsTrue(topicAReceived);
|
||||
// Assert.IsFalse(topicBReceived);
|
||||
// }
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Intercept_ClientMessageQueue_Different_QoS_Of_Subscription_And_Message()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
const string topic = "a";
|
||||
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder()
|
||||
.WithClientMessageQueueInterceptor(c => { })); // Interceptor does nothing but has to be present.
|
||||
|
||||
bool receivedMessage = false;
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
client.UseApplicationMessageReceivedHandler(c => { receivedMessage = true; });
|
||||
|
||||
await client.SubscribeAsync(topic, MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
|
||||
await client.PublishAsync(new MqttApplicationMessage { Topic = topic, QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
Assert.IsTrue(receivedMessage);
|
||||
}
|
||||
// using (var testEnvironment = CreateTestEnvironment())
|
||||
// {
|
||||
// await testEnvironment.StartServer(new MqttServerOptionsBuilder()
|
||||
// .WithClientMessageQueueInterceptor(c => { })); // Interceptor does nothing but has to be present.
|
||||
//
|
||||
// bool receivedMessage = false;
|
||||
// var client = await testEnvironment.ConnectClient();
|
||||
// client.UseApplicationMessageReceivedHandler(c => { receivedMessage = true; });
|
||||
//
|
||||
// await client.SubscribeAsync(topic, MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
//
|
||||
// await client.PublishAsync(new MqttApplicationMessage {Topic = topic, QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce});
|
||||
//
|
||||
// await Task.Delay(500);
|
||||
//
|
||||
// Assert.IsTrue(receivedMessage);
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,14 +11,14 @@ using MQTTnet.Tests.Mockups;
|
||||
namespace MQTTnet.Tests.Server
|
||||
{
|
||||
[TestClass]
|
||||
public class MqttSubscriptionsManager_Tests : BaseTestClass
|
||||
public sealed class MqttSubscriptionsManager_Tests : BaseTestClass
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task MqttSubscriptionsManager_SubscribeSingleSuccess()
|
||||
{
|
||||
var s = CreateSession();
|
||||
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventDispatcher(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventContainer(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new MqttTopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
@@ -35,7 +35,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var s = CreateSession();
|
||||
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventDispatcher(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventContainer(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new MqttTopicFilter { Topic = "A/B/C", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
|
||||
@@ -52,7 +52,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var s = CreateSession();
|
||||
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventDispatcher(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventContainer(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new MqttTopicFilter { Topic = "#", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce });
|
||||
@@ -70,7 +70,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var s = CreateSession();
|
||||
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventDispatcher(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventContainer(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new MqttTopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
@@ -85,7 +85,7 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
var s = CreateSession();
|
||||
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventDispatcher(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
var sm = new MqttClientSubscriptionsManager(s, new MqttServerOptions(), new MqttServerEventContainer(new TestLogger()), new MqttRetainedMessagesManager());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new MqttTopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
@@ -106,15 +106,15 @@ namespace MQTTnet.Tests.Server
|
||||
var logger = new TestLogger();
|
||||
var options = new MqttServerOptions();
|
||||
var retainedMessagesManager = new MqttRetainedMessagesManager();
|
||||
var eventDispatcher = new MqttServerEventDispatcher(logger);
|
||||
|
||||
var eventContainer = new MqttServerEventContainer(logger);
|
||||
|
||||
return new MqttClientSession(
|
||||
"",
|
||||
new ConcurrentDictionary<object, object>(),
|
||||
eventDispatcher,
|
||||
options,
|
||||
eventContainer,
|
||||
retainedMessagesManager,
|
||||
new MqttClientSessionsManager(options, retainedMessagesManager, eventDispatcher, logger));
|
||||
new MqttClientSessionsManager(options, retainedMessagesManager, eventContainer, logger));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,19 +36,19 @@ namespace MQTTnet.Tests.Server
|
||||
Assert.AreEqual("Error while authenticating. If the User Name Flag is set to 0, the Password Flag MUST be set to 0 [MQTT-3.1.2-22].", ex.Message, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public Task Handle_Wrong_UserName()
|
||||
{
|
||||
return TestCredentials("x", "Password1");
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public Task Handle_Wrong_Password()
|
||||
{
|
||||
return TestCredentials("UserName", "x");
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public Task Handle_Wrong_UserName_And_Password()
|
||||
{
|
||||
@@ -61,19 +61,22 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
|
||||
await testEnvironment.StartServer(testEnvironment.Factory.CreateServerOptionsBuilder()
|
||||
.WithConnectionValidator(c =>
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
if (e.Username != "UserName1")
|
||||
{
|
||||
if (c.Username != "UserName1")
|
||||
{
|
||||
c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
||||
}
|
||||
|
||||
if (c.Password != "Password1")
|
||||
{
|
||||
c.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
||||
}
|
||||
}));
|
||||
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
||||
}
|
||||
|
||||
if (e.Password != "Password1")
|
||||
{
|
||||
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var client = testEnvironment.CreateClient();
|
||||
|
||||
|
||||
@@ -19,14 +19,15 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
testEnvironment.IgnoreClientLogErrors = true;
|
||||
|
||||
await testEnvironment.StartServer(o =>
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
o.WithConnectionValidator(v =>
|
||||
{
|
||||
v.ReasonCode = MqttConnectReasonCode.ServerMoved;
|
||||
v.ServerReference = "new_server";
|
||||
});
|
||||
});
|
||||
e.ReasonCode = MqttConnectReasonCode.ServerMoved;
|
||||
e.ServerReference = "new_server";
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -21,31 +21,38 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var serverOptions = new MqttServerOptionsBuilder()
|
||||
.WithConnectionValidator(delegate(MqttConnectionValidatorContext context)
|
||||
{
|
||||
// Don't validate anything. Just set some session items.
|
||||
context.SessionItems["can_subscribe_x"] = true;
|
||||
context.SessionItems["default_payload"] = "Hello World";
|
||||
})
|
||||
.WithSubscriptionInterceptor(delegate(MqttSubscriptionInterceptorContext context)
|
||||
{
|
||||
if (context.TopicFilter.Topic == "x")
|
||||
{
|
||||
if (context.SessionItems["can_subscribe_x"] as bool? == false)
|
||||
{
|
||||
context.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
|
||||
}
|
||||
}
|
||||
})
|
||||
.WithApplicationMessageInterceptor(delegate(MqttApplicationMessageInterceptorContext context)
|
||||
{
|
||||
context.ApplicationMessage.Payload =
|
||||
Encoding.UTF8.GetBytes(
|
||||
context.SessionItems["default_payload"] as string ?? String.Empty);
|
||||
});
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
await testEnvironment.StartServer(serverOptions);
|
||||
server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
// Don't validate anything. Just set some session items.
|
||||
e.SessionItems["can_subscribe_x"] = true;
|
||||
e.SessionItems["default_payload"] = "Hello World";
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
server.InterceptingClientSubscriptionAsync += e =>
|
||||
{
|
||||
if (e.TopicFilter.Topic == "x")
|
||||
{
|
||||
if (e.SessionItems["can_subscribe_x"] as bool? == false)
|
||||
{
|
||||
e.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
server.InterceptingClientPublishAsync += e =>
|
||||
{
|
||||
e.ApplicationMessage.Payload =
|
||||
Encoding.UTF8.GetBytes(
|
||||
e.SessionItems["default_payload"] as string ?? String.Empty);
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
string receivedPayload = null;
|
||||
|
||||
@@ -73,15 +80,16 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
var serverOptions = new MqttServerOptionsBuilder()
|
||||
.WithConnectionValidator(delegate(MqttConnectionValidatorContext context)
|
||||
{
|
||||
// Don't validate anything. Just set some session items.
|
||||
context.SessionItems["can_subscribe_x"] = true;
|
||||
context.SessionItems["default_payload"] = "Hello World";
|
||||
});
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
await testEnvironment.StartServer(serverOptions);
|
||||
server.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
// Don't validate anything. Just set some session items.
|
||||
e.SessionItems["can_subscribe_x"] = true;
|
||||
e.SessionItems["default_payload"] = "Hello World";
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await testEnvironment.ConnectClient();
|
||||
|
||||
|
||||
@@ -19,13 +19,16 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment())
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithSubscriptionInterceptor(
|
||||
c =>
|
||||
{
|
||||
// Set the topic to "a" regards what the client wants to subscribe.
|
||||
c.TopicFilter.Topic = "a";
|
||||
}));
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.InterceptingClientSubscriptionAsync += e =>
|
||||
{
|
||||
// Set the topic to "a" regards what the client wants to subscribe.
|
||||
e.TopicFilter.Topic = "a";
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var topicAReceived = false;
|
||||
var topicBReceived = false;
|
||||
|
||||
@@ -74,10 +77,11 @@ namespace MQTTnet.Tests.Server
|
||||
Assert.AreEqual(0, receivedMessagesCount);
|
||||
|
||||
var subscribeEventCalled = false;
|
||||
server.ClientSubscribedTopicHandler = new MqttServerClientSubscribedTopicHandlerDelegate(e =>
|
||||
server.ClientSubscribedTopicAsync += e =>
|
||||
{
|
||||
subscribeEventCalled = e.TopicFilter.Topic == "a" && e.ClientId == c1.Options.ClientId;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await c1.SubscribeAsync(new MqttTopicFilter { Topic = "a", QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce });
|
||||
await Task.Delay(250);
|
||||
@@ -88,10 +92,11 @@ namespace MQTTnet.Tests.Server
|
||||
Assert.AreEqual(1, receivedMessagesCount);
|
||||
|
||||
var unsubscribeEventCalled = false;
|
||||
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e =>
|
||||
server.ClientUnsubscribedTopicAsync += e =>
|
||||
{
|
||||
unsubscribeEventCalled = e.TopicFilter == "a" && e.ClientId == c1.Options.ClientId;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
await c1.UnsubscribeAsync("a");
|
||||
await Task.Delay(250);
|
||||
@@ -260,14 +265,17 @@ namespace MQTTnet.Tests.Server
|
||||
{
|
||||
using (var testEnvironment = CreateTestEnvironment(MqttProtocolVersion.V500))
|
||||
{
|
||||
await testEnvironment.StartServer(new MqttServerOptionsBuilder().WithSubscriptionInterceptor(
|
||||
c =>
|
||||
var server = await testEnvironment.StartServer();
|
||||
|
||||
server.InterceptingClientSubscriptionAsync += e =>
|
||||
{
|
||||
if (e.TopicFilter.Topic == "not_allowed_topic")
|
||||
{
|
||||
if (c.TopicFilter.Topic == "not_allowed_topic")
|
||||
{
|
||||
c.Response.ReasonCode = MqttSubscribeReasonCode.TopicFilterInvalid;
|
||||
}
|
||||
}));
|
||||
e.Response.ReasonCode = MqttSubscribeReasonCode.TopicFilterInvalid;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
var client = await testEnvironment.ConnectClient();
|
||||
|
||||
|
||||
@@ -7,10 +7,8 @@ using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet.AspNetCore;
|
||||
using MQTTnet.AspNetCore.Extensions;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet.TestApp.AspNetCore2
|
||||
{
|
||||
@@ -44,7 +42,7 @@ namespace MQTTnet.TestApp.AspNetCore2
|
||||
|
||||
app.UseMqttServer(server =>
|
||||
{
|
||||
server.StartedHandler = new MqttServerStartedHandlerDelegate(async args =>
|
||||
server.StartedAsync += async args =>
|
||||
{
|
||||
var frameworkName = GetType().Assembly.GetCustomAttribute<TargetFrameworkAttribute>()?
|
||||
.FrameworkName;
|
||||
@@ -57,7 +55,7 @@ namespace MQTTnet.TestApp.AspNetCore2
|
||||
{
|
||||
try
|
||||
{
|
||||
await server.PublishAsync(msg.Build());
|
||||
await server.PublishAsync("server", msg.Build());
|
||||
msg.WithPayload($"Mqtt hosted on {frameworkName} is still awesome at {DateTime.Now}");
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -69,7 +67,7 @@ namespace MQTTnet.TestApp.AspNetCore2
|
||||
await Task.Delay(TimeSpan.FromSeconds(2));
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
app.Use((context, next) =>
|
||||
|
||||
@@ -58,8 +58,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
try
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
await mqttServer.StartAsync(new MqttServerOptions()).ConfigureAwait(false);
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
|
||||
await mqttServer.StartAsync().ConfigureAwait(false);
|
||||
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
@@ -202,8 +202,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
try
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
await mqttServer.StartAsync(new MqttServerOptions());
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
|
||||
await mqttServer.StartAsync();
|
||||
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
@@ -253,8 +253,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
try
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
await mqttServer.StartAsync(new MqttServerOptions());
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
|
||||
await mqttServer.StartAsync();
|
||||
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
UseTls = true,
|
||||
SslProtocol = SslProtocols.Tls13,
|
||||
// Don't use this in production code. This handler simply allows any invalid certificate to work.
|
||||
CertificateValidationHandler = (w) => true
|
||||
CertificateValidationHandler = w => true
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
UseTls = true,
|
||||
SslProtocol = SslProtocols.Tls12,
|
||||
// Don't use this in production code. This handler simply allows any invalid certificate to work.
|
||||
CertificateValidationHandler = (w) => true
|
||||
CertificateValidationHandler = w => true
|
||||
};
|
||||
|
||||
// mqtt.eclipseprojects.io
|
||||
|
||||
@@ -15,11 +15,10 @@ namespace MQTTnet.TestApp.NetCore
|
||||
MqttNetConsoleLogger.ForwardToConsole(logger);
|
||||
|
||||
var factory = new MqttFactory(logger);
|
||||
var server = factory.CreateMqttServer();
|
||||
var server = factory.CreateMqttServer( new MqttServerOptionsBuilder().Build());
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
var serverOptions = new MqttServerOptionsBuilder().Build();
|
||||
await server.StartAsync(serverOptions);
|
||||
await server.StartAsync();
|
||||
|
||||
var clientOptions = new MqttClientOptionsBuilder().WithTcpServer("localhost").Build();
|
||||
await client.ConnectAsync(clientOptions);
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Implementations;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
using MQTTnet.Server.Internal;
|
||||
@@ -14,8 +13,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static void RunEmptyServer()
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
mqttServer.StartAsync(new MqttServerOptions()).GetAwaiter().GetResult();
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(new MqttServerOptions());
|
||||
mqttServer.StartAsync().GetAwaiter().GetResult();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
@@ -27,8 +26,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
MqttNetConsoleLogger.ForwardToConsole(logger);
|
||||
|
||||
var mqttFactory = new MqttFactory(logger);
|
||||
var mqttServer = mqttFactory.CreateMqttServer();
|
||||
mqttServer.StartAsync(new MqttServerOptions()).GetAwaiter().GetResult();
|
||||
var mqttServer = mqttFactory.CreateMqttServer(new MqttServerOptions());
|
||||
mqttServer.StartAsync().GetAwaiter().GetResult();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
@@ -40,48 +39,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
var options = new MqttServerOptions
|
||||
{
|
||||
ConnectionValidator = new MqttServerConnectionValidatorDelegate(p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
if (p.Username != "USER" || p.Password != "PASS")
|
||||
{
|
||||
p.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
Storage = new RetainedMessageHandler(),
|
||||
|
||||
ApplicationMessageInterceptor = new MqttServerApplicationMessageInterceptorDelegate(context =>
|
||||
{
|
||||
if (MqttTopicFilterComparer.Compare(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#") == MqttTopicFilterCompareResult.IsMatch)
|
||||
{
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
|
||||
}
|
||||
|
||||
if (context.ApplicationMessage.Topic == "not_allowed_topic")
|
||||
{
|
||||
context.ProcessPublish = false;
|
||||
context.CloseConnection = true;
|
||||
}
|
||||
}),
|
||||
|
||||
SubscriptionInterceptor = new MqttServerSubscriptionInterceptorDelegate(context =>
|
||||
{
|
||||
if (context.TopicFilter.Topic.StartsWith("admin/foo/bar") && context.ClientId != "theAdmin")
|
||||
{
|
||||
context.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
|
||||
}
|
||||
|
||||
if (context.TopicFilter.Topic.StartsWith("the/secret/stuff") && context.ClientId != "Imperator")
|
||||
{
|
||||
context.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
|
||||
context.CloseConnection = true;
|
||||
}
|
||||
})
|
||||
Storage = new RetainedMessageHandler()
|
||||
};
|
||||
|
||||
// Extend the timestamp for all messages from clients.
|
||||
@@ -93,14 +51,63 @@ namespace MQTTnet.TestApp.NetCore
|
||||
//options.DefaultEndpointOptions.IsEnabled = true;
|
||||
//options.TlsEndpointOptions.IsEnabled = false;
|
||||
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
var mqttServer = new MqttFactory().CreateMqttServer(options);
|
||||
|
||||
mqttServer.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
|
||||
mqttServer.InterceptingClientPublishAsync += e =>
|
||||
{
|
||||
if (MqttTopicFilterComparer.Compare(e.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#") == MqttTopicFilterCompareResult.IsMatch)
|
||||
{
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
e.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
|
||||
}
|
||||
|
||||
if (e.ApplicationMessage.Topic == "not_allowed_topic")
|
||||
{
|
||||
e.ProcessPublish = false;
|
||||
e.CloseConnection = true;
|
||||
}
|
||||
|
||||
return PlatformAbstractionLayer.CompletedTask;
|
||||
};
|
||||
|
||||
mqttServer.ValidatingClientConnectionAsync += e =>
|
||||
{
|
||||
if (e.ClientId == "SpecialClient")
|
||||
{
|
||||
if (e.Username != "USER" || e.Password != "PASS")
|
||||
{
|
||||
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
|
||||
}
|
||||
}
|
||||
|
||||
return PlatformAbstractionLayer.CompletedTask;
|
||||
};
|
||||
|
||||
mqttServer.InterceptingClientSubscriptionAsync += e =>
|
||||
{
|
||||
if (e.TopicFilter.Topic.StartsWith("admin/foo/bar") && e.ClientId != "theAdmin")
|
||||
{
|
||||
e.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
|
||||
}
|
||||
|
||||
if (e.TopicFilter.Topic.StartsWith("the/secret/stuff") && e.ClientId != "Imperator")
|
||||
{
|
||||
e.Response.ReasonCode = MqttSubscribeReasonCode.ImplementationSpecificError;
|
||||
e.CloseConnection = true;
|
||||
}
|
||||
|
||||
return PlatformAbstractionLayer.CompletedTask;
|
||||
};
|
||||
|
||||
mqttServer.InterceptingClientPublishAsync += e =>
|
||||
{
|
||||
MqttNetConsoleLogger.PrintToConsole(
|
||||
$"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0])}'",
|
||||
ConsoleColor.Magenta);
|
||||
});
|
||||
|
||||
return PlatformAbstractionLayer.CompletedTask;
|
||||
};
|
||||
|
||||
//options.ApplicationMessageInterceptor = c =>
|
||||
//{
|
||||
@@ -124,12 +131,13 @@ namespace MQTTnet.TestApp.NetCore
|
||||
// }
|
||||
//};
|
||||
|
||||
mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e =>
|
||||
mqttServer.ClientConnectedAsync += e =>
|
||||
{
|
||||
Console.Write("Client disconnected event fired.");
|
||||
});
|
||||
return PlatformAbstractionLayer.CompletedTask;
|
||||
};
|
||||
|
||||
await mqttServer.StartAsync(options);
|
||||
await mqttServer.StartAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
|
||||
Reference in New Issue
Block a user