Рабочий чат

This commit is contained in:
Халимов Рустам
2026-04-01 23:18:55 +03:00
parent 249c344df8
commit 4ae7dd60ce
42 changed files with 1016 additions and 184 deletions

View File

@@ -225,18 +225,31 @@ public sealed class ChatHub : Hub
[HubMethodName("friend_request")]
public async Task FriendRequest(FriendSignalRequest request)
{
if (request == null || string.IsNullOrEmpty(request.FriendId))
{
_logger.LogWarning("FriendRequest called with null request or empty FriendId");
return;
}
_logger.LogInformation("Signaling friend_request_received to {FriendId} from {UserId}", request.FriendId, _userContext.UserId);
await SendToUserAsync(request.FriendId, "friend_request_received", new { userId = _userContext.UserId });
}
[HubMethodName("friend_accepted")]
public async Task FriendAccepted(FriendSignalRequest request)
{
if (request == null || string.IsNullOrEmpty(request.FriendId)) return;
_logger.LogInformation("Signaling friend_request_accepted to {FriendId} from {UserId}", request.FriendId, _userContext.UserId);
await SendToUserAsync(request.FriendId, "friend_request_accepted", new { userId = _userContext.UserId });
}
[HubMethodName("friend_removed")]
public async Task FriendRemoved(FriendSignalRequest request)
{
if (request == null || string.IsNullOrEmpty(request.FriendId)) return;
_logger.LogInformation("Signaling friend_removed_notify to {FriendId} from {UserId}", request.FriendId, _userContext.UserId);
await SendToUserAsync(request.FriendId, "friend_removed_notify", new { userId = _userContext.UserId });
}
@@ -246,15 +259,26 @@ public sealed class ChatHub : Hub
private async Task SendToUserAsync(string targetUserId, string method, object payload)
{
if (string.IsNullOrEmpty(targetUserId))
{
_logger.LogWarning("SendToUserAsync called with null or empty targetUserId");
return;
}
if (_userConnections.TryGetValue(targetUserId, out var connectionIds))
{
string[] ids;
lock (connectionIds) { ids = connectionIds.ToArray(); }
_logger.LogDebug("Sending {Method} to user {TargetUserId} ({ConnectionCount} connections)", method, targetUserId, ids.Length);
foreach (var connId in ids)
{
await Clients.Client(connId).SendAsync(method, payload);
}
}
else
{
_logger.LogDebug("User {TargetUserId} not online, skipping {Method} signal", targetUserId, method);
}
}
[HubMethodName("call_offer")]