Sending/receiving messages

Once connected to a room, we have a connection that can be used to send and receive messages. Let's register event handlers for receiving messages, as well as disconnections.

First, we'll add methods to handle these events:

// called when we've disconnected from the room
void OnDisconnect( object sender, string message )
{
  Debug.Log( "Disconnected from room" );
}
// called when a message is received
void OnMessage( object sender, Message e )
{
}

Then we'll modify our OnGUI method to hook up these event handlers upon connection:

void OnGUI() { if( roomConnection != null ) return; if( GUILayout.Button( "Join Random", GUILayout.Width( 200f ) ) ) { client.Multiplayer.CreateJoinRoom( "$service-room$", "MyCode", true, null, null, ...

Get Unity Multiplayer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.