Documentation
¶
Index ¶
- Constants
- Variables
- func NewPingRequestMessage() ([]handlers.WebsocketEncodedMessage, error)
- func NewWebSocketDataHandler(logger *zap.Logger, ws config.WebSocketConfig) (types.PriceWebSocketDataHandler, error)
- type BaseMessage
- type ChannelType
- type MethodType
- type PingRequestMessage
- type PongResponseMessage
- type SubscriptionRequestMessage
- type SubscriptionResponseMessage
- type TickerData
- type TickerResponseMessage
- 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) NewSubscribeRequestMessage(instruments []string) ([]handlers.WebsocketEncodedMessage, error)
Constants ¶
const ( // SubscriptionMethod is the method that is sent to the MEXC websocket to subscribe to a // currency pair i.e. market. // // ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#live-subscribing-unsubscribing-to-streams SubscriptionMethod MethodType = "SUBSCRIPTION" // PingMethod is the method that is sent to the MEXC websocket to ping the server. This should // be done every 30 seconds. // // ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#live-subscribing-unsubscribing-to-streams PingMethod MethodType = "PING" // PongMethod is the method that is sent from the server to the client to confirm that the // client has successfully pinged the server. // // ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#live-subscribing-unsubscribing-to-streams PongMethod MethodType = "PONG" // MiniTickerChannel is the channel that is used to subscribe to the mini ticker data for a // currency pair i.e. market. // // ex: [email protected]@BTCUSDT@UTC+8 // // ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#miniticker MiniTickerChannel ChannelType = "[email protected]@" )
const ( // Name is the name of the MEXC provider. Name = "mexc_ws" // WSS is the public MEXC Websocket URL. WSS = "wss://wbs.mexc.com/ws" // DefaultPingInterval is the default ping interval for the MEXC websocket. The documentation // specifies that this should be done every 30 seconds, however, the actual threshold should be // slightly lower than this to account for network latency. DefaultPingInterval = 20 * time.Second // MaxSubscriptionsPerConnection is the maximum number of subscriptions that can be made // per connection. // // ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#websocket-market-streams MaxSubscriptionsPerConnection = 20 )
Variables ¶
var DefaultWebSocketConfig = config.WebSocketConfig{ Name: Name, Enabled: true, MaxBufferSize: 1000, ReconnectionTimeout: config.DefaultReconnectionTimeout, PostConnectionTimeout: config.DefaultPostConnectionTimeout, Endpoints: []config.Endpoint{{URL: WSS}}, ReadBufferSize: config.DefaultReadBufferSize, WriteBufferSize: config.DefaultWriteBufferSize, HandshakeTimeout: config.DefaultHandshakeTimeout, EnableCompression: config.DefaultEnableCompression, ReadTimeout: config.DefaultReadTimeout, WriteTimeout: config.DefaultWriteTimeout, PingInterval: DefaultPingInterval, WriteInterval: config.DefaultWriteInterval, MaxReadErrorCount: config.DefaultMaxReadErrorCount, MaxSubscriptionsPerConnection: MaxSubscriptionsPerConnection, MaxSubscriptionsPerBatch: config.DefaultMaxSubscriptionsPerBatch, }
DefaultWebSocketConfig is the default configuration for the MEXC Websocket.
Functions ¶
func NewPingRequestMessage ¶
func NewPingRequestMessage() ([]handlers.WebsocketEncodedMessage, error)
NewPingRequestMessage returns a new PingRequestMessage.
func NewWebSocketDataHandler ¶
func NewWebSocketDataHandler( logger *zap.Logger, ws config.WebSocketConfig, ) (types.PriceWebSocketDataHandler, error)
NewWebSocketDataHandler returns a new MEXC PriceWebSocketDataHandler.
Types ¶
type BaseMessage ¶
type BaseMessage struct { // ID is the ID of the subscription request. ID int64 `json:"id"` // Code is the status code of the subscription request. Code int64 `json:"code"` // Message is the message that is sent from the MEXC websocket to confirm that // the client has successfully subscribed to a currency pair i.e. market. Message string `json:"msg"` }
BaseMessage defines the base message that is used to determine the type of message that is being sent to the MEXC websocket.
func (*BaseMessage) IsEmpty ¶
func (m *BaseMessage) IsEmpty() bool
IsEmpty returns true if no data has been set for the message.
type ChannelType ¶
type ChannelType string
ChannelType defines the type of channel that the client is subscribing to.
type MethodType ¶
type MethodType string
MethodType defines the type of message that is being sent to the MEXC websocket.
type PingRequestMessage ¶
type PingRequestMessage struct {
BaseMessage
}
PingRequestMessage defines the message that is sent to the MEXC websocket to ping the server.
{ "method":"PING" }
ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#live-subscribing-unsubscribing-to-streams
type PongResponseMessage ¶
type PongResponseMessage struct {
BaseMessage
}
PongResponseMessage defines the message that is sent from the server to the client to confirm that the client has successfully pinged the server.
{ "id":0, "code":0, "msg":"PONG" }
ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#live-subscribing-unsubscribing-to-streams
type SubscriptionRequestMessage ¶
type SubscriptionRequestMessage struct { // Method is the method that is being sent to the MEXC websocket. Method string `json:"method"` // Params is the list of channels that the client is subscribing to. This // list cannot exceed 30 channels. Params []string `json:"params"` }
SubscriptionRequestMessage defines the message that is sent to the MEXC websocket to subscribe to a currency pair i.e. market.
{ "method": "SUBSCRIPTION", "params": [ "[email protected]@BTCUSDT" ] }
ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#websocket-market-streams
type SubscriptionResponseMessage ¶
type SubscriptionResponseMessage struct {
BaseMessage
}
SubscriptionResponseMessage defines the message that is sent from the MEXC websocket to confirm that the client has successfully subscribed to a currency pair i.e. market.
{ "id":0, "code":0, "msg":"[email protected]@BTCUSDT" }
ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#live-subscribing-unsubscribing-to-streams
type TickerData ¶
type TickerData struct { // Symbol is the currency pair i.e. market that the ticker data is for. Symbol string `json:"s"` // Price is the latest price for the currency pair i.e. market. Price string `json:"p"` }
TickerData defines the latest ticker data for a currency pair i.e. market.
type TickerResponseMessage ¶
type TickerResponseMessage struct { // Channel is the channel that the client is subscribed to. Channel string `json:"c"` // Data is the latest ticker data for a currency pair i.e. market. Data TickerData `json:"d"` }
TickerResponseMessage defines the message that is sent from the MEXC websocket to provide the latest ticker data for a currency pair i.e. market.
c string channel name d data data >s string symbol >p string deal price >r string price Change Percent in utc8 >tr string price Change Percent in time zone >h string 24h high price >l string 24h low price >v string 24h volume >q string 24h quote Volume >lastRT string etf Last Rebase Time >MT string etf Merge Times >NV string etf Net Value
{ "d": { "s":"BTCUSDT", "p":"36474.74", "r":"0.0354", "tr":"0.0354", "h":"36549.72", "l":"35101.68", "v":"375173478.65", "q":"10557.72895", "lastRT":"-1", "MT":"0", "NV":"--", "t":"1699502456050" }, "c":"[email protected]@BTCUSDT@UTC+8", "t":1699502456051, "s":"BTCUSDT" }
ref: https://mexcdevelop.github.io/apidocs/spot_v3_en/#miniticker
type WebSocketHandler ¶
type WebSocketHandler struct {
// contains filtered or unexported fields
}
WebSocketDataHandler implements the WebSocketDataHandler interface. This is used to handle messages received from the MEXC websocket API.
func (*WebSocketHandler) Copy ¶
func (h *WebSocketHandler) Copy() types.PriceWebSocketDataHandler
Copy is used to create a copy of the data handler.
func (*WebSocketHandler) CreateMessages ¶
func (h *WebSocketHandler) CreateMessages( tickers []types.ProviderTicker, ) ([]handlers.WebsocketEncodedMessage, error)
CreateMessages is used to create a message to send to the data provider. This is used to subscribe to the given ticker. This is called when the connection to the data provider is first established.
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. This is called when a message is received from the data provider. There are three types of messages that can be received from the data provider:
1. A message that confirms that the client has successfully subscribed to a channel. 2. A message that confirms that the client has successfully pinged the server. 3. A message that contains the latest price for a ticker.
func (*WebSocketHandler) HeartBeatMessages ¶
func (h *WebSocketHandler) HeartBeatMessages() ([]handlers.WebsocketEncodedMessage, error)
HeartBeatMessages is used by the MEXC handler to send heart beat messages to the data provider. This is used to keep the connection alive when no messages are being sent from the data provider.
func (*WebSocketHandler) NewSubscribeRequestMessage ¶
func (h *WebSocketHandler) NewSubscribeRequestMessage(instruments []string) ([]handlers.WebsocketEncodedMessage, error)
NewSubscribeRequestMessage returns a new SubscriptionRequestMessage.