PureBasic
PureBasic
WebSocket Binance Trade Stream (subscribe and receive updates)
See more WebSocket Examples
Subscribe to a binance trade stream and receive updates.Chilkat PureBasic Downloads
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkWebSocket.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
ws.i = CkWebSocket::ckCreate()
If ws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; 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.
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect to wss://stream.binance.com:9443
success = CkRest::ckConnect(rest,"stream.binance.com",9443,1,0)
If success = 0
Debug CkRest::ckLastErrorText(rest)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
success = CkWebSocket::ckUseConnection(ws,rest)
If success = 0
Debug CkWebSocket::ckLastErrorText(ws)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
CkWebSocket::ckAddClientHeaders(ws)
; Raw streams are accessed at /ws/<streamName>
responseBody.s = CkRest::ckFullRequestNoBody(rest,"GET","/ws/btcusdt")
If CkRest::ckLastMethodSuccess(rest) = 0
Debug CkRest::ckLastErrorText(rest)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
success = CkWebSocket::ckValidateServerHandshake(ws)
If success <> 1
Debug CkWebSocket::ckLastErrorText(ws)
Debug responseBody
Debug CkRest::ckResponseHeader(rest)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
Debug responseBody
Debug CkRest::ckResponseHeader(rest)
; POST JSON to subscribe to a stream
; {
; "method": "SUBSCRIBE",
; "params":
; [
; "btcusdt@aggTrade",
; "btcusdt@depth"
; ],
; "id": 1
; }
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"method","SUBSCRIBE")
CkJsonObject::ckUpdateString(json,"params[0]","btcusdt@aggTrade")
CkJsonObject::ckUpdateString(json,"params[1]","btcusdt@depth")
CkJsonObject::ckUpdateInt(json,"id",1)
; Send a full message in a single frame
finalFrame.i = 1
success = CkWebSocket::ckSendFrame(ws,CkJsonObject::ckEmit(json),finalFrame)
If success <> 1
Debug CkWebSocket::ckLastErrorText(ws)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
ProcedureReturn
EndIf
jsonTradeData.i = CkJsonObject::ckCreate()
If jsonTradeData.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::setCkEmitCompact(jsonTradeData, 0)
; Begin reading the trade stream response.
; We'll just read the 1st 10 updates and then exit..
receivedFinalFrame.i = 0
numTradesReceived.i = 0
While numTradesReceived < 5
success = CkWebSocket::ckReadFrame(ws)
If success <> 1
Debug "Failed to receive a frame"
Debug "ReadFrame fail reason = " + Str(CkWebSocket::ckReadFrameFailReason(ws))
Debug CkWebSocket::ckLastErrorText(ws)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonTradeData)
ProcedureReturn
EndIf
; The responses we desire are in Text frames, where the opcode = 1.
If CkWebSocket::ckFrameOpcodeInt(ws) = 1
receivedJson.s = CkWebSocket::ckGetFrameData(ws)
CkJsonObject::ckLoad(jsonTradeData,receivedJson)
Debug CkJsonObject::ckEmit(jsonTradeData)
numTradesReceived = numTradesReceived + 1
EndIf
Wend
; Close the websocket connection.
success = CkWebSocket::ckSendClose(ws,1,1000,"Closing this websocket.")
If success <> 1
Debug CkWebSocket::ckLastErrorText(ws)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonTradeData)
ProcedureReturn
EndIf
; Read the Close response.
success = CkWebSocket::ckReadFrame(ws)
If success <> 1
Debug "ReadFrame fail reason = " + Str(CkWebSocket::ckReadFrameFailReason(ws))
Debug CkWebSocket::ckLastErrorText(ws)
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonTradeData)
ProcedureReturn
EndIf
Debug "Success."
; The output of the above code is shown here:
CkWebSocket::ckDispose(ws)
CkRest::ckDispose(rest)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonTradeData)
ProcedureReturn
EndProcedure