Developer Documentation

OdinHandler.OnMessageReceived

public MessageReceivedProxy OnMessageReceived

Called on every Peer that received message from a peer by UInt64%5b%5d%2cSystem

Value Type

TypeDescription
MessageReceivedProxy

Example

OnMessageReceived 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);
	}
}