Developer Documentation
Our new developer documentation is now available. Please check it out!

MessageReceivedProxy

[Serializable]
public class MessageReceivedProxy : UnityEvent<object, MessageReceivedEventArgs>

This class provides the base functionality for UnityEvents based Room. A persistent callback that can be saved with the Scene. Unity Inspector event wrapper (UnityEvent)

Discussion

The callback proxy for OnMessageReceived callback functions. You’ll receive the sender of the message in the object parameter and details about the message in a MessageReceivedEventArgs class instance.

Example

MessageReceivedProxy Example
class Message
{
	public string text;
	public string type;
}

class MyMessageHandler : MonoBehaviour
{
	public void OnMessageReceived(object sender, MessageReceivedEventArgs args)
	{
		// Get the peer id
		ulong peerId = args.PeerId;

		// Get the data
		byte[] bytes = args.Data;

		// Serialize data in your own classes (just an example, add error handling)
		var bytesAsString = Encoding.UTF8.GetString(bytes);
        var message = JsonConvert.DeserializeObject<Message>(bytesAsString);

        // Show a text chat message
        if (message.type == "chat") {
            UI.ShowIncomingChatMessage(message.text);
        }
	}

	public void SendChatMessage(Room room, string chatMessage)
	{
		var message = new Message{
			type = "chat";
			text = chatMessage;
		}
		var stringMessage = JsonConvert.SerializeObject(message, Formatting.None);
        var bytes = Encoding.UTF8.GetBytes(stringMessage);
		room.SendMessage(bytes);
	}
}