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
(Delphi DLL) 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
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Rest, WebSocket, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var ws: HCkWebSocket; rest: HCkRest; success: Boolean; responseBody: PWideChar; json: HCkJsonObject; finalFrame: Boolean; jsonTradeData: HCkJsonObject; receivedFinalFrame: Boolean; numTradesReceived: Integer; receivedJson: PWideChar; begin // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. ws := CkWebSocket_Create(); // 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 := CkRest_Create(); // Connect to wss://stream.binance.com:9443 success := CkRest_Connect(rest,'stream.binance.com',9443,True,False); if (success = False) then begin Memo1.Lines.Add(CkRest__lastErrorText(rest)); Exit; end; success := CkWebSocket_UseConnection(ws,rest); if (success = False) then begin Memo1.Lines.Add(CkWebSocket__lastErrorText(ws)); Exit; end; CkWebSocket_AddClientHeaders(ws); // Raw streams are accessed at /ws/<streamName> responseBody := CkRest__fullRequestNoBody(rest,'GET','/ws/btcusdt'); if (CkRest_getLastMethodSuccess(rest) = False) then begin Memo1.Lines.Add(CkRest__lastErrorText(rest)); Exit; end; success := CkWebSocket_ValidateServerHandshake(ws); if (success <> True) then begin Memo1.Lines.Add(CkWebSocket__lastErrorText(ws)); Memo1.Lines.Add(responseBody); Memo1.Lines.Add(CkRest__responseHeader(rest)); Exit; end; Memo1.Lines.Add(responseBody); Memo1.Lines.Add(CkRest__responseHeader(rest)); // POST JSON to subscribe to a stream // { // "method": "SUBSCRIBE", // "params": // [ // "btcusdt@aggTrade", // "btcusdt@depth" // ], // "id": 1 // } json := CkJsonObject_Create(); CkJsonObject_UpdateString(json,'method','SUBSCRIBE'); CkJsonObject_UpdateString(json,'params[0]','btcusdt@aggTrade'); CkJsonObject_UpdateString(json,'params[1]','btcusdt@depth'); CkJsonObject_UpdateInt(json,'id',1); // Send a full message in a single frame finalFrame := True; success := CkWebSocket_SendFrame(ws,CkJsonObject__emit(json),finalFrame); if (success <> True) then begin Memo1.Lines.Add(CkWebSocket__lastErrorText(ws)); Exit; end; jsonTradeData := CkJsonObject_Create(); CkJsonObject_putEmitCompact(jsonTradeData,False); // Begin reading the trade stream response. // We'll just read the 1st 10 updates and then exit.. receivedFinalFrame := False; numTradesReceived := 0; while numTradesReceived < 5 do begin success := CkWebSocket_ReadFrame(ws); if (success <> True) then begin Memo1.Lines.Add('Failed to receive a frame'); Memo1.Lines.Add('ReadFrame fail reason = ' + IntToStr(CkWebSocket_getReadFrameFailReason(ws))); Memo1.Lines.Add(CkWebSocket__lastErrorText(ws)); Exit; end; // The responses we desire are in Text frames, where the opcode = 1. if (CkWebSocket_getFrameOpcodeInt(ws) = 1) then begin receivedJson := CkWebSocket__getFrameData(ws); CkJsonObject_Load(jsonTradeData,receivedJson); Memo1.Lines.Add(CkJsonObject__emit(jsonTradeData)); numTradesReceived := numTradesReceived + 1; end; end; // Close the websocket connection. success := CkWebSocket_SendClose(ws,True,1000,'Closing this websocket.'); if (success <> True) then begin Memo1.Lines.Add(CkWebSocket__lastErrorText(ws)); Exit; end; // Read the Close response. success := CkWebSocket_ReadFrame(ws); if (success <> True) then begin Memo1.Lines.Add('ReadFrame fail reason = ' + IntToStr(CkWebSocket_getReadFrameFailReason(ws))); Memo1.Lines.Add(CkWebSocket__lastErrorText(ws)); Exit; end; Memo1.Lines.Add('Success.'); // The output of the above code is shown here: CkWebSocket_Dispose(ws); CkRest_Dispose(rest); CkJsonObject_Dispose(json); CkJsonObject_Dispose(jsonTradeData); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.