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
(Visual FoxPro) 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
LOCAL loWs LOCAL loRest LOCAL lnSuccess LOCAL lcResponseBody LOCAL loJson LOCAL lnFinalFrame LOCAL loJsonTradeData LOCAL lnReceivedFinalFrame LOCAL lnNumTradesReceived LOCAL lcReceivedJson * This example requires the Chilkat API to have been previously unlocked. * See Global Unlock Sample for sample code. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.WebSocket') loWs = 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. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Rest') loRest = CreateObject('Chilkat.Rest') * Connect to wss://stream.binance.com:9443 lnSuccess = loRest.Connect("stream.binance.com",9443,1,0) IF (lnSuccess = 0) THEN ? loRest.LastErrorText RELEASE loWs RELEASE loRest CANCEL ENDIF lnSuccess = loWs.UseConnection(loRest) IF (lnSuccess = 0) THEN ? loWs.LastErrorText RELEASE loWs RELEASE loRest CANCEL ENDIF loWs.AddClientHeaders() * Raw streams are accessed at /ws/<streamName> lcResponseBody = loRest.FullRequestNoBody("GET","/ws/btcusdt") IF (loRest.LastMethodSuccess = 0) THEN ? loRest.LastErrorText RELEASE loWs RELEASE loRest CANCEL ENDIF lnSuccess = loWs.ValidateServerHandshake() IF (lnSuccess <> 1) THEN ? loWs.LastErrorText ? lcResponseBody ? loRest.ResponseHeader RELEASE loWs RELEASE loRest CANCEL ENDIF ? lcResponseBody ? loRest.ResponseHeader * POST JSON to subscribe to a stream * { * "method": "SUBSCRIBE", * "params": * [ * "btcusdt@aggTrade", * "btcusdt@depth" * ], * "id": 1 * } * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJson = CreateObject('Chilkat.JsonObject') loJson.UpdateString("method","SUBSCRIBE") loJson.UpdateString("params[0]","btcusdt@aggTrade") loJson.UpdateString("params[1]","btcusdt@depth") loJson.UpdateInt("id",1) * Send a full message in a single frame lnFinalFrame = 1 lnSuccess = loWs.SendFrame(loJson.Emit(),lnFinalFrame) IF (lnSuccess <> 1) THEN ? loWs.LastErrorText RELEASE loWs RELEASE loRest RELEASE loJson CANCEL ENDIF * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJsonTradeData = CreateObject('Chilkat.JsonObject') loJsonTradeData.EmitCompact = 0 * Begin reading the trade stream response. * We'll just read the 1st 10 updates and then exit.. lnReceivedFinalFrame = 0 lnNumTradesReceived = 0 DO WHILE lnNumTradesReceived < 5 lnSuccess = loWs.ReadFrame() IF (lnSuccess <> 1) THEN ? "Failed to receive a frame" ? "ReadFrame fail reason = " + STR(loWs.ReadFrameFailReason) ? loWs.LastErrorText RELEASE loWs RELEASE loRest RELEASE loJson RELEASE loJsonTradeData CANCEL ENDIF * The responses we desire are in Text frames, where the opcode = 1. IF (loWs.FrameOpcodeInt = 1) THEN lcReceivedJson = loWs.GetFrameData() loJsonTradeData.Load(lcReceivedJson) ? loJsonTradeData.Emit() lnNumTradesReceived = lnNumTradesReceived + 1 ENDIF ENDDO * Close the websocket connection. lnSuccess = loWs.SendClose(1,1000,"Closing this websocket.") IF (lnSuccess <> 1) THEN ? loWs.LastErrorText RELEASE loWs RELEASE loRest RELEASE loJson RELEASE loJsonTradeData CANCEL ENDIF * Read the Close response. lnSuccess = loWs.ReadFrame() IF (lnSuccess <> 1) THEN ? "ReadFrame fail reason = " + STR(loWs.ReadFrameFailReason) ? loWs.LastErrorText RELEASE loWs RELEASE loRest RELEASE loJson RELEASE loJsonTradeData CANCEL ENDIF ? "Success." * The output of the above code is shown here: RELEASE loWs RELEASE loRest RELEASE loJson RELEASE loJsonTradeData |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.