How to block public connection in a GamePlay map? (OnlineSubsystem + DedicatedServer)

I’m gathering players in a map (“lobby”) that has its own GemeMode.
On this map I can control the number of players allowed setting this variable.

FOnlineSessionSettings Settings;
Settings.NumPublicConnections = 1;

Until now everything is perfect. If a second player tries to enter the lobby it will be rejected.

The problem is after making the server travel.
From the Lobby map I send the player to another map (Level1) using a server travel.
Level 1 has a different GameMode than the Lobby.

So if a client tries to connect to the server it travels directly to Level1 without going through the Lobby.(It seems to be in spectator mode). It looks like the lobby map was overwritten by Level1 as main map.

But I would prefer the client to be rejected in this case too. It is not interesting to have an indefinite number of clients in spectator mode. Especially since they are using resources that can affect performance.


Then try disabling the number of public connections (Like this).

FOnlineSessionSettings SessionSettings = FOnlineSessionSettings();			
SessionSettings.NumPublicConnections = 0;
SessionSettings.bAllowJoinInProgress = false;
SessionSettings.bShouldAdvertise = false;		


	const IOnlineSessionPtr SessionPtr = Session::GetInterface();
	if(SessionPtr == nullptr)
	{
		return;
	}
	SessionPtr->UpdateSession( NAME_GameSession, SessionSettings);
	

But clients can connect anyway.


I also tried with GameSession but without success



	if (GameSession == nullptr)
	{
		return;
	}
	GameSession->MaxPlayers = 0;
	GameSession->MaxSpectators = 0;
	GameSession->MaxPartySize = 0;

GameSession->UpdateSessionJoinability(NAME_GameSession,false,false,false,	false);

I tried it on BeginPlay() and PreLogin()
In both Game Modes (Lobby and Level 1)
But there is no success, clients can connect.


I don’t know why it doesn’t work.
Does anyone know?
How can I make it work?

Thank you so much!!

I think i found a solution… seems to work, but it is completily invented by me

void AGamePlayGameMode::PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId,	FString& ErrorMessage)
{	

	IOnlineSessionPtr SessionPtr = Session::GetInterface();
	if(SessionPtr == nullptr)
	{
		Message::Error("AGamePlayGameMode::PreLogin --> SessionPtr is NULL");
		return;
	}

	if (!UniqueId.IsValid())
	{
		Message::Error("AGamePlayGameMode::PreLogin --> UniqueNetId is NULL");
		return;		
	}

	if(SessionPtr->IsPlayerInSession(NAME_GameSession, *UniqueId))
	{
		ErrorMessage = GameSession->ApproveLogin(Options);
		Message::Warning("PLAYER IS REGISTER WITH SESSION");
		
	}
	else
	{
		ErrorMessage = TEXT("PLAYER IS NOT REGISTER WITH SESSION");
		Message::Warning("PLAYER IS NOT REGISTER WITH SESSION");
	}

	FGameModeEvents::GameModePreLoginEvent.Broadcast(this, UniqueId, ErrorMessage);	
	
}

I got the idea from this post:

if someone know a better solution let know please!!

Thank you so much!!


Edited:


Well, seeing none provide a better solution i wiil acept my own answer as solution…
However feel free to provide a better solution if you know it… even if this post is old in the future.
thx u

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.