-
Notifications
You must be signed in to change notification settings - Fork 12
/
RealtimeSocket.cs
501 lines (422 loc) · 16.1 KB
/
RealtimeSocket.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Supabase.Realtime.Socket;
using Supabase.Realtime.Exceptions;
using Supabase.Realtime.Interfaces;
using Websocket.Client;
using static Supabase.Realtime.Constants;
#if NETSTANDARD2_0
using Websocket.Client.Models;
#endif
namespace Supabase.Realtime;
/// <summary>
/// Socket connection handler.
/// </summary>
public class RealtimeSocket : IDisposable, IRealtimeSocket
{
/// <summary>
/// Returns whether or not the connection is alive.
/// </summary>
public bool IsConnected => _connection.IsRunning;
/// <summary>
/// The Socket Endpoint
/// </summary>
private string EndpointUrl
{
get
{
var parameters = new Dictionary<string, string?>
{
{ "token", _options.Parameters.Token },
{ "apikey", _options.Parameters.ApiKey },
{ "vsn", "1.0.0" }
};
return string.Format($"{_endpoint}?{Utils.QueryString(parameters)}");
}
}
private readonly List<IRealtimeSocket.StateEventHandler> _socketEventHandlers = new();
private readonly List<IRealtimeSocket.MessageEventHandler> _messageEventHandlers = new();
private readonly List<IRealtimeSocket.HeartbeatEventHandler> _heartbeatEventHandlers = new();
private readonly List<IRealtimeSocket.ErrorEventHandler> _errorEventHandlers = new();
private readonly string _endpoint;
private readonly ClientOptions _options;
private readonly WebsocketClient _connection;
private CancellationTokenSource? _heartbeatTokenSource;
private bool _hasSuccessfullyConnectedOnce;
private bool _hasPendingHeartbeat;
private string? _pendingHeartbeatRef;
private readonly List<Task> _buffer = new();
private bool _isReconnecting;
private int _reconnectionAttempts = 0;
/// <summary>
/// Initializes this Socket instance.
/// </summary>
/// <param name="endpoint"></param>
/// <param name="options"></param>
public RealtimeSocket(string endpoint, ClientOptions options)
{
_endpoint = $"{endpoint}/{TransportWebsocket}";
_options = options;
if (!options.Headers.ContainsKey("X-Client-Info"))
options.Headers.Add("X-Client-Info", Core.Util.GetAssemblyVersion(typeof(Client)));
_connection = new WebsocketClient(new Uri(EndpointUrl));
}
void IDisposable.Dispose()
{
_heartbeatTokenSource?.Cancel();
DisposeConnection();
}
/// <summary>
/// Connects to a socket server and registers event listeners.
/// </summary>
public async Task Connect()
{
if (_connection.IsRunning) return;
_connection.ReconnectTimeout = _options.ReconnectAfterInterval(_reconnectionAttempts);
_connection.ErrorReconnectTimeout = TimeSpan.FromSeconds(30);
_connection.ReconnectionHappened.Subscribe(HandleSocketReconnectionHappened);
_connection.DisconnectionHappened.Subscribe(HandleSocketDisconnectionHappened);
_connection.MessageReceived.Subscribe(HandleSocketMessage);
await _connection.StartOrFail();
}
/// <summary>
/// Disconnects from the socket server.
/// </summary>
/// <param name="code"></param>
/// <param name="reason"></param>
public void Disconnect(WebSocketCloseStatus code = WebSocketCloseStatus.NormalClosure, string reason = "")
{
_heartbeatTokenSource?.Cancel();
_connection.Stop(code, reason);
}
#region Event Listeners
/// <summary>
/// Adds a listener to be notified when the socket state changes.
/// </summary>
/// <param name="handler"></param>
public void AddStateChangedHandler(IRealtimeSocket.StateEventHandler handler)
{
if (!_socketEventHandlers.Contains(handler))
_socketEventHandlers.Add(handler);
}
/// <summary>
/// Removes a specified listener from socket state changes.
/// </summary>
/// <param name="handler"></param>
public void RemoveStateChangedHandler(IRealtimeSocket.StateEventHandler handler)
{
if (_socketEventHandlers.Contains(handler))
_socketEventHandlers.Remove(handler);
}
/// <summary>
/// Notifies all listeners that the socket state has changed.
/// </summary>
/// <param name="newState"></param>
private void NotifySocketStateChange(SocketState newState)
{
if (!_socketEventHandlers.Any()) return;
Debugger.Instance.Log(this, $"Socket State Change: {newState}");
foreach (var handler in _socketEventHandlers.ToArray())
handler.Invoke(this, newState);
}
/// <summary>
/// Clears all of the listeners from receiving event state changes.
/// </summary>
public void ClearStateChangedHandlers() =>
_socketEventHandlers.Clear();
/// <summary>
/// Adds a listener to be notified when a message is received.
/// </summary>
/// <param name="handler"></param>
public void AddMessageReceivedHandler(IRealtimeSocket.MessageEventHandler handler)
{
if (_messageEventHandlers.Contains(handler))
return;
_messageEventHandlers.Add(handler);
}
/// <summary>
/// Removes a specified listener from messages received.
/// </summary>
/// <param name="handler"></param>
public void RemoveMessageReceivedHandler(IRealtimeSocket.MessageEventHandler handler)
{
if (!_messageEventHandlers.Contains(handler))
return;
_messageEventHandlers.Remove(handler);
}
/// <summary>
/// Notifies all listeners that the socket has received a message
/// </summary>
/// <param name="heartbeat"></param>
private void NotifyMessageReceived(SocketResponse heartbeat)
{
foreach (var handler in _messageEventHandlers.ToArray())
handler.Invoke(this, heartbeat);
}
/// <summary>
/// Clears all of the listeners from receiving event state changes.
/// </summary>
public void ClearMessageReceivedHandlers() =>
_messageEventHandlers.Clear();
/// <summary>
/// Adds a listener to be notified when a message is received.
/// </summary>
/// <param name="handler"></param>
public void AddHeartbeatHandler(IRealtimeSocket.HeartbeatEventHandler handler)
{
if (!_heartbeatEventHandlers.Contains(handler))
_heartbeatEventHandlers.Add(handler);
}
/// <summary>
/// Removes a specified listener from messages received.
/// </summary>
/// <param name="handler"></param>
public void RemoveHeartbeatHandler(IRealtimeSocket.HeartbeatEventHandler handler)
{
if (_heartbeatEventHandlers.Contains(handler))
_heartbeatEventHandlers.Remove(handler);
}
/// <summary>
/// Notifies all listeners that the socket has received a heartbeat
/// </summary>
/// <param name="heartbeat"></param>
private void NotifyHeartbeatReceived(SocketResponse heartbeat)
{
foreach (var handler in _heartbeatEventHandlers.ToArray())
handler.Invoke(this, heartbeat);
}
/// <summary>
/// Clears all of the listeners from receiving event state changes.
/// </summary>
public void ClearHeartbeatHandlers() =>
_heartbeatEventHandlers.Clear();
/// <summary>
/// Adds an error event handler.
/// </summary>
/// <param name="handler"></param>
public void AddErrorHandler(IRealtimeSocket.ErrorEventHandler handler)
{
if (!_errorEventHandlers.Contains(handler))
_errorEventHandlers.Add(handler);
}
/// <summary>
/// Removes an error event handler
/// </summary>
/// <param name="handler"></param>
/// <exception cref="NotImplementedException"></exception>
public void RemoveErrorHandler(IRealtimeSocket.ErrorEventHandler handler)
{
if (_errorEventHandlers.Contains(handler))
_errorEventHandlers.Remove(handler);
}
/// <summary>
/// Clears Error Event Handlers
/// </summary>
public void ClearErrorHandlers() =>
_errorEventHandlers.Clear();
private void NotifyErrorOccurred(RealtimeException exception)
{
NotifySocketStateChange(SocketState.Error);
foreach (var handler in _errorEventHandlers.ToArray())
handler.Invoke(this, exception);
}
#endregion
/// <summary>
/// Pushes formatted data to the socket server.
///
/// If the connection is not alive, the data will be placed into a buffer to be sent when reconnected.
/// </summary>
/// <param name="data"></param>
public void Push(SocketRequest data)
{
Debugger.Instance.Log(this,
$"Socket Push [topic: {data.Topic}, event: {data.Event}, ref: {data.Ref}]:\n\t{JsonConvert.SerializeObject(data.Payload, Formatting.Indented)}");
var task = new Task(() => _options.Encode!(data, encoded => _connection.Send(encoded)));
if (_connection.IsRunning)
task.Start();
else
_buffer.Add(task);
}
/// <summary>
/// Returns the latency (in millis) of roundtrip time from socket to server and back.
/// </summary>
/// <returns></returns>
public Task<double> GetLatency()
{
var tsc = new TaskCompletionSource<double>();
var start = DateTime.Now;
var pingRef = Guid.NewGuid().ToString();
// ReSharper disable once ConvertToLocalFunction
IRealtimeSocket.MessageEventHandler? messageHandler = null;
messageHandler = (_, messageResponse) =>
{
if (messageResponse.Ref != pingRef) return;
RemoveMessageReceivedHandler(messageHandler!);
tsc.SetResult((DateTime.Now - start).TotalMilliseconds);
};
AddMessageReceivedHandler(messageHandler);
Push(new SocketRequest { Topic = "phoenix", Event = "heartbeat", Ref = pingRef });
return tsc.Task;
}
/// <summary>
/// Maintains a heartbeat connection with the socket server to prevent disconnection.
/// </summary>
private void SendHeartbeat()
{
if (!_connection.IsRunning) return;
if (_hasPendingHeartbeat)
{
_hasPendingHeartbeat = false;
Debugger.Instance.Log(this, "Socket Heartbeat Timeout: Attempting to re-establish a connection.");
_connection.Stop(WebSocketCloseStatus.EndpointUnavailable, "heartbeat timeout");
return;
}
_pendingHeartbeatRef = MakeMsgRef();
Push(new SocketRequest
{
Topic = "phoenix", Event = "heartbeat", Ref = _pendingHeartbeatRef,
Payload = new Dictionary<string, string>()
});
}
/// <summary>
/// Called when the socket opens, registers the heartbeat thread and cancels the reconnection timer.
/// </summary>
private void HandleSocketOpened()
{
_reconnectionAttempts = 0;
_hasSuccessfullyConnectedOnce = true;
// Was a reconnection attempt
if (_isReconnecting)
NotifySocketStateChange(SocketState.Reconnect);
// Reset flag for reconnections
_isReconnecting = false;
Debugger.Instance.Log(this, $"Socket Connected to: {EndpointUrl}");
_heartbeatTokenSource?.Cancel();
_hasPendingHeartbeat = false;
_heartbeatTokenSource = new CancellationTokenSource();
Task.Run(EmitHeartbeat, _heartbeatTokenSource.Token);
// Send any pending `Push` messages that were queued while socket was disconnected.
FlushBuffer();
NotifySocketStateChange(SocketState.Open);
}
private async Task EmitHeartbeat()
{
while (_heartbeatTokenSource is { IsCancellationRequested: false })
{
SendHeartbeat();
await Task.Delay(_options.HeartbeatInterval, _heartbeatTokenSource.Token);
}
}
#region Socket Event Handlers
/// <summary>
/// The socket has reconnected (or connected)
/// </summary>
/// <param name="reconnectionInfo"></param>
private void HandleSocketReconnectionHappened(ReconnectionInfo reconnectionInfo)
{
Debugger.Instance.Log(this, $"Socket Reconnection: {reconnectionInfo.Type}");
if (reconnectionInfo.Type != ReconnectionType.Initial)
_isReconnecting = true;
HandleSocketOpened();
}
/// <summary>
/// The socket has disconnected, called either through a socket closing or erroring.
/// </summary>
/// <param name="disconnectionInfo"></param>
private void HandleSocketDisconnectionHappened(DisconnectionInfo disconnectionInfo)
{
Debugger.Instance.Log(this, $"Socket Disconnection: {disconnectionInfo.Type}", disconnectionInfo.Exception);
if (disconnectionInfo.Exception != null)
HandleSocketError(disconnectionInfo);
else
HandleSocketClosed(disconnectionInfo);
}
/// <summary>
/// Parses a received socket message into a non-generic type.
/// </summary>
/// <param name="args"></param>
private void HandleSocketMessage(ResponseMessage args)
{
_options.Decode!(args.Text, decoded =>
{
Debugger.Instance.Log(this, $"Socket Message Received:\n\t{args.Text}");
// Send Separate heartbeat event
if (decoded!.Ref == _pendingHeartbeatRef)
{
NotifyHeartbeatReceived(decoded);
return;
}
decoded.Json = args.Text;
NotifyMessageReceived(decoded);
});
}
/// <summary>
/// Handles socket errors, increments reconnection count if a connection has been established at least once.
/// </summary>
/// <param name="disconnectionInfo"></param>
/// <exception cref="Exception"></exception>
private void HandleSocketError(DisconnectionInfo? disconnectionInfo = null)
{
switch (_hasSuccessfullyConnectedOnce)
{
case true:
{
_isReconnecting = true;
_connection.ReconnectTimeout = _options.ReconnectAfterInterval(++_reconnectionAttempts);
var nextInterval = DateTime.Now.AddSeconds(_connection.ReconnectTimeout.Value.Seconds).ToLocalTime();
Debugger.Instance.Log(this, $"Next reconnection attempt will occur at: {nextInterval}");
break;
}
case false when disconnectionInfo is { Exception: not RealtimeException }:
NotifyErrorOccurred(RealtimeException.FromDisconnectionInfo(disconnectionInfo));
break;
}
}
/// <summary>
/// Begins the reconnection thread with a progressively increasing interval.
/// </summary>
private void HandleSocketClosed(DisconnectionInfo? disconnectionInfo = null)
{
Debugger.Instance.Log(this, $"Socket Closed at {DateTime.Now.ToLocalTime()}", disconnectionInfo?.Exception);
}
#endregion
/// <summary>
/// Generates an incrementing identifier for message references - this reference is used
/// to coordinate requests with their responses.
/// </summary>
/// <returns></returns>
public string MakeMsgRef() => Guid.NewGuid().ToString();
/// <summary>
/// Returns the expected reply event name based off a generated message ref.
/// </summary>
/// <param name="msgRef"></param>
/// <returns></returns>
public string ReplyEventName(string msgRef) => $"chan_reply_{msgRef}";
/// <summary>
/// Dispose of the web socket connection.
/// </summary>
private async void DisposeConnection()
{
await _connection.Stop(WebSocketCloseStatus.NormalClosure, string.Empty);
_connection.Dispose();
}
/// <summary>
/// Flushes `Push` requests added while a socket was disconnected.
/// </summary>
private void FlushBuffer()
{
if (!_connection.IsRunning || _buffer.Count == 0) return;
Debugger.Instance.Log(this,
$"Socket Flushing Buffer: Connection has been reestablished and socket is sending {_buffer.Count} messages");
foreach (var item in _buffer)
{
item.Start();
}
_buffer.Clear();
}
}