public async Task EnterRoom(EnterRoomModel enterRoomModel) { if (enterRoomModel == null) { await Clients.Caller.SendAsync("Error", new { Error = "invalid_model" }); return; } string confirmToken = null; //check token if (enterRoomModel.ParticipantType == ParticipantType.Player) { confirmToken = await _roomRepo.GetPlayerTokenByIdAsync(enterRoomModel.RoomId); } else if (enterRoomModel.ParticipantType == ParticipantType.Listener) { confirmToken = await _roomRepo.GetListenTokenByIdAsync(enterRoomModel.RoomId); } //send error if participant`s type is invalid if (confirmToken == null) { await Clients.Caller.SendAsync("Error", new { Error = "invalid_type" }); return; } //send error if token is invalid if (confirmToken != enterRoomModel.Token) { await Clients.Caller.SendAsync("Error", new { Error = "invalid_token" }); return; } //get room var room = await _roomRepo.FindByIdAsync(enterRoomModel.RoomId); //send error if room doesn`t exist if (room == null) { await Clients.Caller.SendAsync("Error", new { Error = "doesnt_exist" }); } //Get the players who are in the room now var participants = await _participantRepo.GetAsync(p => p.RoomId == room.Id && p.ParticipantType == ParticipantType.Player); //Delete the room if there are no players in it, and the waiting time for the first player has expired if (!participants.Any() && room.FirstConnectionExpired < DateTime.Now) { await _roomRepo.RemoveAsync(room, false); await Clients.Caller.SendAsync("Error", new { Error = "doesnt_exist" }); await _roomRepo.SaveChangesAsync(); return; } //Add participant to the room await Groups.AddToGroupAsync(Context.ConnectionId, room.Name); var participant = new Participant { ConnectionId = Context.ConnectionId, RoomId = room.Id, ParticipantType = enterRoomModel.ParticipantType, Name = enterRoomModel.Name }; await _participantRepo.CreateAsync(participant, false); var participantViewModel = participant.Adapt <ParticipantViewModel>(); participantViewModel.RoomName = room.Name; await Clients.Group(room.Name).SendAsync("Join", participantViewModel); await _participantRepo.SaveChangesAsync(); }