Keywords: UE4, Networking

The previous article explained how to connect Dedicated Server via IP address:
[UE4]Networking in Basic - Simple Replication Example
https://dawnarc.com/2017/02/ue4networking-in-basic-simple-replication-example/

Current article explained how to connect Server via Sessions(without IP).

Engine API

OnlineSubsystem

BP Node CreateSession in native:
Source Path:

\Engine\Plugins\Online\OnlineSubsystemUtils\Source\OnlineSubsystemUtils\Private\CreateSessionCallbackProxy.cpp

Native Function:

// Creates a session with the default online subsystem
UCreateSessionCallbackProxy* UCreateSessionCallbackProxy::CreateSession(UObject* WorldContextObject, class APlayerController* PlayerController, int32 PublicConnections, bool bUseLAN)
{
    UCreateSessionCallbackProxy* Proxy = NewObject<UCreateSessionCallbackProxy>();
    Proxy->PlayerControllerWeakPtr = PlayerController;
    Proxy->NumPublicConnections = PublicConnections;
    Proxy->bUseLAN = bUseLAN;
    Proxy->WorldContextObject = WorldContextObject;
    return Proxy;
}

BP Node FindSessions in native:
Source Path:

\Engine\Plugins\Online\OnlineSubsystemUtils\Source\OnlineSubsystemUtils\Private\FindSessionsCallbackProxy.cpp

Native Function:

// Searches for advertised sessions with the default online subsystem
UFindSessionsCallbackProxy* UFindSessionsCallbackProxy::FindSessions(UObject* WorldContextObject, class APlayerController* PlayerController, int MaxResults, bool bUseLAN)
{
    UFindSessionsCallbackProxy* Proxy = NewObject<UFindSessionsCallbackProxy>();
    Proxy->PlayerControllerWeakPtr = PlayerController;
    Proxy->bUseLAN = bUseLAN;
    Proxy->MaxResults = MaxResults;
    Proxy->WorldContextObject = WorldContextObject;
    return Proxy;
}

BP Node JoinSession in native:
Source Path: \Engine\Plugins\Online\OnlineSubsystemUtils\Source\OnlineSubsystemUtils\Private\JoinSessionCallbackProxy.cpp

Native Function:

UJoinSessionCallbackProxy* UJoinSessionCallbackProxy::JoinSession(UObject* WorldContextObject, class APlayerController* PlayerController, const FBlueprintSessionResult& SearchResult)
{
    UJoinSessionCallbackProxy* Proxy = NewObject<UJoinSessionCallbackProxy>();
    Proxy->PlayerControllerWeakPtr = PlayerController;
    Proxy->OnlineSearchResult = SearchResult.OnlineResult;
    Proxy->WorldContextObject = WorldContextObject;
    return Proxy;
}

Other BP Nodes of Session:

\Engine\Plugins\Online\OnlineSubsystemUtils\Source\OnlineSubsystemUtils\Private\ConnectionCallbackProxy.cpp
\Engine\Plugins\Online\OnlineSubsystemUtils\Source\OnlineSubsystemUtils\Private\DestroySessionCallbackProxy.cpp

Dedicated Server

Creating a session on Dedicated Server launch?

New a customized GameSession class and override RegisterServer function, then copy following code block into it.

UWorld* World = GetWorld();
IOnlineSessionPtr SessionInt = Online::GetSessionInterface(World);
 
FOnlineSessionSettings Settings;
Settings.NumPublicConnections = 3;
Settings.bShouldAdvertise = true;
Settings.bAllowJoinInProgress = true;
Settings.bIsLANMatch = true;
Settings.bUsesPresence = true;
Settings.bAllowJoinViaPresence = true;

SessionInt->CreateSession(0, GameSessionName, Settings);
return;

How to set customized GameSession Class?

Reference
https://answers.unrealengine.com/questions/260999/creating-a-session-on-dedicated-server-launch.html

Docs Reference

Official Docs

Online Session Nodes
https://docs.unrealengine.com/en-US/Engine/Blueprints/UserGuide/OnlineNodes/index.html

How To Use Sessions In C++
https://wiki.unrealengine.com/How_To_Use_Sessions_In_C%2B%2B

YouTube Tutorials

Unreal Engine 4 | Server browser | Host & Join Session | SESSION SERIES
https://www.youtube.com/watch?v=NW4QHiK6YbQ

Steam Multiplayer - Advanced Steam Session - Unreal Engine
https://www.youtube.com/watch?v=ke3kmK6bvT0

Blueprint Multiplayer
https://www.youtube.com/watch?v=abmzWUWxy1U&list=PLZlv_N0_O1gYqSlbGQVKsRg6fpxWndZqZ
Blueprint Multiplayer: Game Instance | 03 | v4.11 Tutorial Series | Unreal Engine
https://www.youtube.com/watch?v=78XJYBfWXAA

[ue4] Setup Advanced Session - Steam Multiplayer
https://www.youtube.com/watch?v=rWs6SyyVpTE&list=PL3TrrCsmmxllnBvhucQ4c683a2VltXBx6

[UE4 - BP Tutorial] Multiplayer Session
https://www.youtube.com/watch?v=so0-0dafVsE&list=PLLiI_CRDNPL-FUUPcViHe6Ypabo1ghXh3


请原谅我不敢祝愿每一位毕业生都成功、都幸福;因为历史不幸地记载着:有人的成功代价是丧失良知;有人的幸福代价是损害他人。 ----饶毅教授2015北大毕业典礼演讲