Documentation ¶
Index ¶
- Constants
- Variables
- func NewWebSocketDataHandler(logger *zap.Logger, ws config.WebSocketConfig) (types.PriceWebSocketDataHandler, error)
- type BaseMessage
- type Channel
- type ErrorCode
- type ErrorMessage
- type Event
- type RequestResult
- type Status
- type SubscribeRequest
- type SubscribeResponse
- type TickerResult
- type TickerStream
- type WebSocketHandler
- func (h *WebSocketHandler) Copy() types.PriceWebSocketDataHandler
- func (h *WebSocketHandler) CreateMessages(tickers []types.ProviderTicker) ([]handlers.WebsocketEncodedMessage, error)
- func (h *WebSocketHandler) HandleMessage(message []byte) (types.PriceResponse, []handlers.WebsocketEncodedMessage, error)
- func (h *WebSocketHandler) HeartBeatMessages() ([]handlers.WebsocketEncodedMessage, error)
- func (h *WebSocketHandler) NewSubscribeRequest(symbols []string) ([]handlers.WebsocketEncodedMessage, error)
Constants ¶
const ( // ChannelTickers is the tickers channel to subscribe to. ChannelTickers Channel = "spot.tickers" // EventSubscribe is the event for subscribing to a topic. EventSubscribe Event = "subscribe" // EventUpdate is the event indicating an update. EventUpdate Event = "update" // StatusSuccess is the status indicating a subscription was successful. StatusSuccess Status = "success" // ErrorInvalidRequestBody is returned for an invalid body in the request. ErrorInvalidRequestBody ErrorCode = 1 // ErrorInvalidArgument is returned for an invalid argument in the request. ErrorInvalidArgument ErrorCode = 2 // ErrorServer is returned when there is a server side error. ErrorServer ErrorCode = 3 )
const ( // Name is the name of the Gate.io provider. Name = "gate_ws" // URL is the base url of for the Gate.io websocket API. URL = "wss://api.gateio.ws/ws/v4/" )
Variables ¶
var DefaultWebSocketConfig = config.WebSocketConfig{ Name: Name, Enabled: true, MaxBufferSize: 1000, ReconnectionTimeout: 10 * time.Second, PostConnectionTimeout: config.DefaultPostConnectionTimeout, Endpoints: []config.Endpoint{{URL: URL}}, ReadBufferSize: config.DefaultReadBufferSize, WriteBufferSize: config.DefaultWriteBufferSize, HandshakeTimeout: config.DefaultHandshakeTimeout, EnableCompression: config.DefaultEnableCompression, ReadTimeout: config.DefaultReadTimeout, WriteTimeout: config.DefaultWriteTimeout, PingInterval: config.DefaultPingInterval, WriteInterval: config.DefaultWriteInterval, MaxReadErrorCount: config.DefaultMaxReadErrorCount, MaxSubscriptionsPerConnection: config.DefaultMaxSubscriptionsPerConnection, MaxSubscriptionsPerBatch: config.DefaultMaxSubscriptionsPerBatch, }
DefaultWebSocketConfig is the default configuration for the Gate.io Websocket.
Functions ¶
func NewWebSocketDataHandler ¶
func NewWebSocketDataHandler( logger *zap.Logger, ws config.WebSocketConfig, ) (types.PriceWebSocketDataHandler, error)
NewWebSocketDataHandler returns a new Gate.io PriceWebSocketDataHandler.
Types ¶
type BaseMessage ¶
type BaseMessage struct { // Time is the time of the message. Time int `json:"time"` // Channel is the channel to subscribe to. Channel string `json:"channel"` // Event is the event the request/response is taking. Event string `json:"event"` }
BaseMessage is a base message for a request/response from a peer.
type ErrorMessage ¶
type ErrorMessage struct { // Code is the integer representation of the error code. Code int `json:"code"` // Message is the accompanying error message. Message string `json:"message"` }
ErrorMessage represents an error returned from the Gate.io websocket API.
type RequestResult ¶
type RequestResult struct { // Status is the status of the result. Status string `json:"status"` }
RequestResult is the result message returned in a response from the Gate.io websocket API.
type SubscribeRequest ¶
type SubscribeRequest struct { BaseMessage // ID is the optional ID for the message. ID int `json:"id"` // Payload is the argument payload sent for the corresponding request. Payload []string `json:"payload"` }
SubscribeRequest is a subscription request sent to the Gate.io websocket API.
Ex.
{ "time": 1611541000, "id": 123456789, "channel": "spot.orders", "event": "subscribe", "payload": ["BTC_USDT", "GT_USDT"], }
type SubscribeResponse ¶
type SubscribeResponse struct { BaseMessage // ID is the optional ID for the message. ID int `json:"id"` // Error is the error message returned. Will be empty if no error is returned. Error ErrorMessage `json:"error"` // Result is the result returned from the server. Result RequestResult `json:"result"` }
SubscribeResponse is a subscription response sent from the Gate.io websocket API.
Ex.
{ "time": 1611541000, "time_ms": 1611541000001, "channel": "spot.orders", "event": "subscribe", "error": null, "result": { "status": "success" } }
type TickerResult ¶
type TickerResult struct { // CurrencyPair is the currency pair for the given data stream. CurrencyPair string `json:"currency_pair"` // Last is the last price of the pair. Last string `json:"last"` }
TickerResult is the result returned in a TickerStream message.
type TickerStream ¶
type TickerStream struct { BaseMessage // Result is the result body of the data stream. Result TickerResult `json:"result"` }
TickerStream is the data stream returned for a ticker subscription.
Ex.
{ "time": 1669107766, "time_ms": 1669107766406, "channel": "spot.tickers", "event": "update", "result": { "currency_pair": "BTC_USDT", "last": "15743.4", "lowest_ask": "15744.4", "highest_bid": "15743.5", "change_percentage": "-1.8254", "base_volume": "9110.473081735", "quote_volume": "145082083.2535", "high_24h": "16280.9", "low_24h": "15468.5" } }
type WebSocketHandler ¶
type WebSocketHandler struct {
// contains filtered or unexported fields
}
WebSocketHandler implements the WebSocketDataHandler interface. This is used to handle messages received from the Gate.io websocket API.
func (*WebSocketHandler) Copy ¶
func (h *WebSocketHandler) Copy() types.PriceWebSocketDataHandler
Copy is used to create a copy of the WebSocketHandler.
func (*WebSocketHandler) CreateMessages ¶
func (h *WebSocketHandler) CreateMessages( tickers []types.ProviderTicker, ) ([]handlers.WebsocketEncodedMessage, error)
CreateMessages is used to create an initial subscription message to send to the data provider. Only the tickers that are specified in the config are subscribed to. The only channel that is subscribed to is the tickers channel - which supports spot markets.
func (*WebSocketHandler) HandleMessage ¶
func (h *WebSocketHandler) HandleMessage( message []byte, ) (types.PriceResponse, []handlers.WebsocketEncodedMessage, error)
HandleMessage is used to handle a message received from the data provider. The Gate.io provider sends two types of messages:
- Subscribe response message. The subscribe response message is used to determine if the subscription was successful.
- Ticker stream message. This is sent when a ticker update is received from the Gate.io websocket API.
func (*WebSocketHandler) HeartBeatMessages ¶
func (h *WebSocketHandler) HeartBeatMessages() ([]handlers.WebsocketEncodedMessage, error)
HeartBeatMessages is not used for Gate.io.
func (*WebSocketHandler) NewSubscribeRequest ¶
func (h *WebSocketHandler) NewSubscribeRequest(symbols []string) ([]handlers.WebsocketEncodedMessage, error)
NewSubscribeRequest returns a new SubscribeRequest encoded message for the given symbols.