Added connection backoff for OBS.
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user