mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2026-04-05 14:35:43 +00:00
Fix ReactorError not correctly raised. Added Program_ReactorError example
This commit is contained in:
parent
e8b0bb9245
commit
131fd36106
7 changed files with 81 additions and 10 deletions
71
Examples/Program_ReactorError.cs
Normal file
71
Examples/Program_ReactorError.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Telegram.Bot.Types;
|
||||
using TL;
|
||||
|
||||
namespace WTelegramClientTest
|
||||
{
|
||||
static class Program_ReactorError
|
||||
{
|
||||
static WTelegram.Client Client;
|
||||
|
||||
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
|
||||
static async Task Main(string[] _)
|
||||
{
|
||||
Console.WriteLine("The program demonstrate how to handle ReactorError. Press any key to terminate");
|
||||
WTelegram.Helpers.Log = (l, s) => System.Diagnostics.Debug.WriteLine(s);
|
||||
try
|
||||
{
|
||||
await CreateAndConnect();
|
||||
Console.ReadKey();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Client?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task CreateAndConnect()
|
||||
{
|
||||
Client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
||||
Client.OnUpdate += Client_OnUpdate;
|
||||
Client.OnOther += Client_OnOther;
|
||||
var my = await Client.LoginUserIfNeeded();
|
||||
Console.WriteLine($"We are logged-in as " + my);
|
||||
}
|
||||
|
||||
private static async Task Client_OnOther(IObject arg)
|
||||
{
|
||||
if (arg is ReactorError err)
|
||||
{
|
||||
// typically: network connection was totally lost
|
||||
Console.WriteLine($"Fatal reactor error: {err.Exception.Message}");
|
||||
while (true)
|
||||
{
|
||||
Console.WriteLine("Disposing the client and trying to reconnect in 5 seconds...");
|
||||
Client?.Dispose();
|
||||
Client = null;
|
||||
await Task.Delay(5000);
|
||||
try
|
||||
{
|
||||
await CreateAndConnect();
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Connection still failing: " + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
Console.WriteLine("Other: " + arg.GetType().Name);
|
||||
}
|
||||
|
||||
private static Task Client_OnUpdate(UpdatesBase updates)
|
||||
{
|
||||
foreach (var update in updates.UpdateList)
|
||||
Console.WriteLine(update.GetType().Name);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue