Skip to content

Commit

Permalink
🐛 use /x/net/websocket due to kyubotics/coolq-http-api#85
Browse files Browse the repository at this point in the history
  • Loading branch information
satouriko committed Oct 7, 2018
1 parent 3539595 commit ec66a7b
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions adapters/websocket-client-cqhttp/websocket-client-cqhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/gorilla/websocket"
"golang.org/x/net/websocket"
"github.com/projectriri/bot-gateway/router"
"github.com/projectriri/bot-gateway/types"
"github.com/projectriri/bot-gateway/utils"
log "github.com/sirupsen/logrus"
"net/http"
"encoding/json"
)

var (
Expand Down Expand Up @@ -78,24 +78,26 @@ func (p *Plugin) Start() {

log.Infof("[websocket-client-cqhttp] dialing cqhttp-websocket server")
var err error
header := http.Header{}
header.Add("Authorization", fmt.Sprintf("Token %s", p.config.CQHTTPAccessToken))
// Dial /api/ ws
p.apiClient, _, err = websocket.DefaultDialer.Dial(
p.config.CQHTTPWebSocketAddr+"/api/",
header,
)
apiConfig, err := websocket.NewConfig(p.config.CQHTTPWebSocketAddr+"/api/", "")
if err != nil {
log.Fatal("[websocket-client-cqhttp] invalid websocket address")
}
apiConfig.Header.Add("Authorization", fmt.Sprintf("Token %s", p.config.CQHTTPAccessToken))
p.apiClient, err = websocket.DialConfig(apiConfig)
if err != nil {
log.Errorf("[websocket-client-cqhttp] failed to dial cqhttp api websocket (%v)", err)
} else {
log.Infof("[websocket-client-cqhttp] dial cqhttp api websocket success")
}
defer p.apiClient.Close()
// Dial /event/ ws
p.eventClient, _, err = websocket.DefaultDialer.Dial(
p.config.CQHTTPWebSocketAddr+"/event/",
header,
)
eventConfig, err := websocket.NewConfig(p.config.CQHTTPWebSocketAddr+"/event/", "")
if err != nil {
log.Fatal("[websocket-client-cqhttp] invalid websocket address")
}
eventConfig.Header.Add("Authorization", fmt.Sprintf("Token %s", p.config.CQHTTPAccessToken))
p.eventClient, err = websocket.DialConfig(eventConfig)
if err != nil {
log.Errorf("[websocket-client-cqhttp] failed to dial cqhttp event websocket (%v)", err)
} else {
Expand All @@ -106,8 +108,8 @@ func (p *Plugin) Start() {
// Start main event update loop
go func() {
for {
_, msg, err := p.eventClient.ReadMessage()
if err != nil {
msg := json.RawMessage{}
if err := websocket.JSON.Receive(p.eventClient, &msg); err != nil {
log.Errorf("[websocket-client-cqhttp] failed to read event (%v)", err)
continue
}
Expand All @@ -134,14 +136,14 @@ func (p *Plugin) Start() {
for {
// send api request
apiRequestPkt := cc.Consume()
err := p.apiClient.WriteMessage(websocket.TextMessage, apiRequestPkt.Body)
err := websocket.JSON.Send(p.apiClient, apiRequestPkt.Body)
if err != nil {
log.Errorf("[websocket-client-cqhttp] failed to send apirequest (%v)", err)
continue
}
// get api response
_, msg, err := p.apiClient.ReadMessage()
if err != nil {
msg := json.RawMessage{}
if err := websocket.JSON.Receive(p.apiClient, &msg); err != nil {
log.Errorf("[websocket-client-cqhttp] failed to read apiresponse (%v)", err)
continue
}
Expand Down

0 comments on commit ec66a7b

Please sign in to comment.