|
1 | | -using System.Net.WebSockets; |
2 | | -using System.Text; |
| 1 | +using HuaJiBot.NET.MQ; |
3 | 2 | using HuaJiBot.NET.Plugin.GitHubBridge.EventDispatch; |
4 | 3 | using HuaJiBot.NET.Plugin.GitHubBridge.Types; |
5 | 4 | using HuaJiBot.NET.Plugin.GitHubBridge.Types.IssueCommentEventBody; |
6 | 5 | using HuaJiBot.NET.Plugin.GitHubBridge.Types.IssuesEventBody; |
7 | 6 | using HuaJiBot.NET.Plugin.GitHubBridge.Types.PushEventBody; |
8 | 7 | using HuaJiBot.NET.Plugin.GitHubBridge.Types.WorkflowRunEventBody; |
9 | | -using Newtonsoft.Json; |
10 | | -using Newtonsoft.Json.Linq; |
11 | | -using Websocket.Client; |
12 | 8 |
|
13 | 9 | namespace HuaJiBot.NET.Plugin.GitHubBridge; |
14 | 10 |
|
@@ -50,118 +46,62 @@ public partial class PluginMain : PluginBase, IPluginWithConfig<PluginConfig> |
50 | 46 | //配置 |
51 | 47 | public PluginConfig Config { get; } = new(); |
52 | 48 |
|
53 | | - //处理消息 |
54 | | - private async Task ProcessMessageAsync(string msg) |
55 | | - { |
56 | | - var jsonObject = JObject.Parse(msg); |
57 | | - if (jsonObject.TryGetValue("type", out var pktTypeObj)) |
58 | | - { |
59 | | - var pktType = pktTypeObj.Value<string>(); |
60 | | - if (pktType == "active_clients_change") |
61 | | - { |
62 | | - var data = jsonObject["data"]!.Value<JArray>("clients")!; |
63 | | - var clients = data.Select(x => |
64 | | - x.Value<string>("address") |
65 | | - + "(" |
66 | | - + (x["headers"]?["Cf-Ipcountry"]?[0] ?? "?") |
67 | | - + ":" |
68 | | - + (x["headers"]?["X-Forwarded-For"]?[0] ?? "?") |
69 | | - + ")" |
70 | | - ) |
71 | | - .ToArray(); |
72 | | - Info("当前在线客户端:" + string.Join(", ", clients)); |
73 | | - } |
74 | | - else if (pktType == "webhook") |
75 | | - { |
76 | | - var e = jsonObject["data"]!.ToObject<Event>()!; |
77 | | - { |
78 | | - switch (e.Body) |
79 | | - { |
80 | | - case PushEventBody body: //推送事件 |
81 | | - await this.DispatchPushEventAsync(body); |
82 | | - break; |
83 | | - case IssuesEventBody body: //issue事件 |
84 | | - await this.DispatchIssuesEventAsync(body); |
85 | | - break; |
86 | | - case IssueCommentEventBody body: //issue comment事件 |
87 | | - await this.DispatchIssueCommentEventAsync(body); |
88 | | - break; |
89 | | - case WorkflowRunEventBody body: //actions构建事件 |
90 | | - await this.DispatchWorkflowRunEventAsync(body); |
91 | | - break; |
92 | | - case UnknownEventBody body: |
93 | | - Info("收到未实现的事件!" + e.Headers.XGithubEvent[0]); |
94 | | - break; |
95 | | - } |
96 | | - } |
97 | | - } |
98 | | - return; |
99 | | - } |
100 | | - } |
101 | | - |
102 | 49 | //初始化 |
103 | 50 | protected override async Task InitializeAsync() |
104 | 51 | { |
105 | 52 | ShortLinkApi = new ShortLinkApi(Config.ShortLinkToken); |
106 | | - WebsocketClient client = new( |
107 | | - new Uri(Config.Address), |
108 | | - () => |
| 53 | + ServerlessMQ client = new(Config.Address, Config.AuthBearer); |
| 54 | + client.OnWebhook += async data => |
| 55 | + { |
| 56 | + var e = data.ToObject<Event>()!; |
109 | 57 | { |
110 | | - var client = new ClientWebSocket |
| 58 | + switch (e.Body) |
111 | 59 | { |
112 | | - Options = { CollectHttpResponseDetails = true }, |
113 | | - }; |
114 | | - client.Options.SetRequestHeader("Authorization", $"Bearer {Config.AuthBearer}"); |
115 | | - return client; |
| 60 | + case PushEventBody body: //推送事件 |
| 61 | + await this.DispatchPushEventAsync(body); |
| 62 | + break; |
| 63 | + case IssuesEventBody body: //issue事件 |
| 64 | + await this.DispatchIssuesEventAsync(body); |
| 65 | + break; |
| 66 | + case IssueCommentEventBody body: //issue comment事件 |
| 67 | + await this.DispatchIssueCommentEventAsync(body); |
| 68 | + break; |
| 69 | + case WorkflowRunEventBody body: //actions构建事件 |
| 70 | + await this.DispatchWorkflowRunEventAsync(body); |
| 71 | + break; |
| 72 | + case UnknownEventBody body: |
| 73 | + Info("收到未实现的事件!" + e.Headers.XGithubEvent[0]); |
| 74 | + break; |
| 75 | + } |
116 | 76 | } |
117 | | - ) |
| 77 | + }; |
| 78 | + client.OnConnected += info => |
118 | 79 | { |
119 | | - IsReconnectionEnabled = true, |
120 | | - ReconnectTimeout = null, |
121 | | - MessageEncoding = Encoding.UTF8, |
122 | | - IsTextMessageConversionEnabled = true, |
| 80 | + var status = info.IsReconnect ? "重新连接成功" : "连接成功"; |
| 81 | + Info($"{status} - 时间: {info.Timestamp:yyyy-MM-dd HH:mm:ss}"); |
123 | 82 | }; |
124 | | - client.MessageReceived.Subscribe(msg => |
| 83 | + client.OnClosed += info => |
125 | 84 | { |
126 | | - if (msg.MessageType == WebSocketMessageType.Text) |
127 | | - { |
128 | | - try |
129 | | - { |
130 | | - ProcessMessageAsync(msg.Text ?? throw new NullReferenceException("msg.Text")) |
131 | | - .ContinueWith( |
132 | | - task => |
133 | | - { |
134 | | - var ex = task.Exception; |
135 | | - if (ex is not null) |
136 | | - Error("ProcessMessage 处理消息时出现异常:", ex); |
137 | | - if (msg.Text is { } raw) |
138 | | - Error("ProcessMessage 处理消息时出现异常Raw:", raw); |
139 | | - }, |
140 | | - TaskContinuationOptions.OnlyOnFaulted |
141 | | - ); |
142 | | - } |
143 | | - catch (Exception e) |
144 | | - { |
145 | | - Error("处理消息时出现异常:", e); |
146 | | - if (msg.Text is { } raw) |
147 | | - Error("处理消息时出现异常Raw:", raw); |
148 | | - } |
149 | | - } |
150 | | - else |
151 | | - { |
152 | | - Info("收到非文本消息!"); |
153 | | - } |
154 | | - }); |
155 | | - client.DisconnectionHappened.Subscribe(info => |
156 | 85 | Info( |
157 | | - "Disconnection Happened. Type:" |
158 | | - + info.Type |
159 | | - + " Description:" |
160 | | - + info.CloseStatusDescription |
161 | | - ) |
162 | | - ); |
163 | | - client.ReconnectionHappened.Subscribe(info => Info("Reconnection Happened " + info.Type)); |
164 | | - await client.Start(); |
| 86 | + $"连接断开 - 类型: {info.Type}, 原因: {info.Reason ?? "未知"}, 时间: {info.Timestamp:yyyy-MM-dd HH:mm:ss}" |
| 87 | + + (info.CloseStatus.HasValue ? $", 状态码: {info.CloseStatus}" : "") |
| 88 | + ); |
| 89 | + }; |
| 90 | + client.OnClientChanged += async clients => |
| 91 | + { |
| 92 | + var data = clients.Clients; |
| 93 | + var clientsStr = data.Select(x => |
| 94 | + x.Address |
| 95 | + + "(" |
| 96 | + + (x.Headers.GetValueOrDefault("Cf-Ipcountry")?[0] ?? "?") |
| 97 | + + ":" |
| 98 | + + (x.Headers.GetValueOrDefault("X-Forwarded-For")?[0] ?? "?") |
| 99 | + + ")" |
| 100 | + ) |
| 101 | + .ToArray(); |
| 102 | + Info("当前在线客户端:" + string.Join(", ", clientsStr)); |
| 103 | + }; |
| 104 | + |
165 | 105 | Info("启动成功!"); |
166 | 106 | } |
167 | 107 |
|
|
0 commit comments