Skip to content

Commit e406c92

Browse files
committed
Add further support for cancellation tokens
1 parent ce3a8eb commit e406c92

File tree

8 files changed

+21
-9
lines changed

8 files changed

+21
-9
lines changed

Packages/StreamVideo/Runtime/Core/IStreamVideoClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public interface IStreamVideoClient : IStreamVideoClientEventsListener, IDisposa
6868
/// </summary>
6969
/// <param name="credentials">Credentials required to connect user: api_key, user_id, and user_token</param>
7070
Task<IStreamVideoUser> ConnectUserAsync(AuthCredentials credentials);
71+
72+
Task<IStreamVideoUser> ConnectUserAsync(AuthCredentials credentials, CancellationToken cancellationToken);
7173

7274
/// <summary>
7375
/// Disconnect user from Stream server.

Packages/StreamVideo/Runtime/Core/LowLevelClient/WebSockets/BasePersistentWebSocket.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ internal abstract class BasePersistentWebSocket : IPersistentWebSocket
2222
public event Action Connected;
2323
public event Action Disconnected;
2424

25+
public const int ConnectTimeoutMs = 1000;
26+
2527
public ConnectionState ConnectionState
2628
{
2729
get => _connectionState;
@@ -78,7 +80,9 @@ private void TryToReconnect()
7880
Logs.Info($"{GetType()} TryToReconnect");
7981
#endif
8082

81-
ConnectAsync().LogIfFailed();
83+
var cts = new CancellationTokenSource();
84+
cts.CancelAfter(ConnectTimeoutMs);
85+
ConnectAsync(cts.Token).LogIfFailed();
8286
}
8387

8488
public Task ConnectAsync(CancellationToken cancellationToken = default)

Packages/StreamVideo/Runtime/Core/LowLevelClient/WebSockets/CoordinatorWebSocket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected override async Task ExecuteConnectAsync(CancellationToken cancellation
7373
var uri = UriFactory.CreateCoordinatorConnectionUri();
7474

7575
//StreamTodo: Add cancel token support to WS
76-
await WebsocketClient.ConnectAsync(uri);
76+
await WebsocketClient.ConnectAsync(uri, cancellationToken);
7777

7878
#if STREAM_DEBUG_ENABLED
7979
Logs.Info("Coordinator connected! Let's send the connect message");

Packages/StreamVideo/Runtime/Core/LowLevelClient/WebSockets/SfuWebSocket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected override async Task ExecuteConnectAsync(CancellationToken cancellation
179179

180180
var sfuUri = UriFactory.CreateSfuConnectionUri(_sfuUrl);
181181

182-
await WebsocketClient.ConnectAsync(sfuUri);
182+
await WebsocketClient.ConnectAsync(sfuUri, cancellationToken);
183183

184184
//StreamTodo: review when is the actual "connected state" - perhaps not the WS connection itself but receiving an appropriate event should set the flag
185185
//e.g. are we able to send any data as soon as the connection is established?

Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,12 @@ public void Dispose()
221221
//StreamTodo: Consider removing this overload and exposing ConnectAsync() DisconnectAsync() only. The config would contain credentials (token or token provider), etc.
222222
//Similar to Android SDK: https://getstream.io/video/docs/android/guides/client-auth/
223223

224-
public async Task<IStreamVideoUser> ConnectUserAsync(AuthCredentials credentials)
224+
public Task<IStreamVideoUser> ConnectUserAsync(AuthCredentials credentials)
225+
=> ConnectUserAsync(credentials, CancellationToken.None);
226+
227+
public async Task<IStreamVideoUser> ConnectUserAsync(AuthCredentials credentials, CancellationToken cancellationToken)
225228
{
226-
await InternalLowLevelClient.ConnectUserAsync(credentials);
229+
await InternalLowLevelClient.ConnectUserAsync(credentials, cancellationToken);
227230

228231
#if STREAM_DEBUG_ENABLED
229232
_logs.Warning("StreamVideoClient - CONNECTION - Trigger Connected event.");

Packages/StreamVideo/Runtime/Libs/Websockets/IWebsocketClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Net.WebSockets;
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
namespace StreamVideo.Libs.Websockets
@@ -18,7 +19,7 @@ public interface IWebsocketClient : IDisposable
1819

1920
bool TryDequeueMessage(out byte[] message);
2021

21-
Task ConnectAsync(Uri serverUri);
22+
Task ConnectAsync(Uri serverUri, CancellationToken cancellationToken);
2223

2324
void Update();
2425

Packages/StreamVideo/Runtime/Libs/Websockets/NativeWebSocketWrapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Net.WebSockets;
44
using System.Text;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using NativeWebSocket;
78
using StreamVideo.Libs.Utils;
@@ -37,7 +38,7 @@ public bool TryDequeueMessage(out byte[] message)
3738
return message != null;
3839
}
3940

40-
public async Task ConnectAsync(Uri serverUri)
41+
public async Task ConnectAsync(Uri serverUri, CancellationToken cancellationToken)
4142
{
4243
if (_webSocket != null)
4344
{

Packages/StreamVideo/Runtime/Libs/Websockets/WebsocketClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public WebsocketClient(ILogs logs, Encoding encoding = default, bool isDebugMode
3838

3939
public bool TryDequeueMessage(out byte[] message) => _receiveQueue.TryDequeue(out message);
4040

41-
public async Task ConnectAsync(Uri serverUri)
41+
public async Task ConnectAsync(Uri serverUri, CancellationToken cancellationToken)
4242
{
4343
if (IsConnected || IsConnecting)
4444
{
@@ -52,7 +52,8 @@ public async Task ConnectAsync(Uri serverUri)
5252
{
5353
await TryDisposeResourcesAsync(WebSocketCloseStatus.NormalClosure,
5454
"Clean up resources before connecting");
55-
_connectionCts = new CancellationTokenSource();
55+
cancellationToken.ThrowIfCancellationRequested();
56+
_connectionCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
5657

5758
_internalClient = new ClientWebSocket();
5859
_internalClient.Options.SetRequestHeader("User-Agent", "unity-video-sdk-ws-client");

0 commit comments

Comments
 (0)