Skip to content

Commit fc53dcb

Browse files
authored
Merge pull request #198 from GetStream/bugfix/uni-147-fix-sfu-ws-reconnecting-when-coordinator-looses-connection
Bugfix/uni 147 fix sfu ws reconnecting when coordinator looses connection
2 parents d0228bf + 8ff2614 commit fc53dcb

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

Packages/StreamVideo/Runtime/Core/LowLevelClient/ReconnectScheduler.cs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ internal interface IReconnectScheduler
1010
event Action ReconnectionScheduled;
1111

1212
void Reset();
13+
14+
void SetTarget(IReconnectTarget target);
15+
}
16+
17+
internal interface IReconnectTarget
18+
{
19+
event ConnectionStateChangeHandler ConnectionStateChanged;
20+
ConnectionState ConnectionState { get; }
1321
}
22+
1423
/// <summary>
1524
/// Schedules next reconnection time based on the past attempts and network availability
1625
/// </summary>
@@ -44,29 +53,26 @@ private set
4453
}
4554
}
4655

47-
public ReconnectScheduler(ITimeService timeService, IStreamVideoLowLevelClient lowLevelClient,
48-
INetworkMonitor networkMonitor, Func<bool> shouldReconnect)
56+
public ReconnectScheduler(ITimeService timeService, INetworkMonitor networkMonitor, Func<bool> shouldReconnect)
4957
{
5058
_shouldReconnect = shouldReconnect ?? throw new ArgumentNullException(nameof(shouldReconnect));
51-
_client = lowLevelClient ?? throw new ArgumentNullException(nameof(lowLevelClient));
5259
_timeService = timeService ?? throw new ArgumentNullException(nameof(timeService));
5360
_networkMonitor = networkMonitor ?? throw new ArgumentNullException(nameof(networkMonitor));
5461

5562
_networkMonitor.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
63+
}
5664

57-
_client.Connected += OnConnected;
58-
//_client.Reconnecting += OnReconnecting;
59-
_client.ConnectionStateChanged += OnConnectionStateChanged;
65+
//StreamTODO: refactor so that each WS instance creates the reconnector internally and injects its instance
66+
public void SetTarget(IReconnectTarget target)
67+
{
68+
UnsubscribeFromTarget();
69+
_target = target ?? throw new ArgumentNullException(nameof(target));
70+
SubscribeToTarget();
6071
}
6172

6273
public void Dispose()
6374
{
64-
if (_client != null)
65-
{
66-
_client.Connected -= OnConnected;
67-
//_client.Reconnecting -= OnReconnecting;
68-
_client.ConnectionStateChanged -= OnConnectionStateChanged;
69-
}
75+
UnsubscribeFromTarget();
7076
}
7177

7278
public void SetReconnectStrategySettings(ReconnectStrategy reconnectStrategy, float? exponentialMinInterval,
@@ -114,14 +120,35 @@ public void Stop()
114120
}
115121

116122
//StreamTodo: connection info could be split to separate interface
117-
private readonly IStreamVideoLowLevelClient _client;
118123
private readonly ITimeService _timeService;
119124
private readonly INetworkMonitor _networkMonitor;
120125

121126
private int _reconnectAttempts;
122127
private bool _isStopped;
123128
private double? _nextReconnectTime;
124129
private Func<bool> _shouldReconnect;
130+
131+
private IReconnectTarget _target;
132+
133+
private void SubscribeToTarget()
134+
{
135+
if (_target == null)
136+
{
137+
return;
138+
}
139+
140+
_target.ConnectionStateChanged += OnConnectionStateChanged;
141+
}
142+
143+
private void UnsubscribeFromTarget()
144+
{
145+
if (_target == null)
146+
{
147+
return;
148+
}
149+
150+
_target.ConnectionStateChanged -= OnConnectionStateChanged;
151+
}
125152

126153
private void TryScheduleNextReconnectTime()
127154
{
@@ -198,8 +225,8 @@ private void OnNetworkAvailabilityChanged(bool isNetworkAvailable)
198225
return;
199226
}
200227

201-
if (_client.ConnectionState == ConnectionState.Connected ||
202-
_client.ConnectionState == ConnectionState.Connecting)
228+
if (_target.ConnectionState == ConnectionState.Connected ||
229+
_target.ConnectionState == ConnectionState.Connecting)
203230
{
204231
return;
205232
}

Packages/StreamVideo/Runtime/Core/LowLevelClient/StreamVideoLowLevelClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public StreamVideoLowLevelClient(IWebsocketClient coordinatorWebSocket, IWebsock
143143
}
144144

145145
//StreamTodo: move to factory
146-
var coordinatorReconnect = new ReconnectScheduler(_timeService, this, _networkMonitor, shouldReconnect: () => true);
147-
var sfuReconnect = new ReconnectScheduler(_timeService, this, _networkMonitor, shouldReconnect: () => RtcSession.ShouldSfuAttemptToReconnect());
146+
var coordinatorReconnect = new ReconnectScheduler(_timeService, _networkMonitor, shouldReconnect: () => true);
147+
var sfuReconnect = new ReconnectScheduler(_timeService, _networkMonitor, shouldReconnect: () => RtcSession.ShouldSfuAttemptToReconnect());
148148

149149
//StreamTodo: move to factory
150150
_coordinatorWS = new CoordinatorWebSocket(coordinatorWebSocket, coordinatorReconnect, authProvider: this,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace StreamVideo.Core.LowLevelClient.WebSockets
1818
{
19-
internal abstract class BasePersistentWebSocket : IPersistentWebSocket
19+
internal abstract class BasePersistentWebSocket : IPersistentWebSocket, IReconnectTarget
2020
{
2121
public event ConnectionStateChangeHandler ConnectionStateChanged;
2222
public event Action Connected;
@@ -207,6 +207,8 @@ protected BasePersistentWebSocket(IWebsocketClient websocketClient, IReconnectSc
207207
Logs = logs;
208208
WebsocketClient = websocketClient ?? throw new ArgumentNullException(nameof(websocketClient));
209209

210+
_reconnectScheduler.SetTarget(this);
211+
210212
WebsocketClient.ConnectionFailed += OnConnectionFailed;
211213
WebsocketClient.Disconnected += OnDisconnected;
212214

0 commit comments

Comments
 (0)