using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.SignalR; using MTWorkHR.Application.Filters; using MTWorkHR.Application.Identity; using MTWorkHR.Application.Models; using MTWorkHR.Core.Entities; using MTWorkHR.Core.Entities.Base; using MTWorkHR.Core.Global; using MTWorkHR.Core.UnitOfWork; using MTWorkHR.Infrastructure.Entities; using System; using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; namespace MTWorkHR.API.Chat { [AppAuthorize] public class ChatHub : Hub { private readonly IUnitOfWork _unitOfWork; private readonly GlobalInfo _globalInfo; public ChatHub(IUnitOfWork unitOfWork, GlobalInfo globalInfo) { _unitOfWork = unitOfWork; _globalInfo = globalInfo; } public async Task SendMessage(string user, string message) { await Clients.All.SendAsync("ReceiveMessage", user, message); } public override Task OnDisconnectedAsync(Exception exception) { var connections = _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId).Result; var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault(); _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList()); _unitOfWork.CompleteAsync(); Clients.Others.SendAsync("userOff", currUserId); return base.OnDisconnectedAsync(exception); } //2Tutorial public async Task authMe(string userId) { string currSignalrID = Context.ConnectionId; //Person tempPerson = ctx.Person.Where(p => p.Username == personInfo.userName && p.Password == personInfo.password) // .SingleOrDefault(); if (userId == _globalInfo.UserId) //if credentials are correct { Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID); HubConnection currUser = new HubConnection { UserId = _globalInfo.UserId, SignalrId = currSignalrID, TimeStamp = DateTime.Now }; var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser); await _unitOfWork.CompleteAsync(); ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID); await Clients.Caller.SendAsync("authMeResponseSuccess", newUser);//4Tutorial await Clients.Others.SendAsync("userOn", newUser);//4Tutorial } else //if credentials are incorrect { await Clients.Caller.SendAsync("authMeResponseFail"); } } //3Tutorial public async Task reauthMe(string userId) { string currSignalrID = Context.ConnectionId; //ApplicationUser tempPerson = ctx.Person.Where(p => p.Id == personId) // .SingleOrDefault(); if (userId == _globalInfo.UserId) //if credentials are correct { Console.WriteLine("\n" + _globalInfo.UserName + " logged in" + "\nSignalrID: " + currSignalrID); HubConnection currUser = new HubConnection { UserId = _globalInfo.UserId, SignalrId = currSignalrID, TimeStamp = DateTime.Now }; var newConnection = await _unitOfWork.HubConnection.AddAsync(currUser); await _unitOfWork.CompleteAsync(); ChatUserDto newUser = new ChatUserDto(_globalInfo.UserId, _globalInfo.UserName, currSignalrID); await Clients.Caller.SendAsync("reauthMeResponse", newUser);//4Tutorial await Clients.Others.SendAsync("userOn", newUser);//4Tutorial } } //end of reauthMe //4Tutorial public void logOut(Guid personId) { var connections = _unitOfWork.HubConnection.GetAllAsync(Context.ConnectionId).Result; // var currUserId = connections.Item1.Select(c => c.UserId).SingleOrDefault(); _unitOfWork.HubConnection.DeleteRangeAsync(connections.Item1.ToList()); _unitOfWork.CompleteAsync(); Clients.Caller.SendAsync("logoutResponse"); Clients.Others.SendAsync("userOff", personId); } } }