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

Now reconnecting Hub #427

Merged
merged 2 commits into from
Mar 26, 2024
Merged
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
29 changes: 28 additions & 1 deletion src/TagzApp.Blazor.Client/Components/Pages/Moderation.razor
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@

private HubConnection _Connection;

private bool _IsDisposing = false;

string _Tag = string.Empty;

protected override async Task OnInitializedAsync()
Expand Down Expand Up @@ -173,11 +175,35 @@
{

_Connection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/mod"))
.WithUrl(NavigationManager.ToAbsoluteUri("/mod"))
.WithAutomaticReconnect()
.Build();

await ListenForModerationContent();

_Connection.Closed += async _ => {

if (_IsDisposing) return;

while (!_IsDisposing)
{

await Task.Delay(2000);
try {

await ConfigureSignalRConnection();

} catch (Exception ex)
{

Console.WriteLine($"Error while attempting to reconnect ({_Connection.State.ToString()}) to server: {ex.Message}");

}

}

};

await _Connection.StartAsync();

}
Expand Down Expand Up @@ -375,6 +401,7 @@
public async ValueTask DisposeAsync()
{

_IsDisposing = true;
if (_Connection is not null)
{
await _Connection.DisposeAsync();
Expand Down
53 changes: 51 additions & 2 deletions src/TagzApp.Blazor.Client/Components/Pages/Waterfall.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ else
private SortedList<string, TagzApp.ViewModels.Data.ContentModel> _Content = new();

WaterfallModal Modal = new();
ContentModel ModalContent = null!;
ContentModel ModalContent {get; set; } = null!;

PauseButton thePauseButton = new();

private HashSet<dynamic> _PauseQueue = new();

HubConnection _Connection = null!;

private bool _IsDisposing = false;

[Parameter, EditorRequired]
public string TagTracked { get; set; }

Expand Down Expand Up @@ -85,6 +87,7 @@ else
{
_Connection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri($"/messages?t={TagTracked}"))
.WithAutomaticReconnect()
.Build();

_Connection.On<ContentModel>("NewWaterfallMessage", (content) =>
Expand Down Expand Up @@ -116,15 +119,60 @@ else

});

_Connection.Closed += async _ => {

if (_IsDisposing) return;

while (!_IsDisposing)
{

await Task.Delay(2000);
try {

await StartSignalRHub();

} catch (Exception ex)
{

Console.WriteLine($"Error while attempting to reconnect ({_Connection.State.ToString()}) to server: {ex.Message}");

}

}

return;

};

await _Connection.StartAsync();
}

Task SendMessageTask;
async Task ShowModal(ContentModel content)
{

thePauseButton.SetPauseState(true);
ModalContent = content;
await _Connection.InvokeAsync("SendMessageToOverlay", TagTracked, content.Provider, content.ProviderId);
SendMessageTask = Task.Run(async () => {

Console.WriteLine("Sending message to overlay");

for (var i = 0; i<10; i++)
{

if (i>0) Console.WriteLine($"Retry #{i} to activate overlay");
try {
await _Connection.InvokeAsync("SendMessageToOverlay", TagTracked, content.Provider, content.ProviderId);
break;
} catch (Exception ex) {
Console.WriteLine($"Exception while sending message to overlay: {ex.Message}");
await Task.Delay(1000 + i*1000);
}

}

});
await Task.Delay(200); // LAME delay to allow the content to propogate into the Modal component
await Modal.Open();

}
Expand All @@ -150,6 +198,7 @@ else

public ValueTask DisposeAsync()
{
_IsDisposing = true;
if (_Connection is null) return ValueTask.CompletedTask;
return _Connection.DisposeAsync();
// return ValueTask.CompletedTask;
Expand Down
Loading