docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Delegate DelegateOnMessage

    Represents the method that will be invoked when a message is received from the remote peer over the data channel.

    Namespace: Unity.WebRTC
    Assembly: Unity.WebRTC.dll
    Syntax
    public delegate void DelegateOnMessage(byte[] bytes)
    Parameters
    Type Name Description
    byte[] bytes

    The message received as a byte array.

    Remarks

    The DelegateOnMessage is executed when the data channel successfully receives a message from the remote peer, providing the message content as a parameter. This allows the application to process the incoming data, whether it's for updating the UI, triggering gameplay logic, or handling any response actions. The method receives the message as a byte array, making it flexible for both textual and binary data. This delegate is typically assigned to the OnMessage property.

    Examples
    using UnityEngine;
    using Unity.WebRTC;
    
    public class DataChannelMessageExample : MonoBehaviour
    {
        private void Start()
        {
            var initOption = new RTCDataChannelInit();
            var peerConnection = new RTCPeerConnection();
            var dataChannel = peerConnection.CreateDataChannel("test channel", initOption);
    
            // Assign the delegate to handle the OnMessage event
            dataChannel.OnMessage = (bytes) =>
            {
                string message = System.Text.Encoding.UTF8.GetString(bytes);
                Debug.Log("Received message: " + message);
            };
        }
    }

    See Also

    OnMessage
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)