From 6ed3b0ceddcf0b25ca92c5a13d4809a5d0bc57c7 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 24 Apr 2019 21:37:29 +0200 Subject: [PATCH] Add ClientId for application messages published by the server. --- Source/MQTTnet/Server/IMqttServerOptions.cs | 4 +++- Source/MQTTnet/Server/MqttClientSessionsManager.cs | 8 +++++++- Source/MQTTnet/Server/MqttServerOptions.cs | 2 ++ Source/MQTTnet/Server/MqttServerOptionsBuilder.cs | 9 +++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Source/MQTTnet/Server/IMqttServerOptions.cs b/Source/MQTTnet/Server/IMqttServerOptions.cs index d840c8f..3a24289 100644 --- a/Source/MQTTnet/Server/IMqttServerOptions.cs +++ b/Source/MQTTnet/Server/IMqttServerOptions.cs @@ -4,6 +4,8 @@ namespace MQTTnet.Server { public interface IMqttServerOptions { + string ClientId { get; set; } + bool EnablePersistentSessions { get; } int MaxPendingMessagesPerClient { get; } @@ -19,6 +21,6 @@ namespace MQTTnet.Server MqttServerTcpEndpointOptions DefaultEndpointOptions { get; } MqttServerTlsTcpEndpointOptions TlsEndpointOptions { get; } - IMqttServerStorage Storage { get; } + IMqttServerStorage Storage { get; } } } \ No newline at end of file diff --git a/Source/MQTTnet/Server/MqttClientSessionsManager.cs b/Source/MQTTnet/Server/MqttClientSessionsManager.cs index b237470..fdc62b0 100644 --- a/Source/MQTTnet/Server/MqttClientSessionsManager.cs +++ b/Source/MQTTnet/Server/MqttClientSessionsManager.cs @@ -364,7 +364,13 @@ namespace MQTTnet.Server return null; } - var interceptorContext = new MqttApplicationMessageInterceptorContext(sender?.ClientId, applicationMessage); + var senderClientId = sender?.ClientId; + if (sender == null) + { + senderClientId = _options.ClientId; + } + + var interceptorContext = new MqttApplicationMessageInterceptorContext(senderClientId, applicationMessage); await interceptor.InterceptApplicationMessagePublishAsync(interceptorContext).ConfigureAwait(false); return interceptorContext; } diff --git a/Source/MQTTnet/Server/MqttServerOptions.cs b/Source/MQTTnet/Server/MqttServerOptions.cs index f893dac..7147ef8 100644 --- a/Source/MQTTnet/Server/MqttServerOptions.cs +++ b/Source/MQTTnet/Server/MqttServerOptions.cs @@ -8,6 +8,8 @@ namespace MQTTnet.Server public MqttServerTlsTcpEndpointOptions TlsEndpointOptions { get; } = new MqttServerTlsTcpEndpointOptions(); + public string ClientId { get; set; } + public bool EnablePersistentSessions { get; set; } public int MaxPendingMessagesPerClient { get; set; } = 250; diff --git a/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs b/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs index 451940a..357cb82 100644 --- a/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs +++ b/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs @@ -147,6 +147,15 @@ namespace MQTTnet.Server return this; } + /// + /// Gets or sets the client ID which is used when publishing messages from the server directly. + /// + public MqttServerOptionsBuilder WithClientId(string value) + { + _options.ClientId = value; + return this; + } + public IMqttServerOptions Build() { return _options;