From 3b24208acc1c56e1543c78bb147ad46ff29ba059 Mon Sep 17 00:00:00 2001 From: Tom Date: Sat, 18 Jan 2025 17:43:29 +0000 Subject: [PATCH] Added connection backoff for OBS. --- OBS/Socket/OBSSocketClient.cs | 44 +++++++---------------------------- Startup.cs | 1 + 2 files changed, 9 insertions(+), 36 deletions(-) diff --git a/OBS/Socket/OBSSocketClient.cs b/OBS/Socket/OBSSocketClient.cs index 4d2b680..ac004f9 100644 --- a/OBS/Socket/OBSSocketClient.cs +++ b/OBS/Socket/OBSSocketClient.cs @@ -7,6 +7,7 @@ using System.Collections.Concurrent; using TwitchChatTTS.OBS.Socket.Data; using System.Timers; using System.Net.WebSockets; +using CommonSocketLibrary.Backoff; namespace TwitchChatTTS.OBS.Socket { @@ -16,8 +17,8 @@ namespace TwitchChatTTS.OBS.Socket private readonly IDictionary _sourceIds; private string? URL; + private readonly IBackoff _backoff; private readonly Configuration _configuration; - private System.Timers.Timer _reconnectTimer; public bool Connected { get; set; } public bool Identified { get; set; } @@ -26,6 +27,7 @@ namespace TwitchChatTTS.OBS.Socket public OBSSocketClient( Configuration configuration, + [FromKeyedServices("hermes")] IBackoff backoff, [FromKeyedServices("obs")] IEnumerable handlers, [FromKeyedServices("obs")] MessageTypeManager typeManager, ILogger logger @@ -35,12 +37,9 @@ namespace TwitchChatTTS.OBS.Socket PropertyNamingPolicy = JsonNamingPolicy.CamelCase }, logger) { + _backoff = backoff; _configuration = configuration; - _reconnectTimer = new System.Timers.Timer(TimeSpan.FromSeconds(30)); - _reconnectTimer.Elapsed += async (sender, e) => await Reconnect(e); - _reconnectTimer.Enabled = false; - _requests = new ConcurrentDictionary(); _sourceIds = new Dictionary(); } @@ -51,18 +50,19 @@ namespace TwitchChatTTS.OBS.Socket OnConnected += (sender, e) => { Connected = true; - _reconnectTimer.Enabled = false; _logger.Information("OBS websocket client connected."); }; - OnDisconnected += (sender, e) => + OnDisconnected += async (sender, e) => { - _reconnectTimer.Enabled = Identified; _logger.Information($"OBS websocket client disconnected [status: {e.Status}][reason: {e.Reason}] " + (Identified ? "Will be attempting to reconnect every 30 seconds." : "Will not be attempting to reconnect.")); Connected = false; Identified = false; Streaming = false; + + if (Identified) + await Reconnect(_backoff); }; if (!string.IsNullOrWhiteSpace(_configuration.Obs?.Host) && _configuration.Obs?.Port != null) @@ -115,34 +115,6 @@ namespace TwitchChatTTS.OBS.Socket await handler.Execute(this, message); } - private async Task Reconnect(ElapsedEventArgs e) - { - if (Connected) - { - try - { - await DisconnectAsync(new SocketDisconnectionEventArgs(WebSocketCloseStatus.Empty.ToString(), "")); - } - catch (Exception) - { - _logger.Error("Failed to disconnect from OBS websocket server."); - } - } - - try - { - await Connect(); - } - catch (WebSocketException wse) when (wse.Message.Contains("502")) - { - _logger.Error($"OBS websocket server cannot be found. Be sure the server is on by looking at OBS > Tools > Websocket Server Settings [code: {wse.ErrorCode}]"); - } - catch (Exception ex) - { - _logger.Error(ex, "Failed to reconnect to OBS websocket server."); - } - } - public async Task Send(IEnumerable messages) { if (!Connected) diff --git a/Startup.cs b/Startup.cs index eb8e31a..86fe310 100644 --- a/Startup.cs +++ b/Startup.cs @@ -138,6 +138,7 @@ s.AddSingleton(); s.AddSingleton(); // OBS websocket +s.AddKeyedSingleton("obs", new ExponentialBackoff(1000, 120 * 1000)); s.AddKeyedSingleton("obs"); s.AddKeyedSingleton("obs"); s.AddKeyedSingleton("obs");