MatchMaker offers a utility function to help you start P2P communication with matched users.
MatchMaker and TeamMatchMaker class have a method to exchange client addresses to start P2P communication.
The example below uses MatchMaker class.
// This list keeps track of all P2P class instances
List<P2P> peers = new List<P2P>();
// This event callback receives all matched clients' addresses
matchmaker.OnP2PAddressSync += (string[] addresses) =>
{
for (int i = 0; i < addresses.Length; i++)
{
string address = addresses[i];
Diarkis.Modules.P2P peerClient = new Diarkis.Modules.P2P();
peerClient.OnReady += OnP2PReady;
peerClient.OnFail += OnP2PFail;
peerClient.Connect(address);
lock(peers)
{
peers.Add(peerClient);
}
}
};
private void OnP2PReady(string peerAddress, int peerPort, double holePunchTimeInMilliseconds)
{
// P2P is now ready!
}
private void OnP2PFail(string peerAddress, int peerPort)
{
// P2P has failed...
}
// For Unity Game Engine...
void Update()
{
// We must call Update of each P2P class instances
lock(peers)
{
for (int i = 0; i < peers.Count; i++)
{
peers[i].Update();
}
}
}
// Calling this triggers OnP2PAddressSync event on all matched user clients
matchmaker.P2PAddressSync();