PluralRichPresence/PluralRichPresnce/SimplyPlural/Socket.cs

122 lines
3.4 KiB
C#
Raw Normal View History

2023-12-28 10:28:49 +00:00
using Newtonsoft.Json;
2024-03-15 06:08:47 +00:00
using PluralRichPresence.Api;
2023-10-17 08:04:18 +00:00
using System.Dynamic;
using System.Text;
namespace PluralRichPresence.SimplyPlural
{
public class Socket : ApiType
{
2024-03-15 06:08:47 +00:00
private AWebSocket? wSock;
2023-10-18 01:14:47 +00:00
public const int KEEPALIVE_INTERVAL = 10 * 1000;
2023-12-28 10:28:49 +00:00
public const string DEFAULT_WEBSOCKET_SERVER_URI = "wss://api.apparyllis.com/v1/socket";
public event EventHandler? FronterChanged;
2023-10-17 08:04:18 +00:00
Timer? keepAliveTimer = null;
2023-12-28 10:28:49 +00:00
2023-10-17 08:04:18 +00:00
private void onFronterChanged(dynamic fronterChangedEventData)
{
if(FronterChanged is not null)
{
FronterChanged(this, new EventArgs());
}
}
2023-12-28 10:28:49 +00:00
private void doUpdate(dynamic jsonData)
2023-10-17 08:04:18 +00:00
{
string target = jsonData.target;
switch (target)
{
case "frontHistory":
onFronterChanged(jsonData);
break;
}
}
2023-10-18 01:14:47 +00:00
private async Task reconnect()
{
2023-11-05 06:28:08 +00:00
2023-10-18 01:14:47 +00:00
while (true)
{
try
{
try
{
2024-03-15 06:08:47 +00:00
if (wSock is not null) wSock.Dispose();
wSock = null;
2023-10-18 01:14:47 +00:00
}
catch (Exception) { };
await connect();
await sendLogin();
2023-10-17 08:04:18 +00:00
2023-10-18 01:14:47 +00:00
this.keepAliveTimer = new Timer((TimerCallback) => { _ = sendKeepAlive(); }, null, KEEPALIVE_INTERVAL, 0);
}
2023-12-28 10:28:49 +00:00
catch (Exception) { Logger.Debug("failed to connect."); continue; }
2023-10-18 01:14:47 +00:00
break;
}
2023-11-05 06:28:08 +00:00
2023-10-18 01:14:47 +00:00
}
2023-10-17 08:04:18 +00:00
private async Task connect()
{
2024-03-15 06:08:47 +00:00
wSock = new AWebSocket(Config.GetEntry("SIMPLY_PLURAL_WEBSOCKET_URI", DEFAULT_WEBSOCKET_SERVER_URI));
await wSock.Connect();
wSock.TextReceived += wSockTextReceived;
wSock.Disconnected += wSockDisconnected;
2023-10-17 08:04:18 +00:00
}
2024-03-15 06:08:47 +00:00
private void wSockTextReceived(object? sender, TextReceivedEventArgs e)
2023-10-17 08:04:18 +00:00
{
2024-03-15 06:08:47 +00:00
string message = e.Text;
if (message == "pong") return;
2023-11-05 06:28:08 +00:00
try
{
2024-03-15 06:08:47 +00:00
dynamic? jsonData = JsonConvert.DeserializeObject(message);
if (jsonData is null) return;
string? type = jsonData.msg;
switch (type)
{
case "update":
doUpdate(jsonData);
break;
case null:
default:
break;
}
2023-11-05 06:28:08 +00:00
}
2024-03-15 06:08:47 +00:00
catch (Exception) { };
}
private void wSockDisconnected(object? sender, EventArgs e)
{
_ = reconnect();
2023-10-17 08:04:18 +00:00
}
2024-03-15 06:08:47 +00:00
2023-10-17 08:04:18 +00:00
private async Task sendKeepAlive()
{
2024-03-15 06:08:47 +00:00
if (wSock is not null) {
await wSock.SendText("ping");
}
2023-10-18 01:14:47 +00:00
if (keepAliveTimer is not null) keepAliveTimer.Change(KEEPALIVE_INTERVAL, 0);
2023-10-17 08:04:18 +00:00
}
private async Task sendLogin()
{
dynamic data = new ExpandoObject();
data.op = "authenticate";
data.token = token;
string authenticateJson = JsonConvert.SerializeObject(data);
2024-03-15 06:08:47 +00:00
if(wSock is not null)
await wSock.SendText(authenticateJson);
2023-10-17 08:04:18 +00:00
}
public Socket(string token) : base(token)
{
2023-10-18 01:14:47 +00:00
_ = reconnect();
2023-10-17 08:04:18 +00:00
}
}
}