Mirai-NET-Shelter / Mirai.Net

Mirai.Net是基于mirai-api-http实现的轻量级mirai社区sdk。
GNU Affero General Public License v3.0
186 stars 26 forks source link

Support handling WebSocket lost connection. #18

Closed cyanray closed 2 years ago

cyanray commented 2 years ago

关于处理 WebSocket 掉线问题,#17 ,提供一个我目前正在使用的方案。

使用者可以通过订阅 DisconnectionHappened 处理 WebSocketClient 掉线事件。

修改后的使用方法:

            var connect = async () =>
            {
                while (true)
                {
                    try
                    {
                        Console.WriteLine("尝试建立连接...");
                        await bot.LaunchAsync();
                        Console.WriteLine("Bot working...");
                        break;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            };

            // 与 mah 建立连接
            await connect();

            // 处理失去连接事件
            bot.DisconnectionHappened
                .Subscribe(async status =>
                {
                    Console.WriteLine($"失去连接:{status}");
                    await connect();
                });