// We need to delay slightly to give the disconnect message sent during Dispose time to reach the host, so that we don't destroy the connection without it being flushed first.
/// An abstraction layer between the direct calls into the Lobby API and the outcomes you actually want. E.g. you can request to get a readable list of
/// An abstraction layer between the direct calls into the Lobby API and the outcomes you actually want. E.g. you can request to get a readable list of
/// current lobbies and not need to make the query call directly.
privateboolm_isHandlingPending=false;// Just in case a pending operation tries to enqueue itself again.
if(!m_isHandlingPending)
m_pendingOperations.Enqueue(action);
m_pendingOperations.Enqueue(action);
}
privateboolm_isInCooldown=false;
privatevoidOnUpdate(floatdt)
{
m_timeSinceLastCall+=dt;
m_isHandlingPending=false;// (Backup in case a pending operation hit an exception.)
if(m_timeSinceLastCall>=m_cooldownTime)
{
IsInCooldown=false;
m_isHandlingPending=true;
while(m_pendingOperations.Count>0)
intnumPending=m_pendingOperations.Count;// It's possible a pending operation will re-enqueue itself or new operations, which should wait until the next loop.
/// This observes the local player and updates remote players over Relay when there are local changes, demonstrating basic data transfer over the Unity Transport (UTP).
/// Created after the connection to Relay has been confirmed.
/// </summary>
publicclassRelayUtpClient:MonoBehaviour// This is a MonoBehaviour merely to have access to Update.
publicclassRelayUtpClient:MonoBehaviour,IDisposable// This is a MonoBehaviour merely to have access to Update.
// Don't clean up the NetworkDriver here, or else our disconnect message won't get through to the host. The host will handle cleaning up the connection.
}
publicvoidDispose()
{
if(!m_hasDisposed)
{
Uninitialize();
m_hasDisposed=true;
}
Uninitialize();
Dispose();
}
privatevoidOnLocalChange(LobbyUserlocalUser)
// The host disconnected, and Relay does not support host migration. So, all clients should disconnect.
stringmsg;
if(m_IsRelayConnected)
msg="Host disconnected! Leaving the lobby.";
msg="The host disconnected! Leaving the lobby.";
else
msg="Connection to host was lost. Leaving the lobby.";
elseif(msgType==MsgType.PlayerDisconnect)// Clients message the host when they intend to disconnect, or else the host ends up keeping the connection open.
// The user ready status lives in the lobby data, which won't update immediately, but we need to use it to identify if all remaining players have readied.
// So, we'll wait two lobby update loops before we check remaining players to ensure the lobby has received the disconnect message.
// We rely on the PlayerDisconnect message instead of this disconnect message since this message might not arrive for a long time after the disconnect actually occurs.