From 7b89ab1119e81fc57c830f7ac6dc2b166ded7bb0 Mon Sep 17 00:00:00 2001 From: brian91292 Date: Fri, 20 Dec 2019 00:25:18 -0600 Subject: [PATCH] minor bugfixes/improvements --- Chat/Twitch/TwitchWebSocketClient.cs | 20 ++++---- Chat/websocket-sharp/WebSocket.cs | 4 +- Plugin.cs | 2 +- Properties/AssemblyInfo.cs | 4 +- StreamCore.csproj | 1 + Utilities/JSONFormatter.cs | 73 ++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 Utilities/JSONFormatter.cs diff --git a/Chat/Twitch/TwitchWebSocketClient.cs b/Chat/Twitch/TwitchWebSocketClient.cs index 7587429..98c774f 100644 --- a/Chat/Twitch/TwitchWebSocketClient.cs +++ b/Chat/Twitch/TwitchWebSocketClient.cs @@ -44,7 +44,7 @@ public class TwitchWebSocketClient /// /// The last time the client established a connection to the Twitch servers. /// - public static DateTime ConnectionTime { get; private set; } = DateTime.Now; + public static DateTime ConnectionTime { get; private set; } = DateTime.UtcNow; /// /// A dictionary of channel information for every channel we've joined during this session, the key is the channel name. @@ -81,7 +81,7 @@ public class TwitchWebSocketClient /// public static bool IsChannelValid { get => ChannelInfo.TryGetValue(TwitchLoginConfig.Instance.TwitchChannelName, out var channelInfo) && channelInfo.roomId != String.Empty; } - private static DateTime _sendLimitResetTime = DateTime.Now; + private static DateTime _sendLimitResetTime = DateTime.UtcNow; private static int _reconnectCooldown = 500; private static int _fullReconnects = -1; private static string _lastChannel = ""; @@ -211,7 +211,7 @@ private static void Connect(bool isManualReconnect = false) _ws.Send($"NICK {username}"); // Display a message in the chat informing the user whether or not the connection to the channel was successful - ConnectionTime = DateTime.Now; + ConnectionTime = DateTime.UtcNow; // Invoke OnConnected event if (OnConnected != null) @@ -234,13 +234,14 @@ private static void Connect(bool isManualReconnect = false) _ws.OnClose += (sender, e) => { Plugin.Log("Twitch connection terminated."); + LoggedIn = false; Connected = false; }; _ws.OnError += (sender, e) => { + Plugin.Log($"An error occurred in the twitch connection! Error: {e.Message}, Exception: {e.Exception}"); LoggedIn = false; - Plugin.Log($"An error occured in the twitch connection! Error: {e.Message}, Exception: {e.Exception}"); Connected = false; }; @@ -254,20 +255,21 @@ private static void Connect(bool isManualReconnect = false) { try { - DateTime nextPing = DateTime.Now.AddSeconds(30); + DateTime nextPing = DateTime.UtcNow.AddSeconds(30); while (Connected && _ws.ReadyState == WebSocketState.Open) { //Plugin.Log("Connected and alive!"); Thread.Sleep(500); - if (nextPing < DateTime.Now) + if (nextPing < DateTime.UtcNow) { + Plugin.Log("Ping!"); if (!_ws.IsAlive) { Plugin.Log("Ping failed, reconnecting!"); break; } - nextPing = DateTime.Now.AddSeconds(30); + nextPing = DateTime.UtcNow.AddSeconds(30); } } } @@ -325,10 +327,10 @@ private static void ProcessSendQueue(int fullReconnects) { if (LoggedIn && _ws.ReadyState == WebSocketState.Open) { - if (_sendLimitResetTime < DateTime.Now) + if (_sendLimitResetTime < DateTime.UtcNow) { _messagesSent = 0; - _sendLimitResetTime = DateTime.Now.AddSeconds(_sendResetInterval); + _sendLimitResetTime = DateTime.UtcNow.AddSeconds(_sendResetInterval); } if (_sendQueue.Count > 0) diff --git a/Chat/websocket-sharp/WebSocket.cs b/Chat/websocket-sharp/WebSocket.cs index 4c18508..7b9a1f0 100644 --- a/Chat/websocket-sharp/WebSocket.cs +++ b/Chat/websocket-sharp/WebSocket.cs @@ -2183,8 +2183,8 @@ private void startReceiving () message (); }, ex => { - _logger.Fatal (ex.ToString ()); - error ("An exception has occurred while receiving.", ex); + _logger.Error (ex.ToString ()); + fatal ("An exception has occurred while receiving.", ex); } ); diff --git a/Plugin.cs b/Plugin.cs index 33b7de8..b07963d 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -20,7 +20,7 @@ public class Plugin : IPlugin private readonly TwitchLoginConfig TwitchLoginConfig = new TwitchLoginConfig(); public static readonly string ModuleName = "Stream Core"; public string Name => ModuleName; - public string Version => "2.2.3"; + public string Version => "2.2.4"; private static readonly object _loggerLock = new object(); public static void Log(string text, diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 225a04f..06296ad 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.2.3.0")] -[assembly: AssemblyFileVersion("2.2.3.0")] +[assembly: AssemblyVersion("2.2.4.0")] +[assembly: AssemblyFileVersion("2.2.4.0")] diff --git a/StreamCore.csproj b/StreamCore.csproj index 45a61bf..1408413 100644 --- a/StreamCore.csproj +++ b/StreamCore.csproj @@ -193,6 +193,7 @@ + diff --git a/Utilities/JSONFormatter.cs b/Utilities/JSONFormatter.cs new file mode 100644 index 0000000..e7332ee --- /dev/null +++ b/Utilities/JSONFormatter.cs @@ -0,0 +1,73 @@ +using System.Text; + +namespace StreamCore.Utils +{ + public class JsonFormatter + { + public static string Indent = " "; + + public static string PrettyPrint(string input) + { + var output = new StringBuilder(input.Length * 2); + char? quote = null; + int depth = 0; + + for (int i = 0; i < input.Length; ++i) + { + char ch = input[i]; + + switch (ch) + { + case '{': + case '[': + output.Append(ch); + if (!quote.HasValue) + { + output.AppendLine(); + output.Append(Indent.Repeat(++depth)); + } + break; + case '}': + case ']': + if (quote.HasValue) + output.Append(ch); + else + { + output.AppendLine(); + output.Append(Indent.Repeat(--depth)); + output.Append(ch); + } + break; + case '"': + case '\'': + output.Append(ch); + if (quote.HasValue) + { + if (!output.IsEscaped(i)) + quote = null; + } + else quote = ch; + break; + case ',': + output.Append(ch); + if (!quote.HasValue) + { + output.AppendLine(); + output.Append(Indent.Repeat(depth)); + } + break; + case ':': + if (quote.HasValue) output.Append(ch); + else output.Append(" : "); + break; + default: + if (quote.HasValue || !char.IsWhiteSpace(ch)) + output.Append(ch); + break; + } + } + + return output.ToString(); + } + } +}