ChatHub.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.SignalR;
  3. using MTWorkHR.Application.Filters;
  4. using MTWorkHR.Application.Identity;
  5. using MTWorkHR.Application.Mapper;
  6. using MTWorkHR.Application.Models;
  7. using MTWorkHR.Application.Services;
  8. using MTWorkHR.Core.Entities;
  9. using MTWorkHR.Core.Entities.Base;
  10. using MTWorkHR.Core.Global;
  11. using MTWorkHR.Core.UnitOfWork;
  12. using MTWorkHR.Infrastructure.Entities;
  13. using System;
  14. using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
  15. namespace MTWorkHR.API.Chat
  16. {
  17. [AppAuthorize]
  18. public class ChatHub : Hub
  19. {
  20. private readonly IUnitOfWork _unitOfWork;
  21. private readonly GlobalInfo _globalInfo;
  22. private readonly UserService _userService;
  23. public ChatHub(IUnitOfWork unitOfWork, GlobalInfo globalInfo, UserService userService)
  24. {
  25. _unitOfWork = unitOfWork;
  26. _globalInfo = globalInfo;
  27. _userService = userService;
  28. }
  29. public async Task authMe()
  30. {
  31. string currSignalrID = Context.ConnectionId;
  32. //Person tempPerson = ctx.Person.Where(p => p.Username == personInfo.userName && p.Password == personInfo.password)
  33. // .SingleOrDefault();
  34. if (_globalInfo.UserId != null) //if credentials are correct
  35. {
  36. Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
  37. HubConnection currUser = new HubConnection
  38. {
  39. UserId = _globalInfo.UserId,
  40. SignalrId = currSignalrID,
  41. TimeStamp = DateTime.Now
  42. };
  43. var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
  44. await _unitOfWork.CompleteAsync();
  45. ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
  46. await Clients.Caller.SendAsync("authMeResponseSuccess", newUser);//4Tutorial
  47. await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
  48. }
  49. else //if credentials are incorrect
  50. {
  51. await Clients.Caller.SendAsync("authMeResponseFail");
  52. }
  53. }
  54. public async Task getCompanyUsersList()
  55. {
  56. var companyUsers = await _userService.GetAllCompanyEmployees();
  57. var response = MapperObject.Mapper.Map<List<UserAllDto>>(companyUsers);
  58. await Clients.Caller.SendAsync("getCompanyUsersList", response);
  59. }
  60. //Simple Test Method
  61. public async Task SendMessageAll(string user, string message)
  62. {
  63. await Clients.All.SendAsync("ReceiveMessage", user, message);
  64. }
  65. public async Task sendMsg(string userId, string msg)
  66. {
  67. var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
  68. var receiverUser = allConnections.Item1.FirstOrDefault(c => c.UserId == userId);
  69. var connId = receiverUser != null ? receiverUser.SignalrId : "0";
  70. ChatMessage messag = new ChatMessage { Content = msg, ReceiverId = connId, SenderId = _globalInfo.UserId, SenderName = _globalInfo.UserName, IsSeen = false };
  71. await Clients.Client(connId).SendAsync("sendMsgResponse", Context.ConnectionId, msg);
  72. }
  73. public async Task getAllMessagesByUser(string senderId)
  74. {
  75. var allmessages = await _unitOfWork.ChatMessage.GetAllWithChildrenAsync(senderId, _globalInfo.UserId);
  76. await Clients.Caller.SendAsync("allMessagesResponse", allmessages.Item1);
  77. }
  78. public async override Task<Task> OnDisconnectedAsync(Exception exception)
  79. {
  80. var connections = await _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId);
  81. var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault();
  82. _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList());
  83. _unitOfWork.CompleteAsync();
  84. await Clients.Others.SendAsync("userOff", currUserId);
  85. return base.OnDisconnectedAsync(exception);
  86. }
  87. public void logOut(Guid personId)
  88. {
  89. var connections = _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId).Result;
  90. // var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault();
  91. _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList());
  92. _unitOfWork.CompleteAsync();
  93. Clients.Caller.SendAsync("logoutResponse");
  94. Clients.Others.SendAsync("userOff", personId);
  95. }
  96. public async Task getOnlineUsers()
  97. {
  98. var allConnections = await _unitOfWork.HubConnection.GetAllAsync();
  99. var currUserId = allConnections.Item1.Where(c => c.SignalrId == Context.ConnectionId).Select(c => c.UserId).SingleOrDefault();
  100. List<ChatUserDto> onlineUsers = allConnections.Item1
  101. .Where(c => c.UserId != currUserId)
  102. .Select(c =>
  103. new ChatUserDto(c.UserId, c.UserName, c.SignalrId)
  104. ).ToList();
  105. await Clients.Caller.SendAsync("getOnlineUsersResponse", onlineUsers);
  106. }
  107. //public async Task reauthMe(string userId)
  108. //{
  109. // string currSignalrID = Context.ConnectionId;
  110. // //ApplicationUser tempPerson = ctx.Person.Where(p => p.Id == personId)
  111. // // .SingleOrDefault();
  112. // if (userId == _globalInfo.UserId) //if credentials are correct
  113. // {
  114. // Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
  115. // HubConnection currUser = new HubConnection
  116. // {
  117. // UserId = _globalInfo.UserId,
  118. // SignalrId = currSignalrID,
  119. // TimeStamp = DateTime.Now
  120. // };
  121. // var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
  122. // await _unitOfWork.CompleteAsync();
  123. // ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
  124. // await Clients.Caller.SendAsync("reauthMeResponse", newUser);//4Tutorial
  125. // await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
  126. // }
  127. //} //end of reauthMe
  128. }
  129. }