bbgo_origin/pkg/exchange/ftx/stream_message_handler.go

49 lines
1.2 KiB
Go
Raw Normal View History

2021-02-27 11:27:44 +00:00
package ftx
import (
"encoding/json"
2021-03-02 14:18:41 +00:00
log "github.com/sirupsen/logrus"
2021-02-27 11:27:44 +00:00
"github.com/c9s/bbgo/pkg/types"
)
type messageHandler struct {
2021-03-02 14:18:41 +00:00
*types.StandardStream
2021-02-27 11:27:44 +00:00
}
func (h messageHandler) handleMessage(message []byte) {
var r rawResponse
if err := json.Unmarshal(message, &r); err != nil {
logger.WithError(err).Errorf("failed to unmarshal resp: %s", string(message))
return
}
switch r.Type {
case subscribedRespType:
h.handleSubscribedMessage(r)
2021-03-02 14:18:41 +00:00
case partialRespType:
// snapshot of current market data
h.handleSnapshot(r)
case updateRespType:
//log.Infof("update=> %s", string(message))
2021-02-27 11:27:44 +00:00
default:
logger.Errorf("unsupported message type: %+v", r.Type)
}
}
// {"type": "subscribed", "channel": "orderbook", "market": "BTC/USDT"}
func (h messageHandler) handleSubscribedMessage(response rawResponse) {
2021-02-28 13:48:50 +00:00
r := response.toSubscribedResp()
logger.Infof("%s %s is subscribed", r.Market, r.Channel)
2021-02-27 11:27:44 +00:00
}
2021-03-02 14:18:41 +00:00
func (h messageHandler) handleSnapshot(response rawResponse) {
r, err := response.toSnapshotResp()
if err != nil {
log.WithError(err).Errorf("failed to convert the partial response to snapshot")
return
}
h.EmitBookSnapshot(r.toGlobalOrderBook())
}