Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(AutoIt) WebSocket Binance Trade Stream (subscribe and receive updates)Subscribe to a binance trade stream and receive updates. For more information, see https://binance-docs.github.io/apidocs/spot/en/#live-subscribing-unsubscribing-to-streams
; 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 Local $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: |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.