mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
test to make server
This commit is contained in:
@@ -5,25 +5,68 @@ namespace Govor.Application.Services.UserOnlineStatus;
|
||||
|
||||
public class OnlineUserStore : IOnlineUserStore
|
||||
{
|
||||
private readonly ConcurrentDictionary<Guid, DateTime> _onlineUsers = new();
|
||||
private readonly ConcurrentDictionary<Guid, HashSet<string>> _userConnections = new();
|
||||
|
||||
public void SetOnlineUser(Guid userId)
|
||||
public bool AddConnection(Guid userId, string connectionId)
|
||||
{
|
||||
_onlineUsers[userId] = DateTime.UtcNow;
|
||||
bool isFirstConnection = false;
|
||||
|
||||
_userConnections.AddOrUpdate(userId,
|
||||
_ => {
|
||||
isFirstConnection = true;
|
||||
return new HashSet<string> { connectionId };
|
||||
},
|
||||
(_, connections) => {
|
||||
lock (connections)
|
||||
{
|
||||
if (connections.Count == 0) isFirstConnection = true;
|
||||
connections.Add(connectionId);
|
||||
}
|
||||
return connections;
|
||||
});
|
||||
|
||||
return isFirstConnection;
|
||||
}
|
||||
|
||||
public void SetOfflineUser(Guid userId)
|
||||
public bool RemoveConnection(Guid userId, string connectionId)
|
||||
{
|
||||
_onlineUsers.TryRemove(userId, out _);
|
||||
bool isLastConnection = false;
|
||||
|
||||
if (_userConnections.TryGetValue(userId, out var connections))
|
||||
{
|
||||
lock (connections)
|
||||
{
|
||||
connections.Remove(connectionId);
|
||||
if (connections.Count == 0)
|
||||
{
|
||||
isLastConnection = true;
|
||||
_userConnections.TryRemove(userId, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return isLastConnection;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetConnections(Guid userId)
|
||||
{
|
||||
if (_userConnections.TryGetValue(userId, out var connections))
|
||||
{
|
||||
lock (connections)
|
||||
{
|
||||
return connections.ToList();
|
||||
}
|
||||
}
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
public bool IsOnline(Guid userId)
|
||||
{
|
||||
return _onlineUsers.ContainsKey(userId);
|
||||
return _userConnections.TryGetValue(userId, out var connections) && connections.Count > 0;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<Guid> GetAllOnlineUsers()
|
||||
{
|
||||
return _onlineUsers.Keys.ToList();
|
||||
return _userConnections.Keys.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user