Files
better-genshin-impact/BetterGenshinImpact/Service/Notifier/WebSocketNotifier.cs
禹仔二号 f7903f8e9b webhook添加了sendto发送对象,修复了webhook通知ui显示错误问题,修复了websocket服务端错误关闭链接问题 (#1300)
* 新增图片作为email附件发送功能

* feat(notification): 为 WebhookNotifier 添加 send_to 功能

- 在 WebhookNotifier 类中添加 send_to 属性
- 修改 TransformData 方法,增加 send_to 字段
- 在 NotificationConfig 中添加 webhook_endpoint 和 websocket_endpoint 属性

* refactor(WebhookNotifier): 添加 send_to 属性并调整访问修饰符

* refactor(notification): 重构 WebhookNotifier 并更新 NotificationConfig

- 在 NotificationConfig 中添加 WebhookSendTo 属性
- 重构 WebhookNotifier 构造函数,使用 NotificationConfig 对象进行初始化
- 更新 WebhookNotifier 发送逻辑,使用新的配置属性

* feat: add webhook ui.

* refactor(WebhookNotifier): 重构 TransformData 方法以优化数据结构- 将 notification_data 的内容合并到外层字典,以便于 webhook 接收端获取数据- 新增 event、result、timestamp 等字段,使数据结构

* fix(WebSocketNotifier): 添加消息发送后关闭连接的功能

- 在 SendAsync 方法中添加了 CloseAsync() 调用,确保消息发送后关闭 WebSocket 连接
- 此修改解决了消息发送后未关闭连接的问题,提高了资源利用率和系统稳定性

* fix: implement unified webhook ui

* fix: typos.

---------

Co-authored-by: DR-lin-eng <@DR-lin-eng>
Co-authored-by: 秋云 <physligl@gmail.com>
2025-03-15 10:33:33 +08:00

86 lines
2.9 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);
await CloseAsync(); // 添加关闭连接的代码
}
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);
}
}
}