Cleaned code up. Added OBS & 7tv ws support. Added dependency injection. App loads from yml file.
This commit is contained in:
10
OBS/Socket/Context/HelloContext.cs
Normal file
10
OBS/Socket/Context/HelloContext.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace TwitchChatTTS.OBS.Socket.Context
|
||||
{
|
||||
[Serializable]
|
||||
public class HelloContext
|
||||
{
|
||||
public string? Host { get; set; }
|
||||
public short? Port { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
}
|
10
OBS/Socket/Data/EventMessage.cs
Normal file
10
OBS/Socket/Data/EventMessage.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace TwitchChatTTS.OBS.Socket.Data
|
||||
{
|
||||
[Serializable]
|
||||
public class EventMessage
|
||||
{
|
||||
public string eventType { get; set; }
|
||||
public int eventIntent { get; set; }
|
||||
public Dictionary<string, object> eventData { get; set; }
|
||||
}
|
||||
}
|
15
OBS/Socket/Data/HelloMessage.cs
Normal file
15
OBS/Socket/Data/HelloMessage.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace TwitchChatTTS.OBS.Socket.Data
|
||||
{
|
||||
[Serializable]
|
||||
public class HelloMessage
|
||||
{
|
||||
public string obsWebSocketVersion { get; set; }
|
||||
public int rpcVersion { get; set; }
|
||||
public AuthenticationMessage authentication { get; set; }
|
||||
}
|
||||
|
||||
public class AuthenticationMessage {
|
||||
public string challenge { get; set; }
|
||||
public string salt { get; set; }
|
||||
}
|
||||
}
|
8
OBS/Socket/Data/IdentifiedMessage.cs
Normal file
8
OBS/Socket/Data/IdentifiedMessage.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace TwitchChatTTS.OBS.Socket.Data
|
||||
{
|
||||
[Serializable]
|
||||
public class IdentifiedMessage
|
||||
{
|
||||
public int negotiatedRpcVersion { get; set; }
|
||||
}
|
||||
}
|
16
OBS/Socket/Data/IdentifyMessage.cs
Normal file
16
OBS/Socket/Data/IdentifyMessage.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace TwitchChatTTS.OBS.Socket.Data
|
||||
{
|
||||
[Serializable]
|
||||
public class IdentifyMessage
|
||||
{
|
||||
public int rpcVersion { get; set; }
|
||||
public string? authentication { get; set; }
|
||||
public int eventSubscriptions { get; set; }
|
||||
|
||||
public IdentifyMessage(int version, string auth, int subscriptions) {
|
||||
rpcVersion = version;
|
||||
authentication = auth;
|
||||
eventSubscriptions = subscriptions;
|
||||
}
|
||||
}
|
||||
}
|
16
OBS/Socket/Data/RequestMessage.cs
Normal file
16
OBS/Socket/Data/RequestMessage.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace TwitchChatTTS.OBS.Socket.Data
|
||||
{
|
||||
[Serializable]
|
||||
public class RequestMessage
|
||||
{
|
||||
public string requestType { get; set; }
|
||||
public string requestId { get; set; }
|
||||
public Dictionary<string, object> requestData { get; set; }
|
||||
|
||||
public RequestMessage(string type, string id, Dictionary<string, object> data) {
|
||||
requestType = type;
|
||||
requestId = id;
|
||||
requestData = data;
|
||||
}
|
||||
}
|
||||
}
|
11
OBS/Socket/Data/RequestResponseMessage.cs
Normal file
11
OBS/Socket/Data/RequestResponseMessage.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace TwitchChatTTS.OBS.Socket.Data
|
||||
{
|
||||
[Serializable]
|
||||
public class RequestResponseMessage
|
||||
{
|
||||
public string requestType { get; set; }
|
||||
public string requestId { get; set; }
|
||||
public object requestStatus { get; set; }
|
||||
public Dictionary<string, object> responseData { get; set; }
|
||||
}
|
||||
}
|
48
OBS/Socket/Handlers/EventMessageHandler.cs
Normal file
48
OBS/Socket/Handlers/EventMessageHandler.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.OBS.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.OBS.Socket.Handlers
|
||||
{
|
||||
public class EventMessageHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
private IServiceProvider ServiceProvider { get; }
|
||||
public int OperationCode { get; set; } = 5;
|
||||
|
||||
public EventMessageHandler(ILogger<EventMessageHandler> logger, IServiceProvider serviceProvider) {
|
||||
Logger = logger;
|
||||
ServiceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not EventMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
switch (obj.eventType) {
|
||||
case "StreamStateChanged":
|
||||
case "RecordStateChanged":
|
||||
if (sender is not OBSSocketClient client)
|
||||
return;
|
||||
|
||||
string? raw_state = obj.eventData["outputState"].ToString();
|
||||
string? state = raw_state?.Substring(21).ToLower();
|
||||
client.Live = obj.eventData["outputActive"].ToString() == "True";
|
||||
Logger.LogWarning("Stream " + (state != null && state.EndsWith("ing") ? "is " : "has ") + state + ".");
|
||||
|
||||
if (client.Live == false && state != null && !state.EndsWith("ing")) {
|
||||
OnStreamEnd();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Logger.LogDebug(obj.eventType + " EVENT: " + string.Join(" | ", obj.eventData?.Select(x => x.Key + "=" + x.Value?.ToString()) ?? new string[0]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnStreamEnd() {
|
||||
}
|
||||
}
|
||||
}
|
55
OBS/Socket/Handlers/HelloHandler.cs
Normal file
55
OBS/Socket/Handlers/HelloHandler.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.OBS.Socket.Data;
|
||||
using TwitchChatTTS.OBS.Socket.Context;
|
||||
|
||||
namespace TwitchChatTTS.OBS.Socket.Handlers
|
||||
{
|
||||
public class HelloHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
public int OperationCode { get; set; } = 0;
|
||||
private HelloContext Context { get; }
|
||||
|
||||
public HelloHandler(ILogger<HelloHandler> logger, HelloContext context) {
|
||||
Logger = logger;
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not HelloMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
Logger.LogTrace("OBS websocket password: " + Context.Password);
|
||||
if (obj.authentication is null || Context.Password is null) // TODO: send re-identify message.
|
||||
return;
|
||||
|
||||
var salt = obj.authentication.salt;
|
||||
var challenge = obj.authentication.challenge;
|
||||
Logger.LogTrace("Salt: " + salt);
|
||||
Logger.LogTrace("Challenge: " + challenge);
|
||||
|
||||
|
||||
string secret = Context.Password + salt;
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(secret);
|
||||
string hash = null;
|
||||
using (var sha = SHA256.Create()) {
|
||||
bytes = sha.ComputeHash(bytes);
|
||||
hash = Convert.ToBase64String(bytes);
|
||||
|
||||
secret = hash + challenge;
|
||||
bytes = Encoding.UTF8.GetBytes(secret);
|
||||
bytes = sha.ComputeHash(bytes);
|
||||
hash = Convert.ToBase64String(bytes);
|
||||
}
|
||||
|
||||
Logger.LogTrace("Final hash: " + hash);
|
||||
//await sender.Send(1, new IdentifyMessage(obj.rpcVersion, hash, 1023 | 262144 | 524288));
|
||||
await sender.Send(1, new IdentifyMessage(obj.rpcVersion, hash, 1023 | 262144));
|
||||
}
|
||||
}
|
||||
}
|
26
OBS/Socket/Handlers/IdentifiedHandler.cs
Normal file
26
OBS/Socket/Handlers/IdentifiedHandler.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.OBS.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.OBS.Socket.Handlers
|
||||
{
|
||||
public class IdentifiedHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
public int OperationCode { get; set; } = 2;
|
||||
|
||||
public IdentifiedHandler(ILogger<IdentifiedHandler> logger) {
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not IdentifiedMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
sender.Connected = true;
|
||||
Logger.LogInformation("Connected to OBS via rpc version " + obj.negotiatedRpcVersion + ".");
|
||||
}
|
||||
}
|
||||
}
|
35
OBS/Socket/Handlers/RequestResponseHandler.cs
Normal file
35
OBS/Socket/Handlers/RequestResponseHandler.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TwitchChatTTS.OBS.Socket.Data;
|
||||
|
||||
namespace TwitchChatTTS.OBS.Socket.Handlers
|
||||
{
|
||||
public class RequestResponseHandler : IWebSocketHandler
|
||||
{
|
||||
private ILogger Logger { get; }
|
||||
public int OperationCode { get; set; } = 7;
|
||||
|
||||
public RequestResponseHandler(ILogger<RequestResponseHandler> logger) {
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
|
||||
{
|
||||
if (message is not RequestResponseMessage obj || obj == null)
|
||||
return;
|
||||
|
||||
switch (obj.requestType) {
|
||||
case "GetOutputStatus":
|
||||
if (sender is not OBSSocketClient client)
|
||||
return;
|
||||
|
||||
if (obj.requestId == "stream") {
|
||||
client.Live = obj.responseData["outputActive"].ToString() == "True";
|
||||
Logger.LogWarning("Updated stream's live status to " + client.Live);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
31
OBS/Socket/Manager/OBSHandlerManager.cs
Normal file
31
OBS/Socket/Manager/OBSHandlerManager.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using CommonSocketLibrary.Socket.Manager;
|
||||
using CommonSocketLibrary.Common;
|
||||
|
||||
namespace TwitchChatTTS.OBS.Socket.Manager
|
||||
{
|
||||
public class OBSHandlerManager : WebSocketHandlerManager
|
||||
{
|
||||
public OBSHandlerManager(ILogger<OBSHandlerManager> logger, IServiceProvider provider) : base(logger) {
|
||||
var basetype = typeof(IWebSocketHandler);
|
||||
var assembly = GetType().Assembly;
|
||||
var types = assembly.GetTypes().Where(t => t.IsClass && basetype.IsAssignableFrom(t) && t.AssemblyQualifiedName?.Contains(".OBS.") == true);
|
||||
|
||||
foreach (var type in types) {
|
||||
var key = "obs-" + type.Name.Replace("Handlers", "Hand#lers")
|
||||
.Replace("Handler", "")
|
||||
.Replace("Hand#lers", "Handlers")
|
||||
.ToLower();
|
||||
var handler = provider.GetKeyedService<IWebSocketHandler>(key);
|
||||
if (handler == null) {
|
||||
logger.LogError("Failed to find obs websocket handler: " + type.AssemblyQualifiedName);
|
||||
continue;
|
||||
}
|
||||
|
||||
Logger.LogDebug($"Linked type {type.AssemblyQualifiedName} to obs websocket handler {handler.GetType().AssemblyQualifiedName}.");
|
||||
Add(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
19
OBS/Socket/Manager/OBSHandlerTypeManager.cs
Normal file
19
OBS/Socket/Manager/OBSHandlerTypeManager.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using CommonSocketLibrary.Common;
|
||||
using CommonSocketLibrary.Socket.Manager;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace TwitchChatTTS.OBS.Socket.Manager
|
||||
{
|
||||
public class OBSHandlerTypeManager : WebSocketHandlerTypeManager
|
||||
{
|
||||
public OBSHandlerTypeManager(
|
||||
ILogger<OBSHandlerTypeManager> factory,
|
||||
[FromKeyedServices("obs")] HandlerManager<WebSocketClient,
|
||||
IWebSocketHandler> handlers
|
||||
) : base(factory, handlers)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
31
OBS/Socket/OBSSocketClient.cs
Normal file
31
OBS/Socket/OBSSocketClient.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using TwitchChatTTS.OBS.Socket.Manager;
|
||||
using CommonSocketLibrary.Common;
|
||||
using CommonSocketLibrary.Abstract;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace TwitchChatTTS.OBS.Socket
|
||||
{
|
||||
public class OBSSocketClient : WebSocketClient {
|
||||
private bool _live;
|
||||
public bool? Live {
|
||||
get => Connected ? _live : null;
|
||||
set {
|
||||
if (value.HasValue)
|
||||
_live = value.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public OBSSocketClient(
|
||||
ILogger<OBSSocketClient> logger,
|
||||
[FromKeyedServices("obs")] HandlerManager<WebSocketClient, IWebSocketHandler> handlerManager,
|
||||
[FromKeyedServices("obs")] HandlerTypeManager<WebSocketClient, IWebSocketHandler> typeManager
|
||||
) : base(logger, handlerManager, typeManager, new JsonSerializerOptions() {
|
||||
PropertyNameCaseInsensitive = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
}) {
|
||||
_live = false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user