From 9af89200c81af13481fa3d74b9fce5805efed65f Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Sun, 2 Feb 2025 03:02:30 +0100 Subject: [PATCH] add config for server transport type --- .../MareConfiguration/Models/ServerStorage.cs | 5 ++++- .../ServerConfigurationManager.cs | 12 ++++++++++++ MareSynchronos/UI/SettingsUi.cs | 13 +++++++++++++ MareSynchronos/WebAPI/SignalR/HubFactory.cs | 14 ++++++++++++-- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/MareSynchronos/MareConfiguration/Models/ServerStorage.cs b/MareSynchronos/MareConfiguration/Models/ServerStorage.cs index 10699d6..24a8b03 100644 --- a/MareSynchronos/MareConfiguration/Models/ServerStorage.cs +++ b/MareSynchronos/MareConfiguration/Models/ServerStorage.cs @@ -1,4 +1,6 @@ -namespace MareSynchronos.MareConfiguration.Models; +using Microsoft.AspNetCore.Http.Connections; + +namespace MareSynchronos.MareConfiguration.Models; [Serializable] public class ServerStorage @@ -10,4 +12,5 @@ public class ServerStorage public string ServerUri { get; set; } = string.Empty; public bool UseOAuth2 { get; set; } = false; public string? OAuthToken { get; set; } = null; + public HttpTransportType HttpTransportType { get; set; } = HttpTransportType.WebSockets; } \ No newline at end of file diff --git a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs index 56c90de..d60c732 100644 --- a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs +++ b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs @@ -4,6 +4,7 @@ using MareSynchronos.MareConfiguration; using MareSynchronos.MareConfiguration.Models; using MareSynchronos.Services.Mediator; using MareSynchronos.WebAPI; +using Microsoft.AspNetCore.Http.Connections; using Microsoft.Extensions.Logging; using System.Diagnostics; using System.IdentityModel.Tokens.Jwt; @@ -549,4 +550,15 @@ public class ServerConfigurationManager return discordToken; } + + public HttpTransportType GetTransport() + { + return CurrentServer.HttpTransportType; + } + + public void SetTransportType(HttpTransportType httpTransportType) + { + CurrentServer.HttpTransportType = httpTransportType; + Save(); + } } \ No newline at end of file diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index 5d4b5dd..6c03267 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -21,6 +21,7 @@ using MareSynchronos.WebAPI; using MareSynchronos.WebAPI.Files; using MareSynchronos.WebAPI.Files.Models; using MareSynchronos.WebAPI.SignalR.Utils; +using Microsoft.AspNetCore.Http.Connections; using Microsoft.Extensions.Logging; using System.Collections.Concurrent; using System.Diagnostics; @@ -1649,6 +1650,17 @@ public class SettingsUi : WindowMediatorSubscriberBase _uiShared.DrawHelpText("You cannot edit the name of the main service."); } + ImGui.SetNextItemWidth(200); + var serverTransport = _serverConfigurationManager.GetTransport(); + _uiShared.DrawCombo("Server Transport Type", Enum.GetValues().Where(t => t != HttpTransportType.None), + (v) => v.ToString(), + onSelected: (t) => _serverConfigurationManager.SetTransportType(t), + serverTransport); + _uiShared.DrawHelpText("You normally do not need to change this, if you don't know what this is or what it's for, keep it to WebSockets." + Environment.NewLine + + "If you run into connection issues with e.g. VPNs, try ServerSentEvents first before trying out LongPolling." + UiSharedService.TooltipSeparator + + "Note: if the server does not support a specific Transport Type it will fall through to the next automatically: WebSockets > ServerSentEvents > LongPolling"); + + if (ImGui.Checkbox("Use Discord OAuth2 Authentication", ref useOauth)) { selectedServer.UseOAuth2 = useOauth; @@ -1675,6 +1687,7 @@ public class SettingsUi : WindowMediatorSubscriberBase } _uiShared.DrawHelpText("Hold CTRL to delete this service"); } + ImGui.EndTabItem(); } diff --git a/MareSynchronos/WebAPI/SignalR/HubFactory.cs b/MareSynchronos/WebAPI/SignalR/HubFactory.cs index 52913cd..fce83c7 100644 --- a/MareSynchronos/WebAPI/SignalR/HubFactory.cs +++ b/MareSynchronos/WebAPI/SignalR/HubFactory.cs @@ -57,13 +57,23 @@ public class HubFactory : MediatorSubscriberBase private HubConnection BuildHubConnection(CancellationToken ct) { - Logger.LogDebug("Building new HubConnection"); + var transportType = _serverConfigurationManager.GetTransport() switch + { + HttpTransportType.None => HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling, + HttpTransportType.WebSockets => HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling, + HttpTransportType.ServerSentEvents => HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling, + HttpTransportType.LongPolling => HttpTransportType.LongPolling, + _ => HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling + }; + + var baseTransport = _serverConfigurationManager.GetTransport(); + Logger.LogDebug("Building new HubConnection using transport {transport}", baseTransport); _instance = new HubConnectionBuilder() .WithUrl(_serverConfigurationManager.CurrentApiUrl + IMareHub.Path, options => { options.AccessTokenProvider = () => _tokenProvider.GetOrUpdateToken(ct); - options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling; + options.Transports = transportType; }) .AddMessagePackProtocol(opt => {