Hello everyone ,
I am connecting with Unity render streaming. I am creating a channel for sending data. The channel status appears as open and inputReceiver.Channel.Send(bytes); I send the message data with the command or I think it is sent. From the other side dataChannel.OnMessage += OnMessage; I am waiting for the message content to arrive with the connected event, but no message is received. I need help creating a channel and sending messages, please help me.
using UnityEngine;
using Unity.WebRTC;
using TMPro;
using Unity.RenderStreaming;
using System.Collections;
public class DataChannelTest : MonoBehaviour
{
#region Veriables
[SerializeField] private TextMeshProUGUI receivedMessage;
[SerializeField] private SignalingManager renderStreaming;
[SerializeField] private InputSender inputSender;
[SerializeField] private InputReceiver inputReceiver;
private RTCDataChannel dataChannel;
#endregion
public void OnStart()
{
dataChannel = renderStreaming.CreateDataChannel();
SetupDataChannelEvents();
}
private void SetupDataChannelEvents()
{
if (dataChannel != null)
dataChannel.OnMessage += OnMessage;
inputSender.SetChannel(renderStreaming.connectionId, dataChannel);
inputReceiver.SetChannel(renderStreaming.connectionId, dataChannel);
}
public void SendData()
{
if (dataChannel != null && dataChannel.ReadyState == RTCDataChannelState.Open)
{
string message = "Hello world.";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(message);
inputReceiver.Channel.Send(bytes);
}
else
Debug.Log("Data channel is not open.");
}
private void OnMessage(byte[] bytes)
{
string message = System.Text.Encoding.UTF8.GetString(bytes);
receivedMessage.text = message;
Debug.Log("Received message: " + message);
}
void OnDestroy()
{
if (dataChannel != null)
dataChannel.Close();
}
}
This code looks strange.
One channel is referenced by the sender and the receiver. You should set two channels to the sender and the receiver separately.
In Addition, you don’t need to use the InputSender and InputReceiver to send custom messages. You can use RTCDataChannel class directly. https://docs.unity3d.com/Packages/com.unity.webrtc@3.0/manual/datachannel.html
I am getting the stream as usual on the web so i guess the negotiation process is completed, can you tell me how do i get access to RTCPeerConnection obect to setup a custom data channel ? I am currently using multiplay sample of render streaming ?