Skip to content

Demonstrate data streams #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion LivekitUnitySampleApp/Assets/LivekitSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,6 +97,7 @@ IEnumerator MakeCall()
var options = new RoomOptions();
var connect = room.Connect(url, token, options);
yield return connect;

if (!connect.IsError)
{
Debug.Log("Connected to " + room.Name);
Expand Down Expand Up @@ -309,7 +311,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code seems to have been removed in the last commit and forgotten to be added before room.connect?

	room.RegisterTextStreamHandler("my-topic", (data, identity) =>
                StartCoroutine(HandleTextStream(data, 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()
Expand Down