Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get session from server by client id #2131

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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