You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
283 lines
7.2 KiB
283 lines
7.2 KiB
2 months ago
|
package hbwebsocketclientbase
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/gorilla/websocket"
|
||
|
"strings"
|
||
|
"sync"
|
||
|
"time"
|
||
|
"wss-pool/internal/gzip"
|
||
|
"wss-pool/internal/model"
|
||
|
"wss-pool/logging/applogger"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
TimerIntervalSecond = 5
|
||
|
ReconnectWaitSecond = 60
|
||
|
|
||
|
wsPath = "/ws"
|
||
|
feedPath = "/feed"
|
||
|
MaxMegByte = 100
|
||
|
)
|
||
|
|
||
|
// It will be invoked after websocket connected
|
||
|
type ConnectedHandler func()
|
||
|
|
||
|
// It will be invoked after valid message received
|
||
|
type MessageHandler func(message string) (interface{}, error)
|
||
|
|
||
|
// It will be invoked after response is parsed
|
||
|
type ResponseHandler func(response interface{})
|
||
|
|
||
|
// The base class that responsible to get data from websocket
|
||
|
type WebSocketClientBase struct {
|
||
|
host string
|
||
|
path string
|
||
|
conn *websocket.Conn
|
||
|
connectedHandler ConnectedHandler
|
||
|
messageHandler MessageHandler
|
||
|
responseHandler ResponseHandler
|
||
|
stopReadChannel chan int
|
||
|
stopTickerChannel chan int
|
||
|
ticker *time.Ticker
|
||
|
lastReceivedTime time.Time
|
||
|
sendMutex *sync.Mutex
|
||
|
Symbol string
|
||
|
}
|
||
|
|
||
|
// Initializer
|
||
|
func (p *WebSocketClientBase) Init(host string) *WebSocketClientBase {
|
||
|
p.host = host
|
||
|
p.path = wsPath
|
||
|
p.stopReadChannel = make(chan int, 1)
|
||
|
p.stopTickerChannel = make(chan int, 1)
|
||
|
p.sendMutex = &sync.Mutex{}
|
||
|
|
||
|
return p
|
||
|
}
|
||
|
|
||
|
// Initializer with path
|
||
|
func (p *WebSocketClientBase) InitWithFeedPath(host string) *WebSocketClientBase {
|
||
|
p.Init(host)
|
||
|
p.path = feedPath
|
||
|
return p
|
||
|
}
|
||
|
|
||
|
// Set callback handler
|
||
|
func (p *WebSocketClientBase) SetHandler(connHandler ConnectedHandler, msgHandler MessageHandler, repHandler ResponseHandler) {
|
||
|
p.connectedHandler = connHandler
|
||
|
p.messageHandler = msgHandler
|
||
|
p.responseHandler = repHandler
|
||
|
}
|
||
|
|
||
|
// Connect to websocket websocketserver
|
||
|
// if autoConnect is true, then the connection can be re-connect if no data received after the pre-defined timeout
|
||
|
func (p *WebSocketClientBase) Connect(autoConnect bool) {
|
||
|
// initialize last received time as now
|
||
|
p.lastReceivedTime = time.Now()
|
||
|
|
||
|
// connect to websocket
|
||
|
p.connectWebSocket()
|
||
|
|
||
|
// start ticker to manage connection
|
||
|
if autoConnect {
|
||
|
p.startTicker()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Send data to websocket websocketserver
|
||
|
func (p *WebSocketClientBase) Send(data string) {
|
||
|
if p.conn == nil {
|
||
|
applogger.Error("WebSocket sent error: no connection available")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
p.sendMutex.Lock()
|
||
|
err := p.conn.WriteMessage(websocket.TextMessage, []byte(data))
|
||
|
p.sendMutex.Unlock()
|
||
|
|
||
|
if err != nil {
|
||
|
applogger.Error("WebSocket sent error: data=%s, error=%s", data, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Close the connection to websocketserver
|
||
|
func (p *WebSocketClientBase) Close() {
|
||
|
p.stopTicker()
|
||
|
p.disconnectWebSocket()
|
||
|
}
|
||
|
|
||
|
// connect to websocketserver
|
||
|
func (p *WebSocketClientBase) connectWebSocket() {
|
||
|
var err error
|
||
|
url := fmt.Sprintf("wss://%s%s", p.host, p.path)
|
||
|
applogger.Debug("WebSocket connecting...", url)
|
||
|
p.conn, _, err = websocket.DefaultDialer.Dial(url, nil)
|
||
|
if err != nil {
|
||
|
applogger.Error("WebSocket connected error: %s", err)
|
||
|
return
|
||
|
}
|
||
|
applogger.Info("WebSocket connected")
|
||
|
|
||
|
// start loop to read and handle message
|
||
|
p.startReadLoop()
|
||
|
|
||
|
if p.connectedHandler != nil {
|
||
|
p.connectedHandler()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// disconnect with websocketserver
|
||
|
func (p *WebSocketClientBase) disconnectWebSocket() {
|
||
|
if p.conn == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// start a new goroutine to send stop signal
|
||
|
go p.stopReadLoop()
|
||
|
|
||
|
applogger.Debug("WebSocket disconnecting...")
|
||
|
err := p.conn.Close()
|
||
|
if err != nil {
|
||
|
applogger.Error("WebSocket disconnect error: %s", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
applogger.Info("WebSocket disconnected")
|
||
|
}
|
||
|
|
||
|
// initialize a ticker and start a goroutine tickerLoop()
|
||
|
func (p *WebSocketClientBase) startTicker() {
|
||
|
p.ticker = time.NewTicker(TimerIntervalSecond * time.Second)
|
||
|
|
||
|
go p.tickerLoop()
|
||
|
}
|
||
|
|
||
|
// stop ticker and stop the goroutine
|
||
|
func (p *WebSocketClientBase) stopTicker() {
|
||
|
p.ticker.Stop()
|
||
|
p.stopTickerChannel <- 1
|
||
|
}
|
||
|
|
||
|
// defines a for loop that will run based on ticker's frequency
|
||
|
// It checks the last data that received from websocketserver, if it is longer than the threshold,
|
||
|
// it will force disconnect websocketserver and connect again.
|
||
|
func (p *WebSocketClientBase) tickerLoop() {
|
||
|
applogger.Debug("tickerLoop started")
|
||
|
for {
|
||
|
select {
|
||
|
// Receive data from stopChannel
|
||
|
case <-p.stopTickerChannel:
|
||
|
applogger.Debug("tickerLoop stopped")
|
||
|
return
|
||
|
|
||
|
// Receive tick from tickChannel
|
||
|
case <-p.ticker.C:
|
||
|
elapsedSecond := time.Now().Sub(p.lastReceivedTime).Seconds()
|
||
|
applogger.Debug("WebSocket received data %f sec ago", elapsedSecond)
|
||
|
|
||
|
if elapsedSecond > ReconnectWaitSecond {
|
||
|
applogger.Info("WebSocket reconnect...")
|
||
|
p.disconnectWebSocket()
|
||
|
p.connectWebSocket()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// start a goroutine readLoop()
|
||
|
func (p *WebSocketClientBase) startReadLoop() {
|
||
|
go p.readLoop()
|
||
|
}
|
||
|
|
||
|
// stop the goroutine readLoop()
|
||
|
func (p *WebSocketClientBase) stopReadLoop() {
|
||
|
p.stopReadChannel <- 1
|
||
|
}
|
||
|
|
||
|
func (p *WebSocketClientBase) contractDepth(buf []byte) {
|
||
|
applogger.Info("message big lai", len(buf))
|
||
|
_, err := p.messageHandler(string(buf))
|
||
|
if err != nil {
|
||
|
applogger.Error("Handle message error: %s", err)
|
||
|
return
|
||
|
}
|
||
|
if p.responseHandler != nil {
|
||
|
p.responseHandler(buf)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// defines a for loop to read data from websocketserver
|
||
|
// it will stop once it receives the signal from stopReadChannel
|
||
|
func (p *WebSocketClientBase) readLoop() {
|
||
|
applogger.Debug("readLoop started")
|
||
|
for {
|
||
|
select {
|
||
|
// Receive data from stopChannel
|
||
|
case <-p.stopReadChannel:
|
||
|
applogger.Debug("readLoop stopped")
|
||
|
return
|
||
|
|
||
|
default:
|
||
|
if p.conn == nil {
|
||
|
applogger.Error("Read error: no connection available")
|
||
|
time.Sleep(TimerIntervalSecond * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
msgType, buf, err := p.conn.ReadMessage()
|
||
|
if err != nil {
|
||
|
applogger.Error("Read error: %s", err)
|
||
|
time.Sleep(TimerIntervalSecond * time.Second)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
p.lastReceivedTime = time.Now()
|
||
|
|
||
|
// decompress gzip data if it is binary message
|
||
|
if msgType == websocket.BinaryMessage {
|
||
|
//只有合约深度不解压
|
||
|
fmt.Println("websocket :", p.Symbol)
|
||
|
if len(buf) >= MaxMegByte && strings.Contains(p.Symbol, "USDT.depth.step") {
|
||
|
// If it contains expected string, then invoke message handler and response handler
|
||
|
p.contractDepth(buf)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// TODO: 注释GZipDecompress
|
||
|
message, err := gzip.GZipDecompress(buf)
|
||
|
if err != nil {
|
||
|
applogger.Error("UnGZip data error: %s", err)
|
||
|
return
|
||
|
}
|
||
|
// applogger.Info(string(buf))
|
||
|
//Try to pass as PingMessage
|
||
|
pingMsg := model.ParsePingMessage(message)
|
||
|
|
||
|
// If it is Ping then respond Pong
|
||
|
if pingMsg != nil && pingMsg.Ping != 0 {
|
||
|
applogger.Debug("Received Ping: %d", pingMsg.Ping)
|
||
|
pongMsg := fmt.Sprintf("{\"pong\": %d}", pingMsg.Ping)
|
||
|
p.Send(pongMsg)
|
||
|
applogger.Debug("Replied Pong: %d", pingMsg.Ping)
|
||
|
} else if strings.Contains(message, "tick") || strings.Contains(message, "data") {
|
||
|
// If it contains expected string, then invoke message handler and response handler
|
||
|
//可能少量数据 低于 MaxMegByte
|
||
|
if strings.Contains(p.Symbol, "USDT.depth.step") {
|
||
|
p.contractDepth(buf)
|
||
|
continue
|
||
|
}
|
||
|
result, err := p.messageHandler(message)
|
||
|
if err != nil {
|
||
|
applogger.Error("Handle message error: %s", err)
|
||
|
continue
|
||
|
}
|
||
|
if p.responseHandler != nil {
|
||
|
p.responseHandler(result)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|