Skip to content

Commit a2a05c4

Browse files
committed
better reconnection handling
1 parent e5ac67e commit a2a05c4

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

ServerConnect.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,18 @@ public ServerConnect(Settings settings, ToolStripItemCollection menuItems)
2424
{
2525
options.Headers.Add(_settings.KeyName, _settings.ApiKey);
2626
})
27-
.WithAutomaticReconnect()
2827
.Build();
2928
}
3029

31-
32-
3330
public async Task Connect()
3431
{
3532

36-
connection.Closed += async (error) =>
33+
connection.Closed += async error =>
3734
{
3835
await Task.Delay(new Random().Next(0, 5) * 1000);
39-
await connection.StartAsync();
36+
await ConnectWithRetryAsync();
4037
};
41-
connection.Reconnected += async (_) =>
38+
connection.Reconnected += async error =>
4239
{
4340
await JoinGroup(_settings.Location);
4441
};
@@ -80,6 +77,24 @@ public async Task Connect()
8077
MessageBox.Show(e.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
8178
Application.Exit();
8279
}
80+
81+
82+
async Task<bool> ConnectWithRetryAsync()
83+
{
84+
while (true)
85+
{
86+
try
87+
{
88+
await connection.StartAsync();
89+
return true;
90+
}
91+
92+
catch
93+
{
94+
await Task.Delay(5000);
95+
}
96+
}
97+
}
8398
}
8499

85100

0 commit comments

Comments
 (0)