40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace ChatSample
|
|
{
|
|
public class InMemoryUserTracker<THub> : IUserTracker<THub>
|
|
{
|
|
private readonly ConcurrentDictionary<HubConnectionContext, UserDetails> _usersOnline
|
|
= new ConcurrentDictionary<HubConnectionContext, UserDetails>();
|
|
|
|
public event Action<UserDetails[]> UsersJoined;
|
|
public event Action<UserDetails[]> UsersLeft;
|
|
|
|
public Task<IEnumerable<UserDetails>> UsersOnline()
|
|
=> Task.FromResult(_usersOnline.Values.AsEnumerable());
|
|
|
|
public Task AddUser(HubConnectionContext connection, UserDetails userDetails)
|
|
{
|
|
_usersOnline.TryAdd(connection, userDetails);
|
|
UsersJoined(new[] { userDetails });
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task RemoveUser(HubConnectionContext connection)
|
|
{
|
|
if (_usersOnline.TryRemove(connection, out var userDetails))
|
|
{
|
|
UsersLeft(new[] { userDetails });
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
}
|