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 2.9KB

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