docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Delegate DelegateOnDataChannel

    Represents the method that will be invoked when a new data channel is added to the RTCPeerConnection.

    Namespace: Unity.WebRTC
    Assembly: Unity.WebRTC.dll
    Syntax
    public delegate void DelegateOnDataChannel(RTCDataChannel channel)
    Parameters
    Type Name Description
    RTCDataChannel channel

    The RTCDataChannel that has been added to the connection.

    Remarks

    The DelegateOnDataChannel is triggered when a new data channel is established, typically as a result of the remote peer creating a channel. This provides an opportunity to configure the new channel, such as setting message handlers or adjusting properties. Ensuring the application is prepared to handle the new data channel is crucial for seamless peer-to-peer communication. This delegate is typically assigned to the OnDataChannel property.

    Examples
    using UnityEngine;
    using Unity.WebRTC;
    
    public class DataChannelHandlerExample : MonoBehaviour
    {
        private void Start()
        {
            var peerConnection = new RTCPeerConnection();
    
            // Assign the delegate to handle the OnDataChannel event
            peerConnection.OnDataChannel = (channel) =>
            {
                Debug.Log("A new data channel has been added: " + channel.Label);
                channel.OnMessage = (bytes) =>
                {
                    string message = System.Text.Encoding.UTF8.GetString(bytes);
                    Debug.Log("Received message on new channel: " + message);
                };
            };
        }
    }

    See Also

    OnDataChannel
    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)