Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
389 lines (331 loc) · 8.38 KB
/
Copy pathclient.go
File metadata and controls
389 lines (331 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package onebot
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"sync"
"sync/atomic"
"time"
"github.com/coder/websocket"
"github.com/google/uuid"
)
// Client OneBot 12 客户端
type Client struct {
// 配置
url string
accessToken string
self *Self
logger *slog.Logger
reconnect bool
reconnectWait time.Duration
heartbeat time.Duration
timeout time.Duration
// 运行时状态
conn *websocket.Conn
mu sync.RWMutex
closed atomic.Bool
connected atomic.Bool
// 事件处理
eventHandlers map[string][]EventHandler
handlerMu sync.RWMutex
// 动作处理
actionChan chan *actionCall
responseChan map[string]chan *ActionResponse
responseMu sync.RWMutex
// 上下文
ctx context.Context
cancel context.CancelFunc
}
// EventHandler 事件处理函数
type EventHandler func(event any)
// actionCall 内部动作调用结构
type actionCall struct {
request *ActionRequest
response chan *ActionResponse
}
// New 创建新的 OneBot 客户端
func New(url string, opts ...Option) (*Client, error) {
ctx, cancel := context.WithCancel(context.Background())
c := &Client{
url: url,
logger: slog.Default(),
reconnect: true,
reconnectWait: 5 * time.Second,
heartbeat: 30 * time.Second,
timeout: 30 * time.Second,
eventHandlers: make(map[string][]EventHandler),
actionChan: make(chan *actionCall, 100),
responseChan: make(map[string]chan *ActionResponse),
ctx: ctx,
cancel: cancel,
}
// 应用选项
for _, opt := range opts {
opt(c)
}
return c, nil
}
// Connect 连接到 OneBot 实现
func (c *Client) Connect() error {
return c.connect()
}
// connect 内部连接方法
func (c *Client) connect() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn != nil {
c.conn.CloseNow()
}
// 设置请求头
opts := &websocket.DialOptions{
HTTPHeader: make(map[string][]string),
}
if c.accessToken != "" {
opts.HTTPHeader["Authorization"] = []string{"Bearer " + c.accessToken}
}
// 连接 WebSocket
conn, _, err := websocket.Dial(c.ctx, c.url, opts)
if err != nil {
return fmt.Errorf("连接 WebSocket 失败: %w", err)
}
// 取消读取限制
conn.SetReadLimit(-1)
c.conn = conn
c.connected.Store(true)
c.logger.Info("已连接到 OneBot 实现", "url", c.url)
// 启动读写协程
go c.readLoop()
go c.writeLoop()
return nil
}
// readLoop 读取消息循环
func (c *Client) readLoop() {
defer func() {
c.connected.Store(false)
c.handleDisconnect()
}()
for {
_, data, err := c.conn.Read(c.ctx)
if err != nil {
if !c.closed.Load() {
c.logger.Error("读取消息失败", "error", err)
}
return
}
// 尝试解析为动作响应
var response ActionResponse
if err := json.Unmarshal(data, &response); err == nil && response.Echo != "" {
c.handleActionResponse(&response)
continue
}
// 解析为事件
event, err := ParseEvent(data)
if err != nil {
c.logger.Error("解析事件失败", "error", err, "data", string(data))
continue
}
c.handleEvent(event)
}
}
// writeLoop 写入消息循环
func (c *Client) writeLoop() {
ticker := time.NewTicker(c.heartbeat)
defer ticker.Stop()
for {
select {
case <-c.ctx.Done():
return
case <-ticker.C:
// 发送心跳(如果需要)
// OneBot 12 标准中心跳是通过元事件推送的,不需要主动发送
case call := <-c.actionChan:
// 设置 echo
if call.request.Echo == "" {
call.request.Echo = uuid.New().String()
}
// 注册响应通道
c.responseMu.Lock()
c.responseChan[call.request.Echo] = call.response
c.responseMu.Unlock()
// 发送请求
data, err := json.Marshal(call.request)
if err != nil {
c.logger.Error("序列化动作请求失败", "error", err)
call.response <- &ActionResponse{
Status: "failed",
Retcode: RetcodeBadRequest,
Message: err.Error(),
}
continue
}
c.mu.RLock()
conn := c.conn
c.mu.RUnlock()
if conn != nil {
err = conn.Write(c.ctx, websocket.MessageText, data)
if err != nil {
c.logger.Error("发送动作请求失败", "error", err)
call.response <- &ActionResponse{
Status: "failed",
Retcode: RetcodeInternalHandlerError,
Message: err.Error(),
}
} else {
c.logger.Debug("发送动作请求", "action", call.request.Action)
}
}
}
}
}
// handleEvent 处理事件
func (c *Client) handleEvent(event any) {
c.handlerMu.RLock()
defer c.handlerMu.RUnlock()
// 获取事件类型
var eventType string
switch e := event.(type) {
case *MessageEvent:
eventType = "message"
if e.DetailType != "" {
eventType = "message." + e.DetailType
}
c.logger.Info("收到消息事件",
"type", e.DetailType,
"user_id", e.UserID,
"message", e.AltMessage)
case *NoticeEvent:
eventType = "notice"
if e.DetailType != "" {
eventType = "notice." + e.DetailType
}
c.logger.Info("收到通知事件", "type", e.DetailType)
case *MetaEvent:
eventType = "meta"
if e.DetailType != "" {
eventType = "meta." + e.DetailType
}
c.logger.Debug("收到元事件", "type", e.DetailType)
case *RequestEvent:
eventType = "request"
if e.DetailType != "" {
eventType = "request." + e.DetailType
}
c.logger.Info("收到请求事件", "type", e.DetailType)
case *Event:
eventType = e.Type
if e.DetailType != "" {
eventType = e.Type + "." + e.DetailType
}
c.logger.Info("收到事件", "type", eventType)
}
// 调用通用处理器
if handlers, ok := c.eventHandlers["*"]; ok {
for _, handler := range handlers {
go handler(event)
}
}
// 调用类型处理器
if handlers, ok := c.eventHandlers[eventType]; ok {
for _, handler := range handlers {
go handler(event)
}
}
}
// handleActionResponse 处理动作响应
func (c *Client) handleActionResponse(response *ActionResponse) {
c.responseMu.Lock()
ch, ok := c.responseChan[response.Echo]
if ok {
delete(c.responseChan, response.Echo)
}
c.responseMu.Unlock()
if ok && ch != nil {
c.logger.Debug("收到动作响应",
"echo", response.Echo,
"status", response.Status,
"retcode", response.Retcode)
select {
case ch <- response:
case <-time.After(time.Second):
c.logger.Error("发送响应到通道超时", "echo", response.Echo)
}
} else {
c.logger.Warn("收到未知的动作响应", "echo", response.Echo)
}
}
// handleDisconnect 处理断开连接
func (c *Client) handleDisconnect() {
if c.closed.Load() {
return
}
c.logger.Warn("WebSocket 连接断开")
if c.reconnect {
go func() {
for !c.closed.Load() {
c.logger.Info("尝试重连...", "wait", c.reconnectWait)
time.Sleep(c.reconnectWait)
if err := c.connect(); err != nil {
c.logger.Error("重连失败", "error", err)
} else {
break
}
}
}()
}
}
// Close 关闭客户端
func (c *Client) Close() error {
c.closed.Store(true)
c.cancel()
c.mu.Lock()
defer c.mu.Unlock()
if c.conn != nil {
return c.conn.Close(websocket.StatusNormalClosure, "客户端关闭")
}
return nil
}
// IsConnected 检查是否已连接
func (c *Client) IsConnected() bool {
return c.connected.Load()
}
// On 注册事件处理器
// eventType 可以是 "*"(所有事件)、"message"(所有消息)、"message.private"(私聊消息)等
func (c *Client) On(eventType string, handler EventHandler) {
c.handlerMu.Lock()
defer c.handlerMu.Unlock()
c.eventHandlers[eventType] = append(c.eventHandlers[eventType], handler)
}
// Call 调用动作
func (c *Client) Call(action string, params map[string]any) (*ActionResponse, error) {
return c.CallWithTimeout(action, params, c.timeout)
}
// CallWithTimeout 调用动作(带超时)
func (c *Client) CallWithTimeout(action string, params map[string]any, timeout time.Duration) (*ActionResponse, error) {
if !c.IsConnected() {
return nil, fmt.Errorf("未连接到 OneBot 实现")
}
request := NewActionRequest(action, params)
if c.self != nil {
request.WithSelf(c.self)
}
call := &actionCall{
request: request,
response: make(chan *ActionResponse, 1),
}
select {
case c.actionChan <- call:
case <-time.After(timeout):
return nil, fmt.Errorf("发送动作请求超时")
}
select {
case response := <-call.response:
return response, nil
case <-time.After(timeout):
// 清理响应通道
c.responseMu.Lock()
delete(c.responseChan, request.Echo)
c.responseMu.Unlock()
return nil, fmt.Errorf("等待动作响应超时")
}
}