Skip to content

Commit

Permalink
Fix websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
PintTheDragon committed Oct 23, 2020
1 parent 08922c4 commit f84171c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
68 changes: 49 additions & 19 deletions SCPStats/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ internal class EventHandler
private static List<string> Players = new List<string>();
private static bool Pinged = true;

internal static bool Exited = false;
internal static Websocket ws = null;
internal static Task Pinger = null;
private static bool Exited = false;
private static Websocket ws = null;
private static Task Pinger = null;
private static bool PingerActive = false;

private static bool ConnectGrace = false;
private static List<string> Queue = new List<string>();

internal static void Reset()
{
ws?.Close(false);

ws = new Websocket("wss://scpstats.com/plugin");
ws = null;

Exited = false;
Pinged = false;
Pinged = true;
Queue = new List<string>();
ConnectGrace = false;
}

internal static void Start()
{
CreateConnection();
}

private static string DictToString(Dictionary<string, string> dict)
Expand Down Expand Up @@ -61,12 +71,9 @@ private static string HandleId(string id)
private static async Task CreateConnection()
{
Pinged = false;

Log.Info("Creating websocket");


if (Exited)
{
Log.Info("Disposing websocket");
ws?.Close(false);
SCPStats.Singleton.OnDisabled();
return;
Expand Down Expand Up @@ -110,7 +117,6 @@ private static async Task CreateConnection()

ws.OnClose = () =>
{
Log.Info("Socket closed");
CreateConnection();
};

Expand All @@ -121,34 +127,46 @@ private static async Task CreateConnection()
Log.Error(e);
}

Log.Info("Websocket connected");

await Task.Delay(500);

if (!PingerActive)
{
Pinger = Ping();
PingerActive = true;
}

if(Pinger?.Status != TaskStatus.Running) Pinger = Ping();
foreach (var s in Queue)
{
ws?.Send(s);
}

Queue = new List<string>();
}


private static async Task Ping()
{
Log.Info("Pinger created");

while (ws.ws.State == WebSocketState.Open)
{
if (Pinged)
{
Log.Info("Ping failed");
CreateConnection();
return;
}

Pinged = true;

Log.Info("Pinging");

ws?.Send("b");
await Task.Delay(10000);
}

PingerActive = false;
}

private static async Task UnGrace()
{
await Task.Delay(10000);
ConnectGrace = false;
}

private static async Task SendRequest(string type, Dictionary<string, string> data)
Expand All @@ -159,9 +177,21 @@ private static async Task SendRequest(string type, Dictionary<string, string> da
SCPStats.Singleton.OnDisabled();
return;
}

if (ConnectGrace && (ws == null || ws.ws.State != WebSocketState.Open))
{
var str1 = type+(data != null ? DictToString(data) : "");

var message1 = "p" + SCPStats.Singleton.Config.ServerId + str1.Length.ToString() + " " + str1 + HmacSha256Digest(SCPStats.Singleton.Config.Secret, str1);

Queue.Add(message1);
return;
}

if (ws == null || ws.ws.State != WebSocketState.Open)
{
ConnectGrace = true;
UnGrace();
await CreateConnection();
}

Expand Down
4 changes: 2 additions & 2 deletions SCPStats/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,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("1.0.3")]
[assembly: AssemblyFileVersion("1.0.3")]
[assembly: AssemblyVersion("1.0.4")]
[assembly: AssemblyFileVersion("1.0.4")]
4 changes: 3 additions & 1 deletion SCPStats/SCPStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class SCPStats : Plugin<Config>
{
public override string Name { get; } = "ScpStats";
public override string Author { get; } = "PintTheDragon";
public override Version Version { get; } = new Version(1, 0, 3);
public override Version Version { get; } = new Version(1, 0, 4);
public override PluginPriority Priority { get; } = PluginPriority.Last;

internal static SCPStats Singleton;
Expand All @@ -25,6 +25,8 @@ public override void OnEnabled()
base.OnDisabled();
return;
}

EventHandler.Start();

Exiled.Events.Handlers.Server.RoundStarted += EventHandler.OnRoundStart;
Exiled.Events.Handlers.Server.RoundEnded += EventHandler.OnRoundEnd;
Expand Down

0 comments on commit f84171c

Please sign in to comment.