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.
263 lines
7.0 KiB
263 lines
7.0 KiB
package hbwebsocketclientbase
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gorilla/websocket"
|
|
"sync"
|
|
"time"
|
|
"wss-pool/internal/gzip"
|
|
"wss-pool/internal/model"
|
|
"wss-pool/internal/requestbuilder"
|
|
"wss-pool/logging/applogger"
|
|
"wss-pool/pkg/model/auth"
|
|
"wss-pool/pkg/model/base"
|
|
)
|
|
|
|
const (
|
|
websocketV2Path = "/ws/v2"
|
|
)
|
|
|
|
// It will be invoked after websocket v2 authentication response received
|
|
type AuthenticationV2ResponseHandler func(resp *auth.WebSocketV2AuthenticationResponse)
|
|
|
|
// The base class that responsible to get data from websocket authentication v2
|
|
type WebSocketV2ClientBase struct {
|
|
host string
|
|
conn *websocket.Conn
|
|
|
|
authenticationResponseHandler AuthenticationV2ResponseHandler
|
|
messageHandler MessageHandler
|
|
responseHandler ResponseHandler
|
|
|
|
stopReadChannel chan int
|
|
stopTickerChannel chan int
|
|
ticker *time.Ticker
|
|
lastReceivedTime time.Time
|
|
sendMutex *sync.Mutex
|
|
|
|
requestBuilder *requestbuilder.WebSocketV2RequestBuilder
|
|
}
|
|
|
|
// Initializer
|
|
func (p *WebSocketV2ClientBase) Init(accessKey string, secretKey string, host string) *WebSocketV2ClientBase {
|
|
p.host = host
|
|
p.stopReadChannel = make(chan int, 1)
|
|
p.stopTickerChannel = make(chan int, 1)
|
|
p.requestBuilder = new(requestbuilder.WebSocketV2RequestBuilder).Init(accessKey, secretKey, host, websocketV2Path)
|
|
p.sendMutex = &sync.Mutex{}
|
|
return p
|
|
}
|
|
|
|
// Set callback handler
|
|
func (p *WebSocketV2ClientBase) SetHandler(authHandler AuthenticationV2ResponseHandler, msgHandler MessageHandler, repHandler ResponseHandler) {
|
|
p.authenticationResponseHandler = authHandler
|
|
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 *WebSocketV2ClientBase) 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 *WebSocketV2ClientBase) 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 *WebSocketV2ClientBase) Close() {
|
|
p.stopTicker()
|
|
p.disconnectWebSocket()
|
|
}
|
|
|
|
// connect to websocketserver
|
|
func (p *WebSocketV2ClientBase) connectWebSocket() {
|
|
var err error
|
|
url := fmt.Sprintf("wss://%s%s", p.host, websocketV2Path)
|
|
applogger.Debug("WebSocket connecting...")
|
|
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()
|
|
|
|
// send authentication if connect to websocket successfully
|
|
auth, err := p.requestBuilder.Build()
|
|
if err != nil {
|
|
applogger.Error("Signature generated error: %s", err)
|
|
return
|
|
}
|
|
p.Send(auth)
|
|
applogger.Info("WebSocket sent authentication")
|
|
}
|
|
|
|
// disconnect with websocketserver
|
|
func (p *WebSocketV2ClientBase) 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 *WebSocketV2ClientBase) startTicker() {
|
|
p.ticker = time.NewTicker(TimerIntervalSecond * time.Second)
|
|
|
|
go p.tickerLoop()
|
|
}
|
|
|
|
// stop ticker and stop the goroutine
|
|
func (p *WebSocketV2ClientBase) 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 *WebSocketV2ClientBase) tickerLoop() {
|
|
applogger.Debug("tickerLoop started")
|
|
for {
|
|
select {
|
|
// start a goroutine readLoop()
|
|
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 *WebSocketV2ClientBase) startReadLoop() {
|
|
go p.readLoop()
|
|
}
|
|
|
|
// stop the goroutine readLoop()
|
|
func (p *WebSocketV2ClientBase) stopReadLoop() {
|
|
p.stopReadChannel <- 1
|
|
}
|
|
|
|
// defines a for loop to read data from websocketserver
|
|
// it will stop once it receives the signal from stopReadChannel
|
|
func (p *WebSocketV2ClientBase) 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
|
|
var message string
|
|
if msgType == websocket.BinaryMessage {
|
|
message, err = gzip.GZipDecompress(buf)
|
|
if err != nil {
|
|
applogger.Error("UnGZip data error: %s", err)
|
|
return
|
|
}
|
|
} else if msgType == websocket.TextMessage {
|
|
message = string(buf)
|
|
}
|
|
|
|
// Try to pass as PingV2Message
|
|
// If it is Ping then respond Pong
|
|
pingV2Msg := model.ParsePingV2Message(message)
|
|
if pingV2Msg.IsPing() {
|
|
applogger.Debug("Received Ping: %d", pingV2Msg.Data.Timestamp)
|
|
pongMsg := fmt.Sprintf("{\"action\": \"pong\", \"data\": { \"ts\": %d } }", pingV2Msg.Data.Timestamp)
|
|
p.Send(pongMsg)
|
|
applogger.Debug("Respond Pong: %d", pingV2Msg.Data.Timestamp)
|
|
} else {
|
|
// Try to pass as websocket v2 authentication response
|
|
// If it is then invoke authentication handler
|
|
wsV2Resp := base.ParseWSV2Resp(message)
|
|
if wsV2Resp != nil {
|
|
switch wsV2Resp.Action {
|
|
case "req":
|
|
authResp := auth.ParseWSV2AuthResp(message)
|
|
if authResp != nil && p.authenticationResponseHandler != nil {
|
|
p.authenticationResponseHandler(authResp)
|
|
}
|
|
|
|
case "sub", "push":
|
|
{
|
|
result, err := p.messageHandler(message)
|
|
if err != nil {
|
|
applogger.Error("Handle message error: %s", err)
|
|
continue
|
|
}
|
|
if p.responseHandler != nil {
|
|
p.responseHandler(result)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|