Transports & Samples
After managing your players with either a matchmaker or lobbies then creating a relay session for them using the Edgegap API, you will need to connect your players to the relay once it is ready. Edgegap offers a specific transport to do just that, and this guide will show you how to implement it in your project. The transport is available here on our GitHub.
With Unity, there currently is a version of the transport for the following netcodes:
- Mirror;
- Fishnet;
- Unity Netcode for GameObjects (NGO).
Adding the transport
After downloading the right transport for your netcode, you need to include it in your project. First, add it to your project files in the following location:
- Mirror : add the
Edgegap
folder underAssets/Mirror/Transports
; - Fishnet : add the
Edgegap KcpTransport
folder underAssets/Fishnet/Plugins
; - NGO : add the
EdgegapRelay
folder underAssets/Edgegap
.
Then, add the EdgegapKcpTransport
script to your NetworkManager
gameObject, make sure to drag that new component in the Transport
property of the gameObject as well.
If the option is present, make sure to disable Relay GUI
in the EdgegapKcpTransport
component of the NetworkManager
. Otherwise, it will interfere when attempting to connect to the relay.
This option is namely present in the Mirror and NGO versions of the transport.
Connecting to the relay
After matchmaking and creating the relay session, use the Edgegap API to determine when the relay is ready to accept connections. Once it is, use the data provided in the API response to set the values of the transport. You will need the following values in the appropriate fields:
- the relay's
IP
address is used as thetransport's relay address
; - the relay's
client port
value is used as thetransport's relay port
when connecting aclient
; - the relay's
server port
value is used as thetransport's relay port
when connecting theserver/host
; - the
session authorization token
is used as thetransport's session ID
; - the
user's authorization token
is used as thetransport's user ID
.
Each player will have their own unique authorization token, but the session token will remain the same for each player in the session.
- Mirror
- Fishnet
- NGO
- ApiResponse class
// `data` is the deserialized API response converted to JSON
// `_EdgegapTransport` is the EdgegapKcpTransport
// Convert uint? to uint
uint sessionAuthorizationToken = data.authorization_token ?? 0;
//TODO find which session user matches the player, `i` being its place in the list
uint userAuthorizationToken = data.session_users?[i].authorization_token ?? 0;
_EdgegapTransport.ChangeValue(
data.relay.ip,
data.relay.ports.client.port,
data.relay.ports.server.port,
data.session_id,
sessionAuthorizationToken,
userAuthorizationToken
);
// then `NetworkManager.Singleton.StartHost();` if host player
// OR `NetworkManager.Singleton.StartClient();` if client
// `data` is the deserialized API response converted to JSON
// `_transport` is the EdgegapKcpTransport
// Convert uint? to uint
uint sessionAuthorizationToken = data.authorization_token ?? 0;
//TODO find which session user matches the player, `i` being its place in the list
uint userAuthorizationToken = data.session_users?[i].authorization_token ?? 0;
Relay relay = data.relay;
string address = relay.ip;
ushort serverPort = relay.ports.server.port;
ushort clientPort = relay.ports.client.port;
var relayData = new EdgegapRelayData(
address,
serverPort,
clientPort,
userAuthorizationToken,
sessionAuthorizationToken
);
_transport.SetEdgegapRelayData(relayData);
// then `_transport.StartConnection(true); _transport.StartConnection(false);` if host
// OR `_transport.StartConnection(false);` if client
// `data` is the deserialized API response converted to JSON
// `_EdgegapTransport` is the EdgegapKcpTransport
// Convert uint? to uint
uint sessionAuthorizationToken = data.authorization_token ?? 0;
//TODO find which session user matches the player, `i` being its place in the list
uint userAuthorizationToken = data.session_users?[i].authorization_token ?? 0;
_EdgegapTransport.relayAddress = data.relay.ip;
_EdgegapTransport.relayGameClientPort = data.relay.ports.client.port;
_EdgegapTransport.relayGameServerPort = data.relay.ports.server.port;
_EdgegapTransport.sessionId = sessionAuthorizationToken;
_EdgegapTransport.userId = userAuthorizationToken;
// then `NetworkManager.Singleton.StartHost();` if host player
// OR `NetworkManager.Singleton.StartClient();` if client
public class ApiResponse
{
public string session_id { get; set; }
public uint? authorization_token { get; set; }
public string status { get; set; }
public bool ready { get; set; }
public bool linked { get; set; }
public object? error { get; set; }
public List<SessionUser>? session_users { get; set; }
public Relay relay { get; set; }
public object? webhook_url { get; set; }
}
Once the values are properly set, use the transport to connect each player to the relay. After a short moment, you will be able to play the game!
Sample projects
The following projects are simple examples using the Edgegap relay transport. For them to work, change the RelayProfileToken
value in the HelloWorldManager
component of the NetworkManager
gameObject to your own token.