Files
better-genshin-impact/BetterGenshinImpact/Service/Notifier/WebSocketNotifier.cs
禹仔二号 6becb9dd5a 添加websocket和smtp通知方法,添加bark通知,websocket为qq机器人预留。 (#1254)
* feat: add email and websocket notification

* fix typos.

* refactor(notifiers): 重构邮件和 WebSocket 通知器

- 提升 EmailNotifier 中的 SmtpClient 为类的成员变量,并在构造函数中初始化,优化资源使用
- 改进 WebSocketNotifier 的连接和重连逻辑,提高稳定性
- 优化通知器的错误处理和日志记录,增强可维护性

* Create BarkNotifier.cs

* Update NotificationConfig.cs

* Update NotificationService.cs

* Update CommonSettingsPageViewModel.cs

* Add files via upload

* Add files via upload

* Add files via upload

* Update CommonSettingsPage.xaml

* Delete BetterGenshinImpact/Service/Notifier/BarkNotifier.cs

* Add files via upload

* Update BarkNotifier.cs

* Update NotificationService.cs

* Update CommonSettingsPageViewModel.cs

* Update CommonSettingsPage.xaml

* Delete BetterGenshinImpact/Service/BarkNotifier.cs

* Add files via upload

* Add files via upload

* Add files via upload

* Update CommonSettingsPage.xaml

* fix: 回退部分代码,修复了程序崩溃的错误

* fix: remove api.day.app

---------

Co-authored-by: 秋云 <physligl@gmail.com>
Co-authored-by: DR-lin-eng <52230594+DR-lin-eng@users.noreply.github.com>
2025-03-13 00:06:09 +08:00

86 lines
2.8 KiB
C#

using System;
using System.Net.WebSockets;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using BetterGenshinImpact.Service.Notification.Model;
using BetterGenshinImpact.Service.Notifier.Interface;
namespace BetterGenshinImpact.Service.Notifier
{
public class WebSocketNotifier : IDisposable, INotifier
{
private ClientWebSocket _webSocket;
private readonly string _endpoint;
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly CancellationTokenSource _cts;
public WebSocketNotifier(string endpoint, JsonSerializerOptions jsonSerializerOptions, CancellationTokenSource cts)
{
_endpoint = endpoint;
_jsonSerializerOptions = jsonSerializerOptions;
_cts = cts;
_webSocket = new ClientWebSocket();
}
public string Name => "WebSocketNotifier";
private async Task EnsureConnectedAsync()
{
if (_webSocket.State == WebSocketState.Open)
return;
_webSocket.Dispose();
_webSocket = new ClientWebSocket();
try
{
Console.WriteLine("Connecting to WebSocket...");
await _webSocket.ConnectAsync(new Uri(_endpoint), _cts.Token);
Console.WriteLine("WebSocket connected.");
}
catch (SystemException ex)
{
Console.WriteLine($"WebSocket connection failed: {ex.Message}");
}
}
public async Task SendAsync(BaseNotificationData notificationData)
{
try
{
await EnsureConnectedAsync();
var json = JsonSerializer.Serialize(notificationData, _jsonSerializerOptions);
var buffer = System.Text.Encoding.UTF8.GetBytes(json);
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, _cts.Token);
}
catch (WebSocketException ex)
{
Console.WriteLine($"WebSocket send failed: {ex.Message}");
await EnsureConnectedAsync(); // Attempt to reconnect
}
}
public async Task CloseAsync()
{
if (_webSocket.State == WebSocketState.Open || _webSocket.State == WebSocketState.CloseReceived)
{
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", _cts.Token);
}
_webSocket.Dispose();
_webSocket = new ClientWebSocket();
}
public void Dispose()
{
_webSocket.Dispose();
_cts.Cancel();
}
public async Task SendNotificationAsync(BaseNotificationData notificationData)
{
await SendAsync(notificationData);
}
}
}