public override async Task PublishAsync(NotificationInfo notification, IEnumerable <UserIdentifier> identifiers) { // 返回标准数据给前端 notification.Data = NotificationData.ToStandardData(notification.Data); foreach (var identifier in identifiers) { Logger.LogDebug($"Find online client with user {identifier.UserId} - {identifier.UserName}"); var onlineClientContext = new OnlineClientContext(notification.TenantId, identifier.UserId); var onlineClients = _onlineClientManager.GetAllByContext(onlineClientContext); foreach (var onlineClient in onlineClients) { try { Logger.LogDebug($"Find online client {onlineClient.UserId} - {onlineClient.ConnectionId}"); var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId); if (signalRClient == null) { Logger.LogDebug("Can not get user " + onlineClientContext.UserId + " with connectionId " + onlineClient.ConnectionId + " from SignalR hub!"); continue; } Logger.LogDebug($"Found a singalr client, begin senging notifications"); await signalRClient.SendAsync("getNotification", notification); } catch (Exception ex) { Logger.LogWarning("Could not send notifications to user: {0}", identifier.UserId); Logger.LogWarning("Send to user notifications error: {0}", ex.Message); } } } }
public async Task PublishAsync(NotificationInfo notification, IEnumerable <Guid> userIds) { foreach (var userId in userIds) { var onlineClientContext = new OnlineClientContext(notification.TenantId, userId); var onlineClients = _onlineClientManager.GetAllByContext(onlineClientContext); foreach (var onlineClient in onlineClients) { try { var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId); if (signalRClient == null) { Logger.LogDebug("Can not get user " + onlineClientContext.UserId + " with connectionId " + onlineClient.ConnectionId + " from SignalR hub!"); continue; } await signalRClient.SendAsync("getNotification", notification); } catch (Exception ex) { Logger.LogWarning("Could not send notifications to user: {0}", userId); Logger.LogWarning("Send to user notifications error: {0}", ex.Message); } } } }
protected virtual async Task SendMessageToUserAsync(ChatMessage chatMessage) { var onlineClientContext = new OnlineClientContext(chatMessage.TenantId, chatMessage.ToUserId.Value); var onlineClients = _onlineClientManager.GetAllByContext(onlineClientContext); foreach (var onlineClient in onlineClients) { try { var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId); if (signalRClient == null) { Logger.LogDebug("Can not get user " + onlineClientContext.UserId + " with connectionId " + onlineClient.ConnectionId + " from SignalR hub!"); continue; } await signalRClient.SendAsync("getChatMessage", chatMessage); } catch (Exception ex) { Logger.LogWarning("Could not send message to user: {0}", chatMessage.ToUserId); Logger.LogWarning("Send to user message error: {0}", ex.Message); } } }
protected override async Task SendMessageToUserAsync(ChatMessage chatMessage) { try { var onlineClientContext = new OnlineClientContext(chatMessage.TenantId, chatMessage.ToUserId.Value); var onlineClients = _onlineClientManager.GetAllByContext(onlineClientContext); var onlineClientConnectionIds = onlineClients.Select(client => client.ConnectionId).ToImmutableArray(); var signalRClients = _hubContext.Clients.Clients(onlineClientConnectionIds); if (signalRClients == null) { Logger.LogDebug("Can not get user " + onlineClientContext.UserId + " connection from SignalR hub!"); return; } await signalRClients.SendAsync(_options.GetChatMessageMethod, chatMessage); } catch (Exception ex) { Logger.LogWarning("Could not send message to user: {0}", chatMessage.ToUserId); Logger.LogWarning("Send to user message error: {0}", ex.Message); } }
public static bool IsOnline( [NotNull] this IOnlineClientManager onlineClientManager, [NotNull] OnlineClientContext context) { return(onlineClientManager.GetAllByContext(context).Any()); }