Add auto reconnect on fail
This commit is contained in:
parent
f67fdbe537
commit
0f9f0c8d9c
6
PluralRichPresnce/PluralRichPresence.csproj.user
Normal file
6
PluralRichPresnce/PluralRichPresence.csproj.user
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<_LastSelectedProfileId>C:\Users\Li\Documents\git\PluralRichPresence\PluralRichPresnce\Properties\PublishProfiles\FolderProfile.pubxml</_LastSelectedProfileId>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -13,7 +13,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
|
|||
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
|
||||
<SelfContained>true</SelfContained>
|
||||
<PublishSingleFile>true</PublishSingleFile>
|
||||
<PublishReadyToRun>true</PublishReadyToRun>
|
||||
<PublishTrimmed>true</PublishTrimmed>
|
||||
<PublishReadyToRun>false</PublishReadyToRun>
|
||||
<PublishTrimmed>false</PublishTrimmed>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -3,4 +3,8 @@
|
|||
https://go.microsoft.com/fwlink/?LinkID=208121.
|
||||
-->
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<History>True|2023-10-18T01:13:41.8203499Z;True|2023-10-18T14:05:36.8381808+13:00;True|2023-10-17T21:30:38.9682339+13:00;</History>
|
||||
<LastFailureDetails />
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -12,9 +12,12 @@ namespace PluralRichPresence.SimplyPlural
|
|||
{
|
||||
public class Socket : ApiType
|
||||
{
|
||||
public const int KEEPALIVE_INTERVAL = 10 * 1000;
|
||||
public const string WEBSOCKET_SERVER_URI = "wss://api.apparyllis.com/v1/socket";
|
||||
public event EventHandler FronterChanged;
|
||||
ClientWebSocket wss = new ClientWebSocket();
|
||||
ClientWebSocket? wss = null;
|
||||
Timer? keepAliveTimer = null;
|
||||
|
||||
|
||||
private void onFronterChanged(dynamic fronterChangedEventData)
|
||||
{
|
||||
|
@ -33,15 +36,19 @@ namespace PluralRichPresence.SimplyPlural
|
|||
List<byte> totalPayload = new List<byte>();
|
||||
byte[] buffer = new byte[0x8000];
|
||||
|
||||
while (wss.State == WebSocketState.Open)
|
||||
while (wss is not null && wss.State == WebSocketState.Open)
|
||||
{
|
||||
WebSocketReceiveResult result = await wss.ReceiveAsync(buffer, CancellationToken.None);
|
||||
try
|
||||
{
|
||||
WebSocketReceiveResult? result = await wss.ReceiveAsync(buffer, CancellationToken.None);
|
||||
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
totalPayload.Add(buffer[i]);
|
||||
|
||||
for (int i = 0; i < result.Count; i++)
|
||||
totalPayload.Add(buffer[i]);
|
||||
|
||||
if (result.EndOfMessage)
|
||||
return totalPayload.ToArray();
|
||||
if (result.EndOfMessage)
|
||||
return totalPayload.ToArray();
|
||||
}
|
||||
catch(Exception) { await reconnect(); break; };
|
||||
}
|
||||
|
||||
return totalPayload.ToArray();
|
||||
|
@ -60,44 +67,69 @@ namespace PluralRichPresence.SimplyPlural
|
|||
|
||||
private async Task receiveTask()
|
||||
{
|
||||
while (wss.State == WebSocketState.Open)
|
||||
while (wss is not null && wss.State == WebSocketState.Open)
|
||||
{
|
||||
string message = await receiveMessageText();
|
||||
if (message == "pong") continue;
|
||||
Console.WriteLine("< " + message);
|
||||
|
||||
try
|
||||
{
|
||||
dynamic? jsonData = JsonConvert.DeserializeObject(message);
|
||||
if (jsonData is null) continue;
|
||||
string type = jsonData.msg;
|
||||
if(type == "update")
|
||||
string message = await receiveMessageText();
|
||||
Console.WriteLine("< " + message);
|
||||
if (message == "pong") continue;
|
||||
|
||||
try
|
||||
{
|
||||
await doUpdate(jsonData);
|
||||
dynamic? jsonData = JsonConvert.DeserializeObject(message);
|
||||
if (jsonData is null) continue;
|
||||
string type = jsonData.msg;
|
||||
if (type == "update")
|
||||
await doUpdate(jsonData);
|
||||
}
|
||||
catch (Exception) { };
|
||||
}
|
||||
catch (Exception) { };
|
||||
catch (Exception) { Console.WriteLine("failed"); break; };
|
||||
}
|
||||
}
|
||||
private async Task reconnect()
|
||||
{
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
if (wss is not null) await wss.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
|
||||
wss = null;
|
||||
}
|
||||
catch (Exception) { };
|
||||
await connect();
|
||||
await sendLogin();
|
||||
|
||||
_ = Task.Run(() => receiveTask());
|
||||
this.keepAliveTimer = new Timer((TimerCallback) => { _ = sendKeepAlive(); }, null, KEEPALIVE_INTERVAL, 0);
|
||||
}
|
||||
catch (Exception) { Console.WriteLine("failed to connect."); continue; }
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
private async Task connect()
|
||||
{
|
||||
await wss.ConnectAsync(new Uri("wss://api.apparyllis.com/v1/socket"), CancellationToken.None);
|
||||
_ = receiveTask();
|
||||
keepAliveTimer = new Timer((TimerCallback) => { _ = sendKeepAlive(); }, null, 0, 10 * 1000);
|
||||
Console.WriteLine("Connecting to " + WEBSOCKET_SERVER_URI);
|
||||
wss = new ClientWebSocket();
|
||||
await wss.ConnectAsync(new Uri(WEBSOCKET_SERVER_URI), CancellationToken.None);
|
||||
}
|
||||
|
||||
private async Task sendText(string text)
|
||||
{
|
||||
if (wss.State != WebSocketState.Open)
|
||||
await connect();
|
||||
|
||||
Console.WriteLine("> " + text);
|
||||
Console.WriteLine("> "+text);
|
||||
await wss.SendAsync(Encoding.UTF8.GetBytes(text), WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
private async Task sendKeepAlive()
|
||||
{
|
||||
await sendText("ping");
|
||||
if (keepAliveTimer is not null) keepAliveTimer.Change(KEEPALIVE_INTERVAL, 0);
|
||||
|
||||
}
|
||||
private async Task sendLogin()
|
||||
{
|
||||
|
@ -110,7 +142,7 @@ namespace PluralRichPresence.SimplyPlural
|
|||
|
||||
public Socket(string token) : base(token)
|
||||
{
|
||||
_ = sendLogin();
|
||||
_ = reconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user