Skip to content

Commit 2eaf746

Browse files
committed
WebSocket Connection Issue #50
#50 - Fixed the connection Issue behavior, that sometimes the connection is losinig, now the container is waiting for a connection and retry it every 10s.
1 parent 936ae34 commit 2eaf746

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

Services/GotifySocketService.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,33 @@ public async void Start()
9696

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

99+
StartConnection(userList, secntfyUrl);
100+
}
101+
102+
private void StartConnection(List<Users> userList, string secntfyUrl)
103+
{
99104
foreach (Users user in userList)
100105
{
101-
string isGotifyAvailable = SecNtfy.CheckIfUrlReachable(user.GotifyUrl) ? "yes" : "no";
102-
string isSecNtfyAvailable = SecNtfy.CheckIfUrlReachable(secntfyUrl) ? "yes" : "no";
103-
106+
string isGotifyAvailable = "";
107+
string isSecNtfyAvailable = "";
108+
try
109+
{
110+
isGotifyAvailable = SecNtfy.CheckIfUrlReachable(user.GotifyUrl) ? "yes" : "no";
111+
112+
if (isGotifyAvailable == "no")
113+
{
114+
StartConnection(userList, secntfyUrl);
115+
return;
116+
}
117+
118+
isSecNtfyAvailable = SecNtfy.CheckIfUrlReachable(secntfyUrl) ? "yes" : "no";
119+
}
120+
catch
121+
{
122+
StartDelayedConnection(userList, secntfyUrl);
123+
return;
124+
}
125+
104126
Console.WriteLine($"Gotify - Url: {user.GotifyUrl}");
105127
Console.WriteLine($"Is Gotify - Url available: {isGotifyAvailable}");
106128
Console.WriteLine($"SecNtfy Server - Url: {secntfyUrl}");
@@ -110,4 +132,12 @@ public async void Start()
110132
StartWSThread(user.GotifyUrl, user.ClientToken);
111133
}
112134
}
135+
136+
private async void StartDelayedConnection(List<Users> userList, string secntfyUrl)
137+
{
138+
Console.WriteLine("Gotify Server is not available try to reconnect in 10s.");
139+
await Task.Delay(10000);
140+
Console.WriteLine("Reconnecting...");
141+
StartConnection(userList, secntfyUrl);
142+
}
113143
}

Services/WebSockClient.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,39 @@ public class WebSockClient
1212

1313
private WebsocketClient? ws = null;
1414

15-
public void Start(string clientToken)
15+
public void Start(string clientToken, bool isRestart = false)
1616
{
17-
int connectionCount = 1;
1817
if (URL != null && URL.Length == 0)
1918
throw new ApplicationException("URL is empty!");
2019

2120
// Init WebSocket
2221
ws = new WebsocketClient(new Uri(URL!));
2322
ws.Name = clientToken;
2423
ws.IsReconnectionEnabled = false;
24+
25+
ws.ReconnectionHappened.Subscribe(info =>
26+
{
27+
//Console.WriteLine($"ReconnectionHappened {info.Type}");
28+
if (info.Type == ReconnectionType.Initial && isRestart)
29+
{
30+
Console.WriteLine($"Gotify with Clienttoken: \"{clientToken}\" is successfully reconnected!");
31+
}
32+
});
2533

2634
// When a disconnection happend try to reconnect the WebSocket
2735
ws.DisconnectionHappened.Subscribe(type =>
2836
{
29-
connectionCount -= 1;
30-
Console.WriteLine($"Disconnection happened, type: {type}");
37+
string wsName = ws.Name;
38+
Console.WriteLine($"Disconnection happened, type: {type.Type}");
3139
if (type.Type == DisconnectionType.Lost)
3240
{
3341
Console.WriteLine($"Connection lost reconnect to Websocket...");
34-
string wsName = ws.Name;
3542
Stop();
36-
Start(wsName);
43+
Start(wsName, true);
44+
} else if (type.Type == DisconnectionType.Error)
45+
{
46+
Console.WriteLine($"Webseocket Reconnection failed with Error. Try to reconnect in 10s.");
47+
ReconnectDelayed(wsName);
3748
}
3849
});
3950

@@ -61,7 +72,8 @@ public void Start(string clientToken)
6172
.Concat() // executes sequentially
6273
.Subscribe();
6374
ws.Start();
64-
Console.WriteLine("Done!");
75+
if (!isRestart)
76+
Console.WriteLine("Done!");
6577
}
6678

6779
/// <summary>
@@ -73,4 +85,11 @@ public async void Stop()
7385
ws = null;
7486
await Task.Delay(1000);
7587
}
88+
89+
public async void ReconnectDelayed(string clientToken)
90+
{
91+
ws = null;
92+
await Task.Delay(10000);
93+
Start(clientToken, true);
94+
}
7695
}

iGotify Notification Assist.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Dapper" Version="2.1.37" />
15+
<PackageReference Include="Dapper" Version="2.1.44" />
1616
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
1717
<PackageReference Include="Dapper.Mapper" Version="2.0.0" />
18-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
19-
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.3" />
18+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
19+
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.4" />
2020
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
21-
<PackageReference Include="secntfy.nuget" Version="1.0.3" />
21+
<PackageReference Include="secntfy.nuget" Version="1.0.4" />
2222
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
2323
<PackageReference Include="Websocket.Client" Version="5.1.1" />
2424
</ItemGroup>

0 commit comments

Comments
 (0)