Fixing a bug where clients in builds would not receive the initial player state upon relay connection. There are also some nitpicks with the tests in here.
/// This will handle observing the local player and updating remote players over Relay when there are local changes.
/// 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.
{
MsgTypemsgType=(MsgType)strm.ReadByte();
stringid=ReadLengthAndString(refstrm);
if(id==m_localUser.ID||!m_localLobby.LobbyUsers.ContainsKey(id))// TODO: Do we want to hold onto the message if the user isn't present *now* in case they're pending?
if(id==m_localUser.ID||!m_localLobby.LobbyUsers.ContainsKey(id))// We don't hold onto messages, since an incoming user will be fully initialized before they send events.
/// When a new client connects, they need to be given all up-to-date info.
/// When a new client connects, they need to be updated with the current state of everyone else.
// When a new client connects, they need to be updated with the current state of everyone else.
// (We can't exclude this client from the events we send to it, since we don't have its ID in strm, but it will ignore messages about itself on arrival.)
foreach(varuserinm_localLobby.LobbyUsers)
foreach(varuserinm_localLobby.LobbyUsers)// The host includes itself here since we don't necessarily have an ID available, but it will ignore its own messages on arrival.
elseif(msgType==MsgType.NewPlayer)// This ensures clients in builds are sent player state once they establish that they can send (and receive) events.
OnNewConnection(conn);
// If a client has changed state, check if this changes whether all players have readied.
if(msgType==MsgType.ReadyState)
if(!conn.IsCreated)// "Nothing more to accept" is signalled by returning an invalid connection from Accept.
break;
m_connections.Add(conn);
OnNewConnection(conn);
OnNewConnection(conn);// This ensures that clients in editors are sent player state once they establish a connection. The timing differs slightly from builds.