ChatHub.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.SignalR;
  3. using MTWorkHR.Application.Filters;
  4. using MTWorkHR.Application.Identity;
  5. using MTWorkHR.Application.Models;
  6. using MTWorkHR.Core.Entities;
  7. using MTWorkHR.Core.Entities.Base;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Core.UnitOfWork;
  10. using MTWorkHR.Infrastructure.Entities;
  11. using System;
  12. using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
  13. namespace MTWorkHR.API.Chat
  14. {
  15. [AppAuthorize]
  16. public class ChatHub : Hub
  17. {
  18. private readonly IUnitOfWork _unitOfWork;
  19. private readonly GlobalInfo _globalInfo;
  20. public ChatHub(IUnitOfWork unitOfWork, GlobalInfo globalInfo)
  21. {
  22. _unitOfWork = unitOfWork;
  23. _globalInfo = globalInfo;
  24. }
  25. public async Task SendMessage(string user, string message)
  26. {
  27. await Clients.All.SendAsync("ReceiveMessage", user, message);
  28. }
  29. public override Task OnDisconnectedAsync(Exception exception)
  30. {
  31. var connections = _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId).Result;
  32. var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault();
  33. _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList());
  34. _unitOfWork.CompleteAsync();
  35. Clients.Others.SendAsync("userOff", currUserId);
  36. return base.OnDisconnectedAsync(exception);
  37. }
  38. //2Tutorial
  39. public async Task authMe(string userId)
  40. {
  41. string currSignalrID = Context.ConnectionId;
  42. //Person tempPerson = ctx.Person.Where(p => p.Username == personInfo.userName && p.Password == personInfo.password)
  43. // .SingleOrDefault();
  44. if (userId == _globalInfo.UserId) //if credentials are correct
  45. {
  46. Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
  47. HubConnection currUser = new HubConnection
  48. {
  49. UserId = _globalInfo.UserId,
  50. SignalrId = currSignalrID,
  51. TimeStamp = DateTime.Now
  52. };
  53. var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
  54. await _unitOfWork.CompleteAsync();
  55. ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
  56. await Clients.Caller.SendAsync("authMeResponseSuccess", newUser);//4Tutorial
  57. await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
  58. }
  59. else //if credentials are incorrect
  60. {
  61. await Clients.Caller.SendAsync("authMeResponseFail");
  62. }
  63. }
  64. //3Tutorial
  65. public async Task reauthMe(string userId)
  66. {
  67. string currSignalrID = Context.ConnectionId;
  68. //ApplicationUser tempPerson = ctx.Person.Where(p => p.Id == personId)
  69. // .SingleOrDefault();
  70. if (userId == _globalInfo.UserId) //if credentials are correct
  71. {
  72. Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID);
  73. HubConnection currUser = new HubConnection
  74. {
  75. UserId = _globalInfo.UserId,
  76. SignalrId = currSignalrID,
  77. TimeStamp = DateTime.Now
  78. };
  79. var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser);
  80. await _unitOfWork.CompleteAsync();
  81. ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID);
  82. await Clients.Caller.SendAsync("reauthMeResponse", newUser);//4Tutorial
  83. await Clients.Others.SendAsync("userOn", newUser);//4Tutorial
  84. }
  85. } //end of reauthMe
  86. //4Tutorial
  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. }
  97. }