Skip to content

Commit 1ba744d

Browse files
committed
🐛 use /x/net/websocket due to kyubotics/coolq-http-api#85
1 parent 3539595 commit 1ba744d

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

adapters/websocket-client-cqhttp/websocket-client-cqhttp.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"fmt"
55
"github.com/BurntSushi/toml"
6-
"github.com/gorilla/websocket"
6+
"golang.org/x/net/websocket"
77
"github.com/projectriri/bot-gateway/router"
88
"github.com/projectriri/bot-gateway/types"
99
"github.com/projectriri/bot-gateway/utils"
1010
log "github.com/sirupsen/logrus"
11-
"net/http"
11+
"encoding/json"
1212
)
1313

1414
var (
@@ -78,24 +78,26 @@ func (p *Plugin) Start() {
7878

7979
log.Infof("[websocket-client-cqhttp] dialing cqhttp-websocket server")
8080
var err error
81-
header := http.Header{}
82-
header.Add("Authorization", fmt.Sprintf("Token %s", p.config.CQHTTPAccessToken))
8381
// Dial /api/ ws
84-
p.apiClient, _, err = websocket.DefaultDialer.Dial(
85-
p.config.CQHTTPWebSocketAddr+"/api/",
86-
header,
87-
)
82+
apiConfig, err := websocket.NewConfig(p.config.CQHTTPWebSocketAddr+"/api/", "localhost")
83+
if err != nil {
84+
log.Fatalf("[websocket-client-cqhttp] invalid websocket address %v", err)
85+
}
86+
apiConfig.Header.Add("Authorization", fmt.Sprintf("Token %s", p.config.CQHTTPAccessToken))
87+
p.apiClient, err = websocket.DialConfig(apiConfig)
8888
if err != nil {
8989
log.Errorf("[websocket-client-cqhttp] failed to dial cqhttp api websocket (%v)", err)
9090
} else {
9191
log.Infof("[websocket-client-cqhttp] dial cqhttp api websocket success")
9292
}
9393
defer p.apiClient.Close()
9494
// Dial /event/ ws
95-
p.eventClient, _, err = websocket.DefaultDialer.Dial(
96-
p.config.CQHTTPWebSocketAddr+"/event/",
97-
header,
98-
)
95+
eventConfig, err := websocket.NewConfig(p.config.CQHTTPWebSocketAddr+"/event/", "localhost")
96+
if err != nil {
97+
log.Fatalf("[websocket-client-cqhttp] invalid websocket address %v", err)
98+
}
99+
eventConfig.Header.Add("Authorization", fmt.Sprintf("Token %s", p.config.CQHTTPAccessToken))
100+
p.eventClient, err = websocket.DialConfig(eventConfig)
99101
if err != nil {
100102
log.Errorf("[websocket-client-cqhttp] failed to dial cqhttp event websocket (%v)", err)
101103
} else {
@@ -106,8 +108,8 @@ func (p *Plugin) Start() {
106108
// Start main event update loop
107109
go func() {
108110
for {
109-
_, msg, err := p.eventClient.ReadMessage()
110-
if err != nil {
111+
msg := json.RawMessage{}
112+
if err := websocket.JSON.Receive(p.eventClient, &msg); err != nil {
111113
log.Errorf("[websocket-client-cqhttp] failed to read event (%v)", err)
112114
continue
113115
}
@@ -134,14 +136,14 @@ func (p *Plugin) Start() {
134136
for {
135137
// send api request
136138
apiRequestPkt := cc.Consume()
137-
err := p.apiClient.WriteMessage(websocket.TextMessage, apiRequestPkt.Body)
139+
err := websocket.JSON.Send(p.apiClient, apiRequestPkt.Body)
138140
if err != nil {
139141
log.Errorf("[websocket-client-cqhttp] failed to send apirequest (%v)", err)
140142
continue
141143
}
142144
// get api response
143-
_, msg, err := p.apiClient.ReadMessage()
144-
if err != nil {
145+
msg := json.RawMessage{}
146+
if err := websocket.JSON.Receive(p.apiClient, &msg); err != nil {
145147
log.Errorf("[websocket-client-cqhttp] failed to read apiresponse (%v)", err)
146148
continue
147149
}

0 commit comments

Comments
 (0)