Skip to content

Commit

Permalink
Add ability to get session from server by client id (#2131)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Smolkov <[email protected]>
  • Loading branch information
AntonSmolkov and Anton Smolkov authored Dec 23, 2024
1 parent 9f81a2b commit 8eb884f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Source/MQTTnet.Server/Internal/MqttClientSessionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,25 @@ public Task<IList<MqttClientStatus>> GetClientsStatus()
return Task.FromResult((IList<MqttClientStatus>)result);
}

public Task<MqttSessionStatus> GetSessionStatus(string id)
{
_sessionsManagementLock.EnterReadLock();
try
{
if (!_sessionsStorage.TryGetSession(id, out var session))
{
throw new InvalidOperationException($"Session with ID '{id}' not found.");
}

var sessionStatus = new MqttSessionStatus(session);
return Task.FromResult(sessionStatus);
}
finally
{
_sessionsManagementLock.ExitReadLock();
}
}

public Task<IList<MqttSessionStatus>> GetSessionsStatus()
{
var result = new List<MqttSessionStatus>();
Expand Down
7 changes: 7 additions & 0 deletions Source/MQTTnet.Server/MqttServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ public Task<IList<MqttSessionStatus>> GetSessionsAsync()
return _clientSessionsManager.GetSessionsStatus();
}

public Task<MqttSessionStatus> GetSessionAsync(string id)
{
ThrowIfNotStarted();

return _clientSessionsManager.GetSessionStatus(id);
}

public Task InjectApplicationMessage(InjectedMqttApplicationMessage injectedApplicationMessage, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(injectedApplicationMessage);
Expand Down

0 comments on commit 8eb884f

Please sign in to comment.