Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Diligent.WebAPI.Data.Entities;
  2. using Diligent.WebAPI.Host.DTOs.Chat;
  3. using Diligent.WebAPI.Host.Mediator.Chat.Commands;
  4. using Diligent.WebAPI.Host.Mediator.Messages.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. // Not completed
  29. //[HubMethodName("SendMessageToUser")]
  30. //public async Task DirectMessage(string message)
  31. //{
  32. // // Send message to another user
  33. // await Clients.User(message.User).ReceiveMessage(new ChatMessage { User = "Ermin", Message = message.Message });
  34. //}
  35. [HubMethodName("SendMessageToGroup")]
  36. public async Task SendMessageToGroup(ChatMessage message)
  37. {
  38. // Find user's room and send message to everyone
  39. //if (_connections.TryGetValue(Context.ConnectionId, out UserConnection room))
  40. if (_connections.TryGetValue(message.ConnId, out UserConnection room))
  41. {
  42. await Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = message.Message });
  43. await _mediator.Send(new AddMessageCommand(room.RoomId, new Message {Content=message.Message,SenderId=message.UserId,Username=room.Username }));
  44. }
  45. }
  46. [HubMethodName("JoinRoom")]
  47. public async Task JoinRoom(UserConnection userConnection)
  48. {
  49. // Check is user in requested room
  50. var result = _connections.Where(x => x.Value.UserId == userConnection.UserId && x.Value.RoomId == userConnection.RoomId).FirstOrDefault();
  51. // If user is not in room, add him
  52. // Return message "User has joined room" to all users in room
  53. if(result.Value == null)
  54. {
  55. await Groups.AddToGroupAsync(Context.ConnectionId, userConnection.RoomId);
  56. _connections[Context.ConnectionId] = userConnection;
  57. await Clients.Group(userConnection.RoomId).ReceiveMessage(new ChatMessage { User = userConnection.UserId, Message = $"{userConnection.Username} has joined room", ConnId = Context.ConnectionId });
  58. }
  59. }
  60. [HubMethodName("LeaveRoom")]
  61. public async Task LeaveRoom(LeaveChatRoomDTO connection)
  62. {
  63. if (_connections.TryGetValue(connection.ConnId, out UserConnection room))
  64. {
  65. _connections.Remove(Context.ConnectionId);
  66. await Clients.OthersInGroup(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.Username} has left room" });
  67. await _mediator.Send(new RemoveUserFromGroupCommand(room.RoomId, room.UserId));
  68. }
  69. }
  70. }
  71. }