2023-07-14 11:40:57 +05:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.SignalR.Services;
|
|
|
|
|
|
|
|
public class ConnectionManagerService
|
|
|
|
{
|
2024-07-04 11:02:45 +05:00
|
|
|
private readonly ConcurrentDictionary<int, string> connections = new();
|
2023-07-14 11:40:57 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
public void AddOrUpdateConnection(int userId, string connectionId)
|
|
|
|
{
|
|
|
|
connections.AddOrUpdate(userId, connectionId,
|
|
|
|
(key, existingConnectionId) => connectionId);
|
|
|
|
}
|
2023-07-14 11:40:57 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
public void RemoveConnection(int userId)
|
|
|
|
{
|
|
|
|
connections.TryRemove(userId, out _);
|
|
|
|
}
|
2023-07-14 11:40:57 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
public string? GetConnectionIdByUserId(int userId)
|
|
|
|
{
|
|
|
|
connections.TryGetValue(userId, out var connectionId);
|
|
|
|
return connectionId;
|
|
|
|
}
|
2023-07-14 11:40:57 +05:00
|
|
|
}
|