Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ChatHub.cs 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. namespace Diligent.WebAPI.Host.Hubs
  11. {
  12. public class ChatHub : Hub<IChatClient>
  13. {
  14. private readonly IDictionary<string, UserConnection> _connections;
  15. private readonly IMediator _mediator;
  16. public ChatHub(IDictionary<string, UserConnection> connections,IMediator mediator)
  17. {
  18. _connections = connections;
  19. _mediator = mediator;
  20. }
  21. //public override Task OnDisconnectedAsync(Exception? exception)
  22. //{
  23. // if (_connections.TryGetValue(Context.ConnectionId, out UserConnection room))
  24. // {
  25. // _connections.Remove(Context.ConnectionId);
  26. // Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.UserId} has left room" });
  27. // }
  28. // return base.OnDisconnectedAsync(exception);
  29. //}
  30. [HubMethodName("SendMessageToGroup")]
  31. public async Task SendMessageToGroup(ChatMessage message)
  32. {
  33. // Find user's room and send message to everyone
  34. // All other users will receive notification
  35. if (_connections.TryGetValue(message.ConnId, out UserConnection room))
  36. {
  37. await Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = message.Message });
  38. await Clients.OthersInGroup(room.RoomId).ReceiveNotifications(room.UserId, room.RoomId);
  39. await _mediator.Send(new AddMessageCommand(room.RoomId, new Message { Content = message.Message, SenderId = message.UserId, Username = room.Username }));
  40. // Find other users in room and save notification in database
  41. foreach (KeyValuePair<string, UserConnection> conn in _connections)
  42. {
  43. if (conn.Value.RoomId == room.RoomId && conn.Key != message.ConnId)
  44. {
  45. await _mediator.Send(new AddNotificationCommand(new NotificationSaveDTO { RoomId = room.RoomId, ReceiverId = conn.Value.UserId }));
  46. }
  47. }
  48. }
  49. }
  50. [HubMethodName("JoinRoom")]
  51. public async Task JoinRoom(UserConnection userConnection)
  52. {
  53. // Check is user in requested room
  54. var result = _connections.Where(x => x.Value.UserId == userConnection.UserId && x.Value.RoomId == userConnection.RoomId).FirstOrDefault();
  55. // If user is not in room, add him
  56. // Return message "User has joined room" to all users in room
  57. if (result.Value == null)
  58. {
  59. await Groups.AddToGroupAsync(Context.ConnectionId, userConnection.RoomId);
  60. _connections[Context.ConnectionId] = userConnection;
  61. await Clients.Group(userConnection.RoomId).ReceiveMessage(new ChatMessage { User = userConnection.UserId, Message = $"{userConnection.Username} has joined room", ConnId = Context.ConnectionId });
  62. }
  63. else
  64. {
  65. await _mediator.Send(new DeleteNotificationCommand(new NotificationDeleteDTO { UserId = userConnection.UserId, RoomId = userConnection.RoomId }));
  66. }
  67. }
  68. [HubMethodName("LeaveRoom")]
  69. public async Task LeaveRoom(LeaveChatRoomDTO connection)
  70. {
  71. if (_connections.TryGetValue(connection.ConnId, out UserConnection room))
  72. {
  73. _connections.Remove(Context.ConnectionId);
  74. await Clients.OthersInGroup(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.Username} has left room" });
  75. await _mediator.Send(new RemoveUserFromGroupCommand(room.RoomId, room.UserId));
  76. }
  77. }
  78. }
  79. }