question

justin-1 avatar image
justin-1 asked

[Unity3D] How to retrieve SessionId on Multiplayer Server after Matchmaking succeeded?

I'm pretty new to hosting multiplayer games on dedicated servers. I've been trying to figure out how to use PlayFab Matchmaking to launch a Multiplayer Server. Our multiplayer game is using Photon Bolt with Unity3D. Once the server is launched, I want to use the SessionId from PlayFab Matchmaking to start a Photon Bolt session with the same session ID. The clients can then use the returned MatchId from PlayFab to join the correct Photon Bolt session.

My main question is: how do I retrieve the SessionId (and InitialPlayers and PreferredRegions) on the server? I tried using the event PlayFabMultiplayerAgentAPI.OnSessionConfigUpdateEvent, but everything inside the SessionConfig callback is always null. Also, the callback is sometimes called twice, which is not what I want.

unity3dmultiplayerMatchmaking
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

justin-1 avatar image
justin-1 answered

I figured out what went wrong. I was using this Photon Bolt tutorial with PlayFab Servers as a base.

But you need to make an important change for matchmaking to work. In PlayFabHeadlessServer.cs, you need to remove the following lines in Start():

// Run Bolt Server
OnServerActive();

These lines caused my server to immediately launch instead of waiting for the callback of PlayFab.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

justin-1 avatar image justin-1 commented ·

The session/match ID can then be retrieved after the 'PlayFabMultiplayerAgentAPI.OnServerActiveCallback' event is called with the following code:

string sessionId = PlayFabMultiplayerAgentAPI.GetConfigSettings()[PlayFabMultiplayerAgentAPI.SessionIdKey];

So you don't need to use 'PlayFabMultiplayerAgentAPI.OnSessionConfigUpdateEvent' for this.

1 Like 1 ·
Kaiwen Yu avatar image Kaiwen Yu commented ·

This is super useful. It took me a lot of effort to know the session id is empty. And this is the root cause of it.

0 Likes 0 ·
Citrus Yan avatar image
Citrus Yan answered

If you integrate Matchmaking with PlayFab Multiplayer Servers following this tutorial: Integrating with PlayFab Multiplayer Servers - PlayFab | Microsoft Docs, your server will receive the following information passed by matchmaking:

  • SessionId - The SessionId for the server will be equal to the MatchId for the match.
  • InitialPlayers - This value is set to the list of members in the match. The list of players can be read in the game by using the GSDK.
  • PreferredRegions - This field is set to the RegionPreferences field from the match. The game server service will choose an appropriate region for the server from this list.

Therefore, you should be able to use the GSDK to retrieve such info once the server is launched, here is a sample code showing how to do that in C#:

// Get all the configuration values
IDictionary<string, string> config = GameserverSDK.getConfigSettings();
// Retrieve a particular configuration value
if (config.TryGetValue(GameserverSDK.SessionCookieKey, out string sessionCookie))
{
// sessionCookie contains the value
}

For more details please check out: https://docs.microsoft.com/en-us/gaming/playfab/features/multiplayer/servers/integrating-game-servers-with-gsdk#getting-configuration-settings-within-your-game-server

5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

justin-1 avatar image justin-1 commented ·

Thank you for your response.

I've followed the tutorial. I think I'm not understanding the build configuration properly. In your response, it sounds as if matchmaking is supposed to launch a server once a match is found. So I tried setting the 'Target standby' to 0, and 'Maximum' to 1 in the build config for my region. But then, a server never gets launched. Polling 'GetMatchmakingTicket' every 7 seconds will keep returning 'WaitingForServer' after a couple of polls, until the timeout is finally reached.

If I set the 'Target standby' to 1 in the build config, my server build is immediately launched, even though no match is queued in the matchmaking. 'GetMatchmakingTicket' will then succeed with a match ID, but the server never receives this match ID, since it was already started before the match was made.

So how am I supposed to set up the server build configuration so I can receive a match ID on the server once matchmaking has succeeded? Or am I doing something else wrong?

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan justin-1 commented ·

The "SessionId" passed to the server is equal to the "MatchId" received from "GetMatchmakingTicket". What do you mean by "but the server never receives this match ID", do you mean you cannot get the MatchId (SessionId) using GSDK once matchmaking has succeeded?

0 Likes 0 ·
justin-1 avatar image justin-1 Citrus Yan commented ·

Yeah, that's what I mean. I've tried listening to 'PlayFabMultiplayerAgentAPI.OnServerActiveCallback', but that gets called as soon as the server is launched if 'Standby servers' is set to something above 0, even though matchmaking hasn't created a match yet. Thus, it doesn't have a MatchId.

I'm using this code to try to get the session/match ID:

Debug.Log(JsonConvert.SerializeObject(PlayFabMultiplayerAgentAPI.GetConfigSettings()));

string sessionId = PlayFabMultiplayerAgentAPI.GetConfigSettings()[PlayFabMultiplayerAgentAPI.SessionIdKey];

The log then contains the following JSON:

{
"heartbeatEndpoint": "[...]:56001",
"logFolder": "[...]",
"game_server1": "5056",
"fullyQualifiedDomainName": "[...]",
"game_server2": "27002",
"vmId": "xcloudneu4[...]:NorthEurope:[...]:tvmps_[...]_d",
"publicIpV4Address": "20.[...]",
"bolt_server": "60001",
"buildId": "[...]",
"master_server1": "5055",
"master_server2": "27001",
"region": "NorthEurope",
"serverId": "[...]",
"name_server1": "5058",
"certificateFolder": "[...]",
"sharedContentFolder": "[...]",
"name_server2": "27000",
"titleId": "[...]"
}

So no key for 'sessionId', thus the next line generates a KeyNotFoundException.

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.