Skip to content

Commit

Permalink
Merge pull request #51 from androidseb25/dev
Browse files Browse the repository at this point in the history
PR: WebSocket Connection Issue #50
  • Loading branch information
androidseb25 authored Apr 15, 2024
2 parents 936ae34 + 2eaf746 commit 307b1ea
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
36 changes: 33 additions & 3 deletions Services/GotifySocketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,33 @@ public async void Start()

List<Users> userList = await DatabaseService.GetUsers();

StartConnection(userList, secntfyUrl);
}

private void StartConnection(List<Users> userList, string secntfyUrl)
{
foreach (Users user in userList)
{
string isGotifyAvailable = SecNtfy.CheckIfUrlReachable(user.GotifyUrl) ? "yes" : "no";
string isSecNtfyAvailable = SecNtfy.CheckIfUrlReachable(secntfyUrl) ? "yes" : "no";

string isGotifyAvailable = "";
string isSecNtfyAvailable = "";
try
{
isGotifyAvailable = SecNtfy.CheckIfUrlReachable(user.GotifyUrl) ? "yes" : "no";

if (isGotifyAvailable == "no")
{
StartConnection(userList, secntfyUrl);
return;
}

isSecNtfyAvailable = SecNtfy.CheckIfUrlReachable(secntfyUrl) ? "yes" : "no";
}
catch
{
StartDelayedConnection(userList, secntfyUrl);
return;
}

Console.WriteLine($"Gotify - Url: {user.GotifyUrl}");
Console.WriteLine($"Is Gotify - Url available: {isGotifyAvailable}");
Console.WriteLine($"SecNtfy Server - Url: {secntfyUrl}");
Expand All @@ -110,4 +132,12 @@ public async void Start()
StartWSThread(user.GotifyUrl, user.ClientToken);
}
}

private async void StartDelayedConnection(List<Users> userList, string secntfyUrl)
{
Console.WriteLine("Gotify Server is not available try to reconnect in 10s.");
await Task.Delay(10000);
Console.WriteLine("Reconnecting...");
StartConnection(userList, secntfyUrl);
}
}
33 changes: 26 additions & 7 deletions Services/WebSockClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,39 @@ public class WebSockClient

private WebsocketClient? ws = null;

public void Start(string clientToken)
public void Start(string clientToken, bool isRestart = false)
{
int connectionCount = 1;
if (URL != null && URL.Length == 0)
throw new ApplicationException("URL is empty!");

// Init WebSocket
ws = new WebsocketClient(new Uri(URL!));
ws.Name = clientToken;
ws.IsReconnectionEnabled = false;

ws.ReconnectionHappened.Subscribe(info =>
{
//Console.WriteLine($"ReconnectionHappened {info.Type}");
if (info.Type == ReconnectionType.Initial && isRestart)
{
Console.WriteLine($"Gotify with Clienttoken: \"{clientToken}\" is successfully reconnected!");
}
});

// When a disconnection happend try to reconnect the WebSocket
ws.DisconnectionHappened.Subscribe(type =>
{
connectionCount -= 1;
Console.WriteLine($"Disconnection happened, type: {type}");
string wsName = ws.Name;
Console.WriteLine($"Disconnection happened, type: {type.Type}");
if (type.Type == DisconnectionType.Lost)
{
Console.WriteLine($"Connection lost reconnect to Websocket...");
string wsName = ws.Name;
Stop();
Start(wsName);
Start(wsName, true);
} else if (type.Type == DisconnectionType.Error)
{
Console.WriteLine($"Webseocket Reconnection failed with Error. Try to reconnect in 10s.");
ReconnectDelayed(wsName);
}
});

Expand Down Expand Up @@ -61,7 +72,8 @@ public void Start(string clientToken)
.Concat() // executes sequentially
.Subscribe();
ws.Start();
Console.WriteLine("Done!");
if (!isRestart)
Console.WriteLine("Done!");
}

/// <summary>
Expand All @@ -73,4 +85,11 @@ public async void Stop()
ws = null;
await Task.Delay(1000);
}

public async void ReconnectDelayed(string clientToken)
{
ws = null;
await Task.Delay(10000);
Start(clientToken, true);
}
}
8 changes: 4 additions & 4 deletions iGotify Notification Assist.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.37" />
<PackageReference Include="Dapper" Version="2.1.44" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="Dapper.Mapper" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="secntfy.nuget" Version="1.0.3" />
<PackageReference Include="secntfy.nuget" Version="1.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Websocket.Client" Version="5.1.1" />
</ItemGroup>
Expand Down

0 comments on commit 307b1ea

Please sign in to comment.