AutoIt
AutoIt
WebSocket Binance Trade Stream (subscribe and receive updates)
See more WebSocket Examples
Subscribe to a binance trade stream and receive updates.Chilkat AutoIt Downloads
Local $bSuccess = False
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oWs = ObjCreate("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 = ObjCreate("Chilkat.Rest")
; Connect to wss://stream.binance.com:9443
$bSuccess = $oRest.Connect("stream.binance.com",9443,True,False)
If ($bSuccess = False) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oWs.UseConnection($oRest)
If ($bSuccess = False) Then
ConsoleWrite($oWs.LastErrorText & @CRLF)
Exit
EndIf
$oWs.AddClientHeaders()
; Raw streams are accessed at /ws/<streamName>
Local $sResponseBody = $oRest.FullRequestNoBody("GET","/ws/btcusdt")
If ($oRest.LastMethodSuccess = False) Then
ConsoleWrite($oRest.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oWs.ValidateServerHandshake()
If ($bSuccess <> True) Then
ConsoleWrite($oWs.LastErrorText & @CRLF)
ConsoleWrite($sResponseBody & @CRLF)
ConsoleWrite($oRest.ResponseHeader & @CRLF)
Exit
EndIf
ConsoleWrite($sResponseBody & @CRLF)
ConsoleWrite($oRest.ResponseHeader & @CRLF)
; POST JSON to subscribe to a stream
; {
; "method": "SUBSCRIBE",
; "params":
; [
; "btcusdt@aggTrade",
; "btcusdt@depth"
; ],
; "id": 1
; }
$oJson = ObjCreate("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
Local $bFinalFrame = True
$bSuccess = $oWs.SendFrame($oJson.Emit(),$bFinalFrame)
If ($bSuccess <> True) Then
ConsoleWrite($oWs.LastErrorText & @CRLF)
Exit
EndIf
$oJsonTradeData = ObjCreate("Chilkat.JsonObject")
$oJsonTradeData.EmitCompact = False
; Begin reading the trade stream response.
; We'll just read the 1st 10 updates and then exit..
Local $bReceivedFinalFrame = False
Local $iNumTradesReceived = 0
While $iNumTradesReceived < 5
$bSuccess = $oWs.ReadFrame()
If ($bSuccess <> True) Then
ConsoleWrite("Failed to receive a frame" & @CRLF)
ConsoleWrite("ReadFrame fail reason = " & $oWs.ReadFrameFailReason & @CRLF)
ConsoleWrite($oWs.LastErrorText & @CRLF)
Exit
EndIf
; The responses we desire are in Text frames, where the opcode = 1.
If ($oWs.FrameOpcodeInt = 1) Then
Local $sReceivedJson = $oWs.GetFrameData()
$oJsonTradeData.Load($sReceivedJson)
ConsoleWrite($oJsonTradeData.Emit() & @CRLF)
$iNumTradesReceived = $iNumTradesReceived + 1
EndIf
Wend
; Close the websocket connection.
$bSuccess = $oWs.SendClose(True,1000,"Closing this websocket.")
If ($bSuccess <> True) Then
ConsoleWrite($oWs.LastErrorText & @CRLF)
Exit
EndIf
; Read the Close response.
$bSuccess = $oWs.ReadFrame()
If ($bSuccess <> True) Then
ConsoleWrite("ReadFrame fail reason = " & $oWs.ReadFrameFailReason & @CRLF)
ConsoleWrite($oWs.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("Success." & @CRLF)
; The output of the above code is shown here: