Untitled
unknown
plain_text
9 months ago
1.2 kB
4
Indexable
using Microsoft.AspNetCore.SignalR;
public class TypingHub : Hub
{
private static readonly ConcurrentDictionary<string, DateTime> UserStates = new();
private static readonly TimeSpan TypingTimeout = TimeSpan.FromSeconds(5);
public async Task Typing()
{
string userId = Context.ConnectionId;
UserStates[userId] = DateTime.UtcNow;
await Clients.Others.SendAsync("UserTyping", userId);
}
public async Task StopTyping()
{
string userId = Context.ConnectionId;
UserStates.TryRemove(userId, out _);
await Clients.Others.SendAsync("UserStoppedTyping", userId);
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
UserStates.TryRemove(Context.ConnectionId, out _);
await base.OnDisconnectedAsync(exception);
}
// Фоновая задача для проверки
public static void CheckInactiveUsers()
{
var now = DateTime.UtcNow;
foreach (var kvp in UserStates)
{
if (now - kvp.Value > TypingTimeout)
{
UserStates.TryRemove(kvp.Key, out _);
}
}
}
}
Editor is loading...
Leave a Comment