You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatHub.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Diligent.WebAPI.Data.Entities;
  2. using Diligent.WebAPI.Host.DTOs.Notification;
  3. using Diligent.WebAPI.Host.DTOs.Chat;
  4. using Diligent.WebAPI.Host.Mediator.Chat.Commands;
  5. using Diligent.WebAPI.Host.Mediator.Messages.Commands;
  6. using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
  7. using MediatR;
  8. using Microsoft.AspNetCore.Authorization;
  9. using Microsoft.AspNetCore.SignalR;
  10. using Diligent.WebAPI.Host.Middlewares;
  11. namespace Diligent.WebAPI.Host.Hubs
  12. {
  13. public class ChatHub : Hub<IChatClient>
  14. {
  15. private readonly IDictionary<string, UserConnection> _connections;
  16. private readonly IMediator _mediator;
  17. public ChatHub(IDictionary<string, UserConnection> connections,IMediator mediator)
  18. {
  19. _connections = connections;
  20. _mediator = mediator;
  21. }
  22. //public override Task OnDisconnectedAsync(Exception? exception)
  23. //{
  24. // if (_connections.TryGetValue(Context.ConnectionId, out UserConnection room))
  25. // {
  26. // _connections.Remove(Context.ConnectionId);
  27. // Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.UserId} has left room" });
  28. // }
  29. // return base.OnDisconnectedAsync(exception);
  30. //}
  31. [HubMethodName("SendMessageToGroup")]
  32. [AuthorizationHubFilter(Roles = "Customer,Support")]
  33. public async Task SendMessageToGroup(ChatMessage message)
  34. {
  35. // Find user's room and send message to everyone
  36. // All other users will receive notification
  37. if (_connections.TryGetValue(message.ConnId, out UserConnection room))
  38. {
  39. await Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = message.Message });
  40. await Clients.OthersInGroup(room.RoomId).ReceiveNotifications(room.UserId, room.RoomId);
  41. await _mediator.Send(new AddMessageCommand(room.RoomId, new Message { Content = message.Message, SenderId = message.UserId, Username = room.Username }));
  42. // Find other users in room and save notification in database
  43. foreach (KeyValuePair<string, UserConnection> conn in _connections)
  44. {
  45. if (conn.Value.RoomId == room.RoomId && conn.Key != message.ConnId)
  46. {
  47. await _mediator.Send(new AddNotificationCommand(new NotificationSaveDTO { RoomId = room.RoomId, ReceiverId = conn.Value.UserId }));
  48. }
  49. }
  50. }
  51. }
  52. [HubMethodName("JoinRoom")]
  53. [AuthorizationHubFilter(Roles = "Customer,Support")]
  54. public async Task JoinRoom(UserConnection userConnection)
  55. {
  56. // Check is user in requested room
  57. var result = _connections.Where(x => x.Value.UserId == userConnection.UserId && x.Value.RoomId == userConnection.RoomId).FirstOrDefault();
  58. // If user is not in room, add him
  59. // Return message "User has joined room" to all users in room
  60. if (result.Value == null)
  61. {
  62. await Groups.AddToGroupAsync(Context.ConnectionId, userConnection.RoomId);
  63. _connections[Context.ConnectionId] = userConnection;
  64. await Clients.Group(userConnection.RoomId).ReceiveMessage(new ChatMessage { User = userConnection.UserId, Message = $"{userConnection.Username} has joined room", ConnId = Context.ConnectionId });
  65. }
  66. else
  67. {
  68. await _mediator.Send(new DeleteNotificationCommand(new NotificationDeleteDTO { UserId = userConnection.UserId, RoomId = userConnection.RoomId }));
  69. }
  70. }
  71. [HubMethodName("LeaveRoom")]
  72. [AuthorizationHubFilter(Roles = "Customer,Support")]
  73. public async Task LeaveRoom(LeaveChatRoomDTO connection)
  74. {
  75. if (_connections.TryGetValue(connection.ConnId, out UserConnection room))
  76. {
  77. _connections.Remove(Context.ConnectionId);
  78. await Clients.OthersInGroup(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.Username} has left room" });
  79. await _mediator.Send(new RemoveUserFromGroupCommand(room.RoomId, room.UserId));
  80. }
  81. }
  82. [HubMethodName("SeeWhoIsTyping")]
  83. public async Task SeeWhoIsTyping(TypingInvokeMessage message)
  84. {
  85. // Find user's room and send message to everyone
  86. //if (_connections.TryGetValue(Context.ConnectionId, out UserConnection room))
  87. if (_connections.TryGetValue(message.ConnId, out UserConnection room))
  88. {
  89. // send the typing info to all in group except the caller
  90. // viewtyping(username + "is typing") // message, roomId
  91. //await Clients.GroupExcept(message.RoomId, Context.ConnectionId).ViewTyping(new TypingMessage { RoomId = room.RoomId, Message = message.Message });
  92. await Clients.GroupExcept(message.RoomId, message.ConnId).ViewTyping(new TypingMessage { RoomId = room.RoomId, Message = message.Message });
  93. }
  94. }
  95. }
  96. }