-
Notifications
You must be signed in to change notification settings - Fork 5
/
payload.go
70 lines (62 loc) · 2.02 KB
/
payload.go
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
package nano
import (
"encoding/json"
"errors"
"strconv"
)
// WebsocketPayload payload 指的是在 websocket 连接上传输的数据,网关的上下行消息采用的都是同一个结构
//
// https://bot.q.qq.com/wiki/develop/api/gateway/reference.html
type WebsocketPayload struct {
Op OpCode `json:"op"`
D json.RawMessage `json:"d,omitempty"`
S uint32 `json:"s,omitempty"`
T string `json:"t,omitempty"`
}
// Reset 恢复到 0 值
func (wp *WebsocketPayload) Reset() {
*wp = WebsocketPayload{}
}
// GetHeartbeatInterval OpCodeHello 获得心跳周期 单位毫秒
func (wp *WebsocketPayload) GetHeartbeatInterval() (uint32, error) {
if wp.Op != OpCodeHello {
return 0, errors.New(getThisFuncName() + " unexpected OpCode " + strconv.Itoa(int(wp.Op)) + ", T: " + wp.T + ", D: " + BytesToString(wp.D))
}
data := &struct {
H uint32 `json:"heartbeat_interval"`
}{}
err := json.Unmarshal(wp.D, data)
return data.H, err
}
// SendPayload 发送 ws 包
func (bot *Bot) SendPayload(wp *WebsocketPayload) error {
bot.mu.Lock()
defer bot.mu.Unlock()
return bot.conn.WriteJSON(wp)
}
// WrapData 将结构体序列化到 wp.D
func (wp *WebsocketPayload) WrapData(v any) (err error) {
wp.D, err = json.Marshal(v)
return
}
// EventReady https://bot.q.qq.com/wiki/develop/api/gateway/reference.html#_2-%E9%89%B4%E6%9D%83%E8%BF%9E%E6%8E%A5
type EventReady struct {
Version int `json:"version"`
SessionID string `json:"session_id"`
User *User `json:"user"`
Shard [2]byte `json:"shard"`
}
// GetEventReady OpCodeDispatch READY
func (wp *WebsocketPayload) GetEventReady() (er EventReady, seq uint32, err error) {
if wp.Op != OpCodeDispatch {
err = errors.New(getThisFuncName() + " unexpected OpCode " + strconv.Itoa(int(wp.Op)) + ", T: " + wp.T + ", D: " + BytesToString(wp.D))
return
}
if wp.T != "READY" {
err = errors.New(getThisFuncName() + " unexpected event type " + wp.T + ", T: " + wp.T + ", D: " + BytesToString(wp.D))
return
}
seq = wp.S
err = json.Unmarshal(wp.D, &er)
return
}