Cleaned code up. Added OBS & 7tv ws support. Added dependency injection. App loads from yml file.
This commit is contained in:
46
Seven/Socket/Handlers/DispatchHandler.cs
Normal file
46
Seven/Socket/Handlers/DispatchHandler.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System.Text.Json;
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.Seven.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
{
|
||||
public class DispatchHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
private IServiceProvider ServiceProvider { get; }
|
||||
public int OperationCode { get; set; } = 0;
|
||||
|
||||
public DispatchHandler(ILogger<DispatchHandler> logger, IServiceProvider serviceProvider) {
|
||||
Logger = logger;
|
||||
ServiceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not DispatchMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
Do(obj?.Body?.Pulled, cf => cf.OldValue);
|
||||
Do(obj?.Body?.Pushed, cf => cf.Value);
|
||||
}
|
||||
|
||||
private void Do(IEnumerable<ChangeField>? fields, Func<ChangeField, object> getter) {
|
||||
if (fields is null)
|
||||
return;
|
||||
|
||||
//ServiceProvider.GetRequiredService<EmoteDatabase>()
|
||||
foreach (var val in fields) {
|
||||
if (getter(val) == null)
|
||||
continue;
|
||||
|
||||
var o = JsonSerializer.Deserialize<EmoteField>(val.OldValue.ToString(), new JsonSerializerOptions() {
|
||||
PropertyNameCaseInsensitive = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
88
Seven/Socket/Handlers/EndOfStreamHandler.cs
Normal file
88
Seven/Socket/Handlers/EndOfStreamHandler.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.Seven.Socket.Context;
|
||||
using TwitchChatTTS.Seven.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
{
|
||||
public class EndOfStreamHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
private IServiceProvider ServiceProvider { get; }
|
||||
private string[] ErrorCodes { get; }
|
||||
private int[] ReconnectDelay { get; }
|
||||
|
||||
public int OperationCode { get; set; } = 7;
|
||||
|
||||
|
||||
public EndOfStreamHandler(ILogger<EndOfStreamHandler> logger, IServiceProvider serviceProvider) {
|
||||
Logger = logger;
|
||||
ServiceProvider = serviceProvider;
|
||||
|
||||
ErrorCodes = [
|
||||
"Server Error",
|
||||
"Unknown Operation",
|
||||
"Invalid Payload",
|
||||
"Auth Failure",
|
||||
"Already Identified",
|
||||
"Rate Limited",
|
||||
"Restart",
|
||||
"Maintenance",
|
||||
"Timeout",
|
||||
"Already Subscribed",
|
||||
"Not Subscribed",
|
||||
"Insufficient Privilege",
|
||||
"Inactivity?"
|
||||
];
|
||||
ReconnectDelay = [
|
||||
1000,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
3000,
|
||||
1000,
|
||||
300000,
|
||||
1000,
|
||||
-1,
|
||||
-1,
|
||||
1000,
|
||||
1000
|
||||
];
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not EndOfStreamMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
var code = obj.Code - 4000;
|
||||
if (code >= 0 && code < ErrorCodes.Length)
|
||||
Logger.LogWarning($"Received end of stream message (reason: {ErrorCodes[code]}, code: {obj.Code}, message: {obj.Message}).");
|
||||
else
|
||||
Logger.LogWarning($"Received end of stream message (code: {obj.Code}, message: {obj.Message}).");
|
||||
|
||||
await sender.DisconnectAsync();
|
||||
|
||||
if (code >= 0 && code < ReconnectDelay.Length && ReconnectDelay[code] < 0) {
|
||||
Logger.LogError($"7tv client will remain disconnected due to a bad client implementation.");
|
||||
return;
|
||||
}
|
||||
|
||||
var context = ServiceProvider.GetRequiredService<ReconnectContext>();
|
||||
await Task.Delay(ReconnectDelay[code]);
|
||||
|
||||
Logger.LogInformation($"7tv client reconnecting.");
|
||||
await sender.ConnectAsync($"{context.Protocol ?? "wss"}://{context.Url}");
|
||||
if (context.SessionId is null) {
|
||||
await sender.Send(33, new object());
|
||||
} else {
|
||||
await sender.Send(34, new ResumeMessage() {
|
||||
SessionId = context.SessionId
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
Seven/Socket/Handlers/ErrorHandler.cs
Normal file
23
Seven/Socket/Handlers/ErrorHandler.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.Seven.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
{
|
||||
public class ErrorHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
public int OperationCode { get; set; } = 6;
|
||||
|
||||
public ErrorHandler(ILogger<ErrorHandler> logger) {
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not ErrorMessage obj || obj == null)
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
25
Seven/Socket/Handlers/ReconnectHandler.cs
Normal file
25
Seven/Socket/Handlers/ReconnectHandler.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.Seven.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
{
|
||||
public class ReconnectHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
public int OperationCode { get; set; } = 4;
|
||||
|
||||
public ReconnectHandler(ILogger<ReconnectHandler> logger) {
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not ReconnectMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
Logger.LogInformation($"7tv server wants us to reconnect (reason: {obj.Reason}).");
|
||||
}
|
||||
}
|
||||
}
|
56
Seven/Socket/Handlers/SevenHelloHandler.cs
Normal file
56
Seven/Socket/Handlers/SevenHelloHandler.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.Seven.Socket.Context;
|
||||
using TwitchChatTTS.Seven.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.Seven.Socket.Handlers
|
||||
{
|
||||
public class SevenHelloHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
private SevenHelloContext Context { get; }
|
||||
public int OperationCode { get; set; } = 1;
|
||||
|
||||
public SevenHelloHandler(ILogger<SevenHelloHandler> logger, SevenHelloContext context) {
|
||||
Logger = logger;
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not SevenHelloMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
if (sender is not SevenSocketClient seven || seven == null)
|
||||
return;
|
||||
|
||||
seven.Connected = true;
|
||||
seven.ConnectionDetails = obj;
|
||||
|
||||
// if (Context.Subscriptions == null || !Context.Subscriptions.Any()) {
|
||||
// Logger.LogWarning("No subscriptions have been set for the 7tv websocket client.");
|
||||
// return;
|
||||
// }
|
||||
|
||||
//await Task.Delay(TimeSpan.FromMilliseconds(1000));
|
||||
//await sender.Send(33, new IdentifyMessage());
|
||||
//await Task.Delay(TimeSpan.FromMilliseconds(5000));
|
||||
//await sender.SendRaw("{\"op\":35,\"d\":{\"type\":\"emote_set.*\",\"condition\":{\"object_id\":\"64505914b9fc508169ffe7cc\"}}}");
|
||||
//await sender.SendRaw(File.ReadAllText("test.txt"));
|
||||
|
||||
// foreach (var sub in Context.Subscriptions) {
|
||||
// if (string.IsNullOrWhiteSpace(sub.Type)) {
|
||||
// Logger.LogWarning("Non-existent or empty subscription type found on the 7tv websocket client.");
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Logger.LogDebug($"Subscription Type: {sub.Type} | Condition: {string.Join(", ", sub.Condition?.Select(e => e.Key + "=" + e.Value) ?? new string[0])}");
|
||||
// await sender.Send(35, new SubscribeMessage() {
|
||||
// Type = sub.Type,
|
||||
// Condition = sub.Condition
|
||||
// });
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user