Added connection backoff for OBS.

This commit is contained in:
Tom
2025-01-18 17:43:29 +00:00
parent c373af5281
commit 3b24208acc
2 changed files with 9 additions and 36 deletions

View File

@ -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<string, long> _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<IWebSocketHandler> handlers,
[FromKeyedServices("obs")] MessageTypeManager<IWebSocketHandler> 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<string, RequestData>();
_sourceIds = new Dictionary<string, long>();
}
@ -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<RequestMessage> messages)
{
if (!Connected)

View File

@ -138,6 +138,7 @@ s.AddSingleton<IChatterGroupManager, ChatterGroupManager>();
s.AddSingleton<IGroupPermissionManager, GroupPermissionManager>();
// OBS websocket
s.AddKeyedSingleton<IBackoff>("obs", new ExponentialBackoff(1000, 120 * 1000));
s.AddKeyedSingleton<IWebSocketHandler, HelloHandler>("obs");
s.AddKeyedSingleton<IWebSocketHandler, IdentifiedHandler>("obs");
s.AddKeyedSingleton<IWebSocketHandler, RequestResponseHandler>("obs");