예제 #1
0
        public override async Task OnConnectedAsync()
        {
            if (Context.User?.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated)
            {
                Debug.Fail("User was not authenticated when connecting to hub.");
                return;
            }

            string roomId     = identity.FindFirst(JwtConfig.RoomIdClaimType) !.Value;
            bool   isAdmin    = identity.FindFirst(JwtConfig.RoleClaimType)?.Value == Policies.AdminRole;
            string?playerName = identity.FindFirst(JwtRegisteredClaimNames.GivenName)?.Value;

            if (!isAdmin)
            {
                Game?game = await _dbContext.GetGameByRoomId(roomId, tracking : false);

                if (game is null)
                {
                    throw new HubException($"Game '{roomId}' does not exist.");
                }

                if (!game.Players.Any(p => p.Name == playerName))
                {
                    throw new HubException($"There's no player with the name '{playerName}' in game '{roomId}'.");
                }
            }

            await Groups.AddToGroupAsync(Context.ConnectionId, roomId);

            //IList<string> connectionIds = s_connectionIds.GetOrAdd(Context.UserIdentifier!, new List<string>());
            //lock (connectionIds)
            //{
            //    connectionIds.Add(Context.ConnectionId);
            //}
        }