OrangeGuidanceTomestone/client/Pinger.cs

52 lines
1.2 KiB
C#
Raw Normal View History

2022-09-06 12:07:23 +00:00
using System.Diagnostics;
2023-09-29 00:53:50 +00:00
using Dalamud.Plugin.Services;
2022-09-06 12:07:23 +00:00
using OrangeGuidanceTomestone.Helpers;
namespace OrangeGuidanceTomestone;
internal class Pinger : IDisposable {
private Plugin Plugin { get; }
private Stopwatch Stopwatch { get; } = new();
private int _waitSecs;
internal Pinger(Plugin plugin) {
this.Plugin = plugin;
this.Stopwatch.Start();
this.Plugin.Framework.Update += this.Ping;
}
public void Dispose() {
this.Plugin.Framework.Update -= this.Ping;
}
2023-09-29 00:53:50 +00:00
private void Ping(IFramework framework) {
2022-09-06 12:07:23 +00:00
if (this.Stopwatch.Elapsed < TimeSpan.FromSeconds(this._waitSecs)) {
return;
}
this.Stopwatch.Restart();
if (this.Plugin.Config.ApiKey == string.Empty) {
this._waitSecs = 5;
return;
}
// 30 mins
this._waitSecs = 1_800;
Task.Run(async () => {
var resp = await ServerHelper.SendRequest(
this.Plugin.Config.ApiKey,
HttpMethod.Post,
"/ping"
);
if (!resp.IsSuccessStatusCode) {
2023-09-29 00:53:50 +00:00
Plugin.Log.Warning($"Failed to ping, status {resp.StatusCode}");
2022-09-06 12:07:23 +00:00
}
});
}
}