Skip to content
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

Gracefully handle remote WebSocket disconnections. #121

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Mastonet/TimelineHttpStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class TimelineHttpStreaming : TimelineStreaming
private HttpClient client;
private CancellationTokenSource? cts;

public event EventHandler StreamingStopped;

public TimelineHttpStreaming(StreamingType type, string? param, string instance, string? accessToken)
: this(type, param, instance, accessToken, DefaultHttpClient.Instance) { }
public TimelineHttpStreaming(StreamingType type, string? param, string instance, string? accessToken, HttpClient client)
Expand Down Expand Up @@ -100,6 +102,7 @@ public override void Stop()
{
cts.Cancel();
cts = null;
StreamingStopped?.Invoke(this, EventArgs.Empty);
}
}
}
2 changes: 2 additions & 0 deletions Mastonet/TimelineStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ protected TimelineStreaming(StreamingType type, string? param, string? accessTok
public abstract Task Start();
public abstract void Stop();

public bool ReconnectStreamOnDisconnect { get; set; }

protected void SendEvent(string eventName, string data)
{
switch (eventName)
Expand Down
35 changes: 23 additions & 12 deletions Mastonet/TimelineWebSocketStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,37 @@ public override async Task Start()
MemoryStream ms = new MemoryStream();
while (socket != null)
{
var result = await socket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
try
{
var result = await socket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

ms.Write(buffer, 0, result.Count);
ms.Write(buffer, 0, result.Count);

if (result.EndOfMessage)
{
var messageStr = Encoding.UTF8.GetString(ms.ToArray());
if (result.EndOfMessage)
{
var messageStr = Encoding.UTF8.GetString(ms.ToArray());

#if NET6_0_OR_GREATER
var message = JsonSerializer.Deserialize(messageStr, TimelineMessageContext.Default.TimelineMessage);
var message = JsonSerializer.Deserialize(messageStr, TimelineMessageContext.Default.TimelineMessage);
#else
var message = JsonSerializer.Deserialize<TimelineMessage>(messageStr);
#endif
if (message != null)
if (message != null)
{
SendEvent(message.Event, message.Payload);
}

ms.Dispose();
ms = new MemoryStream();
}
}
catch (WebSocketException ex)
{
this.Stop();
if (ReconnectStreamOnDisconnect)
{
SendEvent(message.Event, message.Payload);
await this.Start();
}

ms.Dispose();
ms = new MemoryStream();
}
}
ms.Dispose();
Expand All @@ -113,7 +124,7 @@ internal class TimelineMessage

public override void Stop()
{
if (socket != null)
if (socket?.State == WebSocketState.Open)
{
socket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
socket.Dispose();
Expand Down