Download

UE4.22.3 C++ After leaving a Steam session the same client is unable to find the same server.

I’ve successfully connected steam and created a server client connection even after packaging. However after a client leaves or disconnects or the game crashes for client, the client is not able to find the same server again. Other players can but the player who left can’t.
Please let me know how to solve this.

I’m using basic OSS from UE, I’m not using the advance sessions plugin. All my code is in C++ and is inside the GameInstance for joining, finding etc. for leaving the game I’m using the player controller.

/


/Finding sessions


void UMSurInstance::FindSessions()
{
SessionSearch = MakeShareable(new FOnlineSessionSearch());

if (SessionSearch.IsValid())
{
if (IOnlineSubsystem::Get()->GetSubsystemName().IsEqual("NULL"))
{
SessionSearch->bIsLanQuery = true;
}
else
{
SessionSearch->bIsLanQuery = false;
}


SessionSearch->MaxSearchResults = 100;
SessionSearch->QuerySettings.Set(SEARCH_PRESENCE, true, EOnlineComparisonOp::Equals);




UE_LOG(LogTemp, Warning, TEXT("Start Finding Sessions"));
sessionInterface->FindSessions(0, SessionSearch.ToSharedRef());
}
}

void UMSurInstance::OnSessionSearchSuccess(bool success)
{

if (success)
{
TArray<FServerData> FoundServerLists;
//FServerData serverdata1; serverdata1.ServerName = "Test 1"; FServerData serverdata2; serverdata2.ServerName = "Test 2";
//FoundServerLists.Add(serverdata1);
//FoundServerLists.Add(serverdata2);

if( !SessionSearch.IsValid() )
{
return;
//Add no game found to list if no games are active
}

for (const FOnlineSessionSearchResult& searchresult : SessionSearch->SearchResults)
{

UE_LOG(LogTemp, Warning, TEXT("Found session name : %s"), *searchresult.GetSessionIdStr());

FServerData serverdata;

serverdata.ServerName = searchresult.GetSessionIdStr();
serverdata.MaxPlayers = searchresult.Session.SessionSettings.NumPublicConnections;
serverdata.CurrentPlayers = serverdata.MaxPlayers - searchresult.Session.NumOpenPublicConnections;
serverdata.HostUsername = searchresult.Session.OwningUserName;
serverdata.ServerPing = searchresult.PingInMs;


FString servername;
if (searchresult.Session.SessionSettings.Get(SESSION_NAME_SETTINGS_KEY, servername))
{
serverdata.ServerName = servername;
}
else
{
serverdata.ServerName = searchresult.GetSessionIdStr();
}





FoundServerLists.Add(serverdata);


}

menuWidget->SetServerList(FoundServerLists);


UE_LOG(LogTemp, Warning, TEXT("Finished Search"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("No Sessions found"));
}



}

Leaving for Player on death inside Playercontroller



#pragma region Player Dead

void AMSurPlayerController::PlayerDead()
{

    if (Role < ROLE_Authority)
    {
        UE_LOG(LogTemp, Warning, TEXT("Client End session called"));
        ServerPlayerDead();
    }

    if (Role == ROLE_Authority)
    {
        UE_LOG(LogTemp, Warning, TEXT("Server End session called"));

    }




    IOnlineSubsystem* subsys = IOnlineSubsystem::Get();
    if (subsys != nullptr)
    {
        IOnlineSessionPtr SessionInterface = subsys->GetSessionInterface();
        if (SessionInterface.IsValid())
        {

            //FUniqueNetId PlayerKilledID;

            //SessionInterface->EndSession(TEXT("MySessionName"));
            //use destroy session
            SessionInterface->DestroySession(TEXT("MySessionName"), onSessionDestroy);


            //UE_LOG(LogTemp, Warning, TEXT("%s Session end called"), *GetName());
            //SessionInterface->EndSession(MySessionName);


            //SessionInterface->UnregisterPlayer(TEXT("MySessionName"), PlayerKilledID);

        }
        else
        {
            UE_LOG(LogTemp, Warning, TEXT("Sesion Interface is nullptr"))
        }
    }
    else
    {
        UE_LOG(LogTemp,Warning,TEXT("Subsystem is nullptr"))
    }



    //Client_LoadOnDeathWidget();

    //UnPossess();



}



void AMSurPlayerController::ServerPlayerDead_Implementation()
{
    //Kill actor.
    UE_LOG(LogTemp, Warning, TEXT("Stop movement and remove skeletal mesh"));

    //AMSurAnimalCharacter* actorToKill = Cast<AMSurAnimalCharacter>(GetPawn());

    //actorToKill->GetMesh()->SetVisibility(false, false);
    //actorToKill->InputComponent->Deactivate();

}
bool AMSurPlayerController::ServerPlayerDead_Validate()
{
    return true;
}


]

My code is a bloody mess but right now it gets the work done. If anyone knows how to deal with this please comment.

Maybe it is sufficient to call


 
 SessionInterface->RemoveNamedSession(TEXT("MySessionName") 

before calling


Sessions->JoinSession(...);