Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
minor bugfixes/improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
brian91292 committed Dec 20, 2019
1 parent cae8eb8 commit 7b89ab1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
20 changes: 11 additions & 9 deletions Chat/Twitch/TwitchWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class TwitchWebSocketClient
/// <summary>
/// The last time the client established a connection to the Twitch servers.
/// </summary>
public static DateTime ConnectionTime { get; private set; } = DateTime.Now;
public static DateTime ConnectionTime { get; private set; } = DateTime.UtcNow;

/// <summary>
/// A dictionary of channel information for every channel we've joined during this session, the key is the channel name.
Expand Down Expand Up @@ -81,7 +81,7 @@ public class TwitchWebSocketClient
/// </summary>
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 = "";
Expand Down Expand Up @@ -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)
Expand All @@ -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;
};

Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Chat/websocket-sharp/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);

Expand Down
2 changes: 1 addition & 1 deletion Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
1 change: 1 addition & 0 deletions StreamCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Chat\Twitch\TwitchAPI.cs" />
<Compile Include="Chat\YouTube\YouTubeCallbackListener.cs" />
<Compile Include="Utilities\JSONFormatter.cs" />
<Compile Include="Utilities\ObjectPool.cs" />
<Compile Include="Utilities\ReflectionUtil.cs" />
<Compile Include="Utilities\SharedMonoBehaviour.cs" />
Expand Down
73 changes: 73 additions & 0 deletions Utilities/JSONFormatter.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 7b89ab1

Please sign in to comment.