diff --git a/Source/MQTTnet/Server/IMqttServerOptions.cs b/Source/MQTTnet/Server/IMqttServerOptions.cs index d840c8f12..3a2428952 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 @@ public interface IMqttServerOptions 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 b23747047..fdc62b011 100644 --- a/Source/MQTTnet/Server/MqttClientSessionsManager.cs +++ b/Source/MQTTnet/Server/MqttClientSessionsManager.cs @@ -364,7 +364,13 @@ private async Task InterceptApplicatio 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 f893dac67..7147ef841 100644 --- a/Source/MQTTnet/Server/MqttServerOptions.cs +++ b/Source/MQTTnet/Server/MqttServerOptions.cs @@ -8,6 +8,8 @@ public class MqttServerOptions : IMqttServerOptions 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 451940aa1..357cb82d9 100644 --- a/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs +++ b/Source/MQTTnet/Server/MqttServerOptionsBuilder.cs @@ -147,6 +147,15 @@ public MqttServerOptionsBuilder WithPersistentSessions() 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;