@@ -3,12 +3,12 @@ package main
3
3
import (
4
4
"fmt"
5
5
"github.com/BurntSushi/toml"
6
- "github.com/gorilla /websocket"
6
+ "golang.org/x/net /websocket"
7
7
"github.com/projectriri/bot-gateway/router"
8
8
"github.com/projectriri/bot-gateway/types"
9
9
"github.com/projectriri/bot-gateway/utils"
10
10
log "github.com/sirupsen/logrus"
11
- "net/http "
11
+ "encoding/json "
12
12
)
13
13
14
14
var (
@@ -78,24 +78,26 @@ func (p *Plugin) Start() {
78
78
79
79
log .Infof ("[websocket-client-cqhttp] dialing cqhttp-websocket server" )
80
80
var err error
81
- header := http.Header {}
82
- header .Add ("Authorization" , fmt .Sprintf ("Token %s" , p .config .CQHTTPAccessToken ))
83
81
// 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/" , "http://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 )
88
88
if err != nil {
89
89
log .Errorf ("[websocket-client-cqhttp] failed to dial cqhttp api websocket (%v)" , err )
90
90
} else {
91
91
log .Infof ("[websocket-client-cqhttp] dial cqhttp api websocket success" )
92
92
}
93
93
defer p .apiClient .Close ()
94
94
// 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/" , "http://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 )
99
101
if err != nil {
100
102
log .Errorf ("[websocket-client-cqhttp] failed to dial cqhttp event websocket (%v)" , err )
101
103
} else {
@@ -106,8 +108,8 @@ func (p *Plugin) Start() {
106
108
// Start main event update loop
107
109
go func () {
108
110
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 {
111
113
log .Errorf ("[websocket-client-cqhttp] failed to read event (%v)" , err )
112
114
continue
113
115
}
@@ -134,14 +136,14 @@ func (p *Plugin) Start() {
134
136
for {
135
137
// send api request
136
138
apiRequestPkt := cc .Consume ()
137
- err := p . apiClient . WriteMessage ( websocket . TextMessage , apiRequestPkt .Body )
139
+ err := websocket . JSON . Send ( p . apiClient , apiRequestPkt .Body )
138
140
if err != nil {
139
141
log .Errorf ("[websocket-client-cqhttp] failed to send apirequest (%v)" , err )
140
142
continue
141
143
}
142
144
// 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 {
145
147
log .Errorf ("[websocket-client-cqhttp] failed to read apiresponse (%v)" , err )
146
148
continue
147
149
}
0 commit comments