Xbase++
Xbase++
WebSocket Binance Trade Stream (subscribe and receive updates)
See more WebSocket Examples
Subscribe to a binance trade stream and receive updates.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oWs
LOCAL oRest
LOCAL cResponseBody
LOCAL oJson
LOCAL nFinalFrame
LOCAL oJsonTradeData
LOCAL nReceivedFinalFrame
LOCAL nNumTradesReceived
LOCAL cReceivedJson
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oWs := CreateObject("Chilkat.WebSocket")
// For brevity, this example does not check for errors when etablishing the WebSocket connection.
// See Establish WebSocket Connection for more complete sample code for making the connection.
oRest := CreateObject("Chilkat.Rest")
// Connect to wss://stream.binance.com:9443
nSuccess := oRest:Connect("stream.binance.com", 9443, 1, 0)
IF (nSuccess == 0)
? oRest:LastErrorText
oWs:destroy()
oRest:destroy()
RETURN
ENDIF
nSuccess := oWs:UseConnection(oRest)
IF (nSuccess == 0)
? oWs:LastErrorText
oWs:destroy()
oRest:destroy()
RETURN
ENDIF
oWs:AddClientHeaders()
// Raw streams are accessed at /ws/<streamName>
cResponseBody := oRest:FullRequestNoBody("GET", "/ws/btcusdt")
IF (oRest:LastMethodSuccess == 0)
? oRest:LastErrorText
oWs:destroy()
oRest:destroy()
RETURN
ENDIF
nSuccess := oWs:ValidateServerHandshake()
IF (nSuccess != 1)
? oWs:LastErrorText
? cResponseBody
? oRest:ResponseHeader
oWs:destroy()
oRest:destroy()
RETURN
ENDIF
? cResponseBody
? oRest:ResponseHeader
// POST JSON to subscribe to a stream
// {
// "method": "SUBSCRIBE",
// "params":
// [
// "btcusdt@aggTrade",
// "btcusdt@depth"
// ],
// "id": 1
// }
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("method", "SUBSCRIBE")
oJson:UpdateString("params[0]", "btcusdt@aggTrade")
oJson:UpdateString("params[1]", "btcusdt@depth")
oJson:UpdateInt("id", 1)
// Send a full message in a single frame
nFinalFrame := 1
nSuccess := oWs:SendFrame(oJson:Emit(), nFinalFrame)
IF (nSuccess != 1)
? oWs:LastErrorText
oWs:destroy()
oRest:destroy()
oJson:destroy()
RETURN
ENDIF
oJsonTradeData := CreateObject("Chilkat.JsonObject")
oJsonTradeData:EmitCompact := 0
// Begin reading the trade stream response.
// We'll just read the 1st 10 updates and then exit..
nReceivedFinalFrame := 0
nNumTradesReceived := 0
DO WHILE nNumTradesReceived < 5
nSuccess := oWs:ReadFrame()
IF (nSuccess != 1)
? "Failed to receive a frame"
? "ReadFrame fail reason = " + Str(oWs:ReadFrameFailReason)
? oWs:LastErrorText
oWs:destroy()
oRest:destroy()
oJson:destroy()
oJsonTradeData:destroy()
RETURN
ENDIF
// The responses we desire are in Text frames, where the opcode = 1.
IF (oWs:FrameOpcodeInt == 1)
cReceivedJson := oWs:GetFrameData()
oJsonTradeData:Load(cReceivedJson)
? oJsonTradeData:Emit()
nNumTradesReceived := nNumTradesReceived + 1
ENDIF
ENDDO
// Close the websocket connection.
nSuccess := oWs:SendClose(1, 1000, "Closing this websocket.")
IF (nSuccess != 1)
? oWs:LastErrorText
oWs:destroy()
oRest:destroy()
oJson:destroy()
oJsonTradeData:destroy()
RETURN
ENDIF
// Read the Close response.
nSuccess := oWs:ReadFrame()
IF (nSuccess != 1)
? "ReadFrame fail reason = " + Str(oWs:ReadFrameFailReason)
? oWs:LastErrorText
oWs:destroy()
oRest:destroy()
oJson:destroy()
oJsonTradeData:destroy()
RETURN
ENDIF
? "Success."
// The output of the above code is shown here:
oWs:destroy()
oRest:destroy()
oJson:destroy()
oJsonTradeData:destroy()