简体   繁体   中英

Subscribing to a websocket using gorilla/websocket

I want to use some websocket streams of the Binance API . I am posting this here because I think this isn't an issue with the API, but rather my understanding of websockets in general .

I try to subscribe to a stream as the official gorilla/websocket example describes:

conn, res, err := websocket.DefaultDialer.Dial("wss://stream.binance.com/ws/BTCUSD@markPrice", nil)

fmt.Println(conn)
fmt.Println(res)
fmt.Println(err)

for {
    _, message, readErr := conn.ReadMessage()
    if readErr != nil {
        fmt.Println(readErr)
        return
    }
    fmt.Println(message)
}

The connection is created without errors, but after that, no message gets read.

I think the problem is that the API demands me to subscribe to a stream like so:

{
    "method": "SUBSCRIBE",
    "params": [
        "btcusdt@aggTrade",
        "btcusdt@depth"
    ],
    "id": 1
}

I am aware that a websocket connection starts out with one HTTP request where, as far as I understood, I send this data. But where and when am I supposed to do so? Dial doesn't give the option to send anything apart from HTTP headers.

UPDATE:

I managed to send the right request and get the right response as documented here by using Conn.WriteJSON:

type request struct {
    Method string    `json:"method"`
    Params [1]string `json:"params"`
    ID     int       `json:"id"`
}

markPriceReq := request{"SUBSCRIBE", [1]string{"btcusdt@markPrice"}, 1}
conn.WriteJSON(markPriceReq)

However, after the initial response no data gets read anymore. Where do I get the actual mark price data from?

There is an issue with your connection url.

All symbols for stream are lowercase and the ws base endpoint in the document link you posted is wss://stream.binancefuture.com ( Perpetual/Perpetual Tes.net ws endpoint) but you are using wss://stream.binance.com ( Margin/Spot/Savings/Mining ws endpoint).

Mark price stream is only available for Perpetual/Perpetual Tes.net ws endpoint. I use raw stream url : wss://stream.binancefuture.com/ws/btcusdt@markPrice without any issues

This request is for live subscribing/unsubscribing, you only use this when already connected to binance ws and want to subscribe to more streams or unsubscribe to a stream.

{
    "method": "SUBSCRIBE",
    "params": [
        "btcusdt@aggTrade",
        "btcusdt@depth"
    ],
    "id": 1
}

You don't have to use live subscribing/unsubscribing, just use raw url like above is fine:

wss://stream.binancefuture.com/ws/<streamName> (only one stream) wss://stream.binancefuture.com/stream?streams=<streamName1>/<streamName2>/<streamName3> (combined streams)

By using raw url, you are accessed to <streamName> (you can also send a request to use live subscribing/unsubscribing after that)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM