mirror of
https://codeberg.org/Ikatono/TierMaker.git
synced 2025-10-28 20:45:35 -05:00
improved Twitch connection menu experience
This commit is contained in:
@@ -12,7 +12,7 @@ public partial class TwitchChatWatcher : Node
|
||||
{
|
||||
private readonly ClientWebSocket Socket = new();
|
||||
public readonly ConcurrentQueue<TwitchChatMessage> Queue = new();
|
||||
private readonly CancellationTokenSource TokenSource = new();
|
||||
private CancellationTokenSource TokenSource = new();
|
||||
public CancellationToken Token => TokenSource.Token;
|
||||
private CommandHandler CommandHandler { get; set; }
|
||||
public WebSocketState State => Socket.State;
|
||||
@@ -22,6 +22,11 @@ public partial class TwitchChatWatcher : Node
|
||||
|
||||
[Signal]
|
||||
public delegate void IncomingCommandEventHandler(Command command);
|
||||
[Signal]
|
||||
public delegate void SocketConnectedEventHandler();
|
||||
[Signal]
|
||||
public delegate void SocketDisconnectedEventHandler();
|
||||
public string Channel { get; private set ;}
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -30,16 +35,11 @@ public partial class TwitchChatWatcher : Node
|
||||
CommandHandler = GetNode<CommandHandler>("/root/CommandHandler")
|
||||
?? throw new Exception($"{nameof(Command)} not found");
|
||||
}
|
||||
private readonly ConcurrentQueue<string> PrintQueue = new();
|
||||
private readonly ConcurrentQueue<string> ErrorQueue = new();
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (PrintQueue.TryDequeue(out string s))
|
||||
GD.Print(s);
|
||||
if (ErrorQueue.TryDequeue(out string e))
|
||||
GD.PrintErr(e);
|
||||
|
||||
}
|
||||
public async Task ConnectAsync()
|
||||
{
|
||||
@@ -47,9 +47,16 @@ public partial class TwitchChatWatcher : Node
|
||||
if (Socket.State == WebSocketState.Open)
|
||||
return;
|
||||
await Socket.ConnectAsync(new Uri("wss://irc-ws.chat.twitch.tv:443"), Token);
|
||||
_ = Task.Run(GetPacketsTask, Token);
|
||||
_ = Task.Run(HandleMessages, Token);
|
||||
|
||||
if (Socket.State == WebSocketState.Open)
|
||||
{
|
||||
_ = Task.Run(GetPacketsTask, Token);
|
||||
_ = Task.Run(HandleMessages, Token);
|
||||
CallDeferred("emit_signal", nameof(SocketConnected));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Failed to connect to Twitch");
|
||||
}
|
||||
}
|
||||
public async Task Authenticate(string user = null, string pass = null)
|
||||
{
|
||||
@@ -65,10 +72,15 @@ public partial class TwitchChatWatcher : Node
|
||||
}
|
||||
public async Task JoinChannel(string channel)
|
||||
{
|
||||
GD.Print("Joining channel");
|
||||
channel = channel.TrimStart('#');
|
||||
if (Channel is not null)
|
||||
{
|
||||
await SendMessageAsync(TwitchChatMessageType.PART,
|
||||
parameters: new string[] {"#" + Channel});
|
||||
}
|
||||
await SendMessageAsync(TwitchChatMessageType.JOIN,
|
||||
parameters: new string[] {"#" + channel});
|
||||
parameters: new string[] {"#" + channel});
|
||||
Channel = channel;
|
||||
}
|
||||
public async Task SendMessageAsync(string message)
|
||||
{
|
||||
@@ -109,7 +121,6 @@ public partial class TwitchChatWatcher : Node
|
||||
}
|
||||
await SendMessageAsync(message);
|
||||
}
|
||||
private static ulong PacketCount;
|
||||
private async Task GetPacketsTask()
|
||||
{
|
||||
try
|
||||
@@ -123,37 +134,29 @@ public partial class TwitchChatWatcher : Node
|
||||
return;
|
||||
if (Socket.State != WebSocketState.Open)
|
||||
{
|
||||
ErrorQueue.Enqueue("Socket closed");
|
||||
GD.PrintErr("Socket closed");
|
||||
CallDeferred("emit_signal", nameof(SocketDisconnected));
|
||||
return;
|
||||
}
|
||||
if (res.Count == 0)
|
||||
{
|
||||
ErrorQueue.Enqueue("Empty packet received");
|
||||
GD.PrintErr("Empty packet received");
|
||||
continue;
|
||||
}
|
||||
PacketCount++;
|
||||
PrintQueue.Enqueue($"Packet count: {PacketCount}");
|
||||
stringData += Encoding.UTF8.GetString(arr, 0, res.Count);
|
||||
//PrintQueue.Enqueue(stringData);
|
||||
var lines = stringData.Split("\r\n", StringSplitOptions.TrimEntries);
|
||||
if (!lines.Any())
|
||||
continue;
|
||||
stringData = lines.Last();
|
||||
PrintQueue.Enqueue($"Line count: {lines.SkipLast(1).Count()}");
|
||||
foreach (var line in lines.SkipLast(1))
|
||||
MessageStrings.Enqueue(line);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ErrorQueue.Enqueue(e.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!Token.IsCancellationRequested)
|
||||
ErrorQueue.Enqueue($"{nameof(GetPacketsTask)} exited without cancellation");
|
||||
else
|
||||
PrintQueue.Enqueue($"{nameof(GetPacketsTask)} cancelled and exited");
|
||||
GD.PushError($"{nameof(GetPacketsTask)} exited without cancellation");
|
||||
}
|
||||
}
|
||||
private readonly ConcurrentQueue<string> MessageStrings = new();
|
||||
@@ -167,7 +170,7 @@ public partial class TwitchChatWatcher : Node
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
continue;
|
||||
PrintQueue.Enqueue(message);
|
||||
GD.Print(message);
|
||||
// if (PrintAllIncoming)
|
||||
// PrintQueue.Enqueue(message);
|
||||
var tcm = TwitchChatMessage.Parse(message);
|
||||
@@ -180,6 +183,7 @@ public partial class TwitchChatWatcher : Node
|
||||
continue;
|
||||
var chat = p.ChatMessage;
|
||||
chat = chat[com.Length..].TrimStart();
|
||||
//TODO make better
|
||||
CallDeferred("emit_signal", SignalName.IncomingCommand,
|
||||
new Command(p.DisplayName,
|
||||
false, p.Moderator, chat));
|
||||
@@ -188,17 +192,10 @@ public partial class TwitchChatWatcher : Node
|
||||
await Task.Delay(50);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ErrorQueue.Enqueue(e.ToString() + System.Environment.NewLine
|
||||
+ e.StackTrace);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!Token.IsCancellationRequested)
|
||||
ErrorQueue.Enqueue($"{nameof(HandleMessages)} exited without cancellation");
|
||||
else
|
||||
ErrorQueue.Enqueue($"{nameof(HandleMessages)} cancelled and exited");
|
||||
GD.PushError($"{nameof(HandleMessages)} exited without cancellation");
|
||||
}
|
||||
}
|
||||
private async Task SendPong(TwitchChatMessage ping)
|
||||
@@ -206,6 +203,6 @@ public partial class TwitchChatWatcher : Node
|
||||
var pong = TwitchChatMessage.MakePong(ping);
|
||||
await SendMessageAsync(TwitchChatMessageType.PONG, ping.Parameters,
|
||||
ping.MessageTags, ping.Prefix);
|
||||
PrintQueue.Enqueue("Sent Pong");
|
||||
GD.Print("Sent Pong");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user