Skip to content

Commit

Permalink
Add ClientId for application messages published by the server.
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Apr 24, 2019
1 parent 9da3377 commit 6ed3b0c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Source/MQTTnet/Server/IMqttServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace MQTTnet.Server
{
public interface IMqttServerOptions
{
string ClientId { get; set; }

bool EnablePersistentSessions { get; }

int MaxPendingMessagesPerClient { get; }
Expand All @@ -19,6 +21,6 @@ public interface IMqttServerOptions
MqttServerTcpEndpointOptions DefaultEndpointOptions { get; }
MqttServerTlsTcpEndpointOptions TlsEndpointOptions { get; }

IMqttServerStorage Storage { get; }
IMqttServerStorage Storage { get; }
}
}
8 changes: 7 additions & 1 deletion Source/MQTTnet/Server/MqttClientSessionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,13 @@ private async Task<MqttApplicationMessageInterceptorContext> 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;
}
Expand Down
2 changes: 2 additions & 0 deletions Source/MQTTnet/Server/MqttServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions Source/MQTTnet/Server/MqttServerOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ public MqttServerOptionsBuilder WithPersistentSessions()
return this;
}

/// <summary>
/// Gets or sets the client ID which is used when publishing messages from the server directly.
/// </summary>
public MqttServerOptionsBuilder WithClientId(string value)
{
_options.ClientId = value;
return this;
}

public IMqttServerOptions Build()
{
return _options;
Expand Down

0 comments on commit 6ed3b0c

Please sign in to comment.