Starting a Server Session
Prerequisites
#include "ServerSession.h"
#include "LootLockerServerForCpp.h"
#include "Templates/SharedPointer.h"
#include "TimerManager.h"
// Timer handle to maintain the session
FTimerHandle MaintainSessionTimerHandle;
void OnServerAuthCompleted(FLootLockerServerAuthenticationResponse Response)
{
if (Response.Success)
{
UE_LOG(LogTemp, Log, TEXT("LootLocker server session started successfully. Next refresh in 55 minutes."));
// Here we create a new instance of ServerSession to refresh the session.
// We recommend using a singleton pattern or a static instance for better management in a real application.
// Adapt to your game code as needed.
TSharedPtr<ServerSession> MyServerSession = MakeShared<ServerSession>();
if (GWorld)
{
FTimerDelegate TimerDel = FTimerDelegate::CreateRaw(MyServerSession, &ServerSession::MaintainServerSession);
GWorld->GetTimerManager().SetTimer(
MyServerSession->MaintainSessionTimerHandle,
TimerDel,
3300.0f, // 3300 seconds = 55 minutes
true // Loop the timer to maintain the session every 55 minutes
);
}
}
else
{
UE_LOG(LogTemp, Error, TEXT("Failed to start LootLocker session: %s"), *Response.ErrorData.Message);
}
}
// Call this function when you start your Server: GameModeBase, GameInstance, Server Actor, etc.
void ServerSession::StartLootLockerSession()
{
// CPP delegate
FLootLockerServerAuthResponseDelegate CPPDelegate;
CPPDelegate.BindStatic(&OnServerAuthCompleted);
// Call Start Session
ULootLockerServerForCpp::StartSession(CPPDelegate);
}
void OnMaintainSessionCompleted(FLootLockerServerMaintainSessionResponse Response)
{
if (Response.Success)
{
UE_LOG(LogTemp, Log, TEXT("LootLocker server session refreshed. Next refresh in 55 minutes."));
}
else
{
UE_LOG(LogTemp, Error, TEXT("Failed to start refresh session: %s"), *Response.ErrorData.Message);
}
}
void ServerSession::MaintainServerSession()
{
// CPP delegate
FLootLockerServerMaintainSessionResponseDelegate CPPDelegate;
CPPDelegate.BindStatic(&OnMaintainSessionCompleted);
// Call Start Session
ULootLockerServerForCpp::MaintainSession(CPPDelegate);
}Conclusion
Last updated
Was this helpful?

