From 431f76364694812938695bfa92c14fbfab92210c Mon Sep 17 00:00:00 2001 From: Jacob Gelman <3182119+ladvoc@users.noreply.github.com> Date: Tue, 1 Apr 2025 14:28:05 +1100 Subject: [PATCH 1/2] Add data streams example --- .../Assets/LivekitSamples.cs | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/LivekitUnitySampleApp/Assets/LivekitSamples.cs b/LivekitUnitySampleApp/Assets/LivekitSamples.cs index ab934d6..e03ab4a 100644 --- a/LivekitUnitySampleApp/Assets/LivekitSamples.cs +++ b/LivekitUnitySampleApp/Assets/LivekitSamples.cs @@ -4,6 +4,7 @@ using LiveKit.Proto; using UnityEngine.UI; using RoomOptions = LiveKit.RoomOptions; +using StreamTextOptions = LiveKit.StreamTextOptions; using System.Collections.Generic; using Application = UnityEngine.Application; using TMPro; @@ -96,6 +97,11 @@ IEnumerator MakeCall() var options = new RoomOptions(); var connect = room.Connect(url, token, options); yield return connect; + + room.RegisterTextStreamHandler("my-topic", (data, identity) => + StartCoroutine(HandleTextStream(data, identity)) + ); + if (!connect.IsError) { Debug.Log("Connected to " + room.Name); @@ -309,7 +315,45 @@ public IEnumerator publishVideo() public void publishData() { var str = "hello from unity!"; - room.LocalParticipant.PublishData(System.Text.Encoding.Default.GetBytes(str)); + + // Option 1: Using data streams + StartCoroutine(PerformSendText(str)); + + // Option 2: Using publish data + // PerformPublishData(str); + } + + private IEnumerator PerformSendText(string message) + { + var sendTextCall = room.LocalParticipant.SendText(message, "my-topic"); + yield return sendTextCall; + + if (sendTextCall.IsError) + { + Debug.LogError("Failed to send text: " + sendTextCall.Error); + yield break; + } + Debug.Log("Text sent successfully"); + } + + private void PerformPublishData(string message) + { + var enc = System.Text.Encoding.Default.GetBytes(message); + room.LocalParticipant.PublishData(enc); + } + + private IEnumerator HandleTextStream(TextStreamReader reader, string identity) + { + var readAllCall = reader.ReadAll(); + yield return readAllCall; + + if (readAllCall.IsError) + { + Debug.LogError("Failed to read stream: " + readAllCall.Error); + yield break; + } + var message = readAllCall.Text; + Debug.Log($"Received message from {identity}: {message}"); } public IEnumerator OpenCamera() From 694e4f8e28f5c42b350b89d2a078cb505d34740a Mon Sep 17 00:00:00 2001 From: Jacob Gelman <3182119+ladvoc@users.noreply.github.com> Date: Sat, 5 Apr 2025 13:46:44 +1100 Subject: [PATCH 2/2] Register stream handler before connect --- LivekitUnitySampleApp/Assets/LivekitSamples.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/LivekitUnitySampleApp/Assets/LivekitSamples.cs b/LivekitUnitySampleApp/Assets/LivekitSamples.cs index e03ab4a..9d006c8 100644 --- a/LivekitUnitySampleApp/Assets/LivekitSamples.cs +++ b/LivekitUnitySampleApp/Assets/LivekitSamples.cs @@ -98,10 +98,6 @@ IEnumerator MakeCall() var connect = room.Connect(url, token, options); yield return connect; - room.RegisterTextStreamHandler("my-topic", (data, identity) => - StartCoroutine(HandleTextStream(data, identity)) - ); - if (!connect.IsError) { Debug.Log("Connected to " + room.Name);