コード例 #1
0
ファイル: NotificationHub.cs プロジェクト: JacDev/MyFaceApi
 public async Task SendMessageToUser(string toWhoId, string message, DateTime when)
 {
     try
     {
         var fromWhoUser = GetLoggedUser().Id;
         if (!string.IsNullOrWhiteSpace(toWhoId) && Guid.TryParse(toWhoId, out Guid gToWhoId))
         {
             if (_onlineUsersService.IsUserOnline(toWhoId))
             {
                 string toWhoConnectionId = _onlineUsersService.GetOnlineUser(toWhoId).ConnectionId;
                 await Clients.Client(toWhoConnectionId).SendAsync("ReceiveMessage", fromWhoUser, message, when);
             }
             await _messageService.AddMessageAsync(gToWhoId, new MessageToAddDto
             {
                 FromWho = Guid.Parse(fromWhoUser),
                 Text    = message,
                 When    = when
             });
         }
         else
         {
             throw new ArgumentNullException(nameof(toWhoId));
         }
     }
     catch (Exception ex)
     {
         _logger.LogError("Something went wrong during sending message to the user: {userId}", toWhoId);
         _logger.LogError("{0}", ex);
     }
     return;
 }