-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcu_ws.go
129 lines (116 loc) · 2.67 KB
/
lcu_ws.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
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
package lcu
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"net/http"
"sync"
)
type lcuWebsocket struct {
conn *websocket.Conn
wg *sync.WaitGroup
onError func(error)
onPanic func(any)
dispatcher map[string]func(*lwsMessageContent)
}
type lwsMessageContent struct {
Data any `json:"data"`
EventType string `json:"eventType"`
Uri string `json:"uri"`
}
// Subscribe 订阅一个客户端事件。
//
// event: 事件
//
// handler: 回调函数
func (ws *lcuWebsocket) Subscribe(event string, handler func(*lwsMessageContent)) error {
err := ws.conn.WriteJSON([]interface{}{5, event})
if err != nil {
return err
}
ws.dispatcher[event] = handler
return nil
}
// Unsubscribe 取消订阅一个客户端事件。
func (ws *lcuWebsocket) Unsubscribe(event string) error {
err := ws.conn.WriteJSON([]interface{}{6, event})
if err != nil {
return err
}
delete(ws.dispatcher, event)
return nil
}
// StartWebsocket 启动一个websocket连接。
//
// 可用于获取客户端事件
//
// onError: 错误回调,可为nil
//
// onPanic: 异常回调,可为nil
func (lcu *lcuClient) StartWebsocket(onError func(error), onPanic func(any)) (*lcuWebsocket, error) {
dialer := websocket.Dialer{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // 跳过证书验证
},
}
conn, _, err := dialer.Dial(
"wss://127.0.0.1:"+lcu.Port,
http.Header{"Authorization": []string{lcu.Auth.toString()}},
)
if err != nil {
fmt.Println("连接失败")
return nil, err
}
if onError == nil {
onError = func(err error) {
fmt.Printf("Websocket错误: %v\n", err)
}
}
if onPanic == nil {
onPanic = func(recover any) {
fmt.Printf("Websocket异常: %v\n", recover)
}
}
ws := &lcuWebsocket{
conn: conn,
wg: new(sync.WaitGroup),
onError: onError,
onPanic: onPanic,
dispatcher: make(map[string]func(*lwsMessageContent)),
}
go ws.listen()
return ws, nil
}
func (ws *lcuWebsocket) listen() {
ws.wg.Add(1)
defer func() {
ws.wg.Done()
if r := recover(); r != nil {
ws.onPanic(r)
}
}()
for {
_, message, err := ws.conn.ReadMessage()
if err != nil {
ws.onError(err)
continue
}
if len(message) == 0 {
continue
}
proto := new([]any)
// The structure of proto message like: [code.(int), event.(string), data.(lwsMessageContent)]
err = json.Unmarshal(message, &proto)
if err != nil {
ws.onError(err)
continue
}
var msgContent lwsMessageContent
t := (*proto)[2].(map[string]any)
msgContent.Data = t["data"]
msgContent.EventType = t["eventType"].(string)
msgContent.Uri = t["uri"].(string)
ws.dispatcher[(*proto)[1].(string)](&msgContent)
}
}