Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ChatHub.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Diligent.WebAPI.Data.Entities;
  2. using Diligent.WebAPI.Host.DTOs.Notification;
  3. using Diligent.WebAPI.Host.Mediator.Messages.Commands;
  4. using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
  5. using MediatR;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.SignalR;
  8. namespace Diligent.WebAPI.Host.Hubs
  9. {
  10. public class ChatHub : Hub<IChatClient>
  11. {
  12. private readonly IDictionary<string, UserConnection> _connections;
  13. private readonly IMediator _mediator;
  14. public ChatHub(IDictionary<string, UserConnection> connections,IMediator mediator)
  15. {
  16. _connections = connections;
  17. _mediator = mediator;
  18. }
  19. //public override Task OnDisconnectedAsync(Exception? exception)
  20. //{
  21. // if (_connections.TryGetValue(Context.ConnectionId, out UserConnection room))
  22. // {
  23. // _connections.Remove(Context.ConnectionId);
  24. // Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.UserId} has left room" });
  25. // }
  26. // return base.OnDisconnectedAsync(exception);
  27. //}
  28. [HubMethodName("SendMessageToGroup")]
  29. public async Task SendMessageToGroup(ChatMessage message)
  30. {
  31. // Find user's room and send message to everyone
  32. // All other users will receive notification
  33. if (_connections.TryGetValue(message.ConnId, out UserConnection room))
  34. {
  35. await Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = message.Message });
  36. await Clients.OthersInGroup(room.RoomId).ReceiveNotifications(room.UserId, room.RoomId);
  37. await _mediator.Send(new AddMessageCommand(room.RoomId, new Message { Content = message.Message, SenderId = message.UserId, Username = room.Username }));
  38. // Find other users in room and save notification in database
  39. foreach (KeyValuePair<string, UserConnection> conn in _connections)
  40. {
  41. if (conn.Value.RoomId == room.RoomId && conn.Key != message.ConnId)
  42. {
  43. await _mediator.Send(new AddNotificationCommand(new NotificationSaveDTO { RoomId = room.RoomId, ReceiverId = conn.Value.UserId }));
  44. }
  45. }
  46. }
  47. }
  48. [HubMethodName("JoinRoom")]
  49. public async Task JoinRoom(UserConnection userConnection)
  50. {
  51. // Check is user in requested room
  52. var result = _connections.Where(x => x.Value.UserId == userConnection.UserId && x.Value.RoomId == userConnection.RoomId).FirstOrDefault();
  53. // If user is not in room, add him
  54. // Return message "User has joined room" to all users in room
  55. if (result.Value == null)
  56. {
  57. await Groups.AddToGroupAsync(Context.ConnectionId, userConnection.RoomId);
  58. _connections[Context.ConnectionId] = userConnection;
  59. await Clients.Group(userConnection.RoomId).ReceiveMessage(new ChatMessage { User = userConnection.UserId, Message = $"{userConnection.Username} has joined room", ConnId = Context.ConnectionId });
  60. }
  61. else
  62. {
  63. await _mediator.Send(new DeleteNotificationCommand(new NotificationDeleteDTO { UserId = userConnection.UserId, RoomId = userConnection.RoomId }));
  64. }
  65. }
  66. }
  67. }