Delphi ActiveX
Delphi ActiveX
WebSocket Binance Trade Stream (subscribe and receive updates)
See more WebSocket Examples
Subscribe to a binance trade stream and receive updates.Chilkat Delphi ActiveX Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
ws: TChilkatWebSocket;
rest: TChilkatRest;
responseBody: WideString;
json: TChilkatJsonObject;
finalFrame: Integer;
jsonTradeData: TChilkatJsonObject;
receivedFinalFrame: Integer;
numTradesReceived: Integer;
receivedJson: WideString;
begin
success := 0;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
ws := TChilkatWebSocket.Create(Self);
// 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 := TChilkatRest.Create(Self);
// Connect to wss://stream.binance.com:9443
success := rest.Connect('stream.binance.com',9443,1,0);
if (success = 0) then
begin
Memo1.Lines.Add(rest.LastErrorText);
Exit;
end;
success := ws.UseConnection(rest.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(ws.LastErrorText);
Exit;
end;
ws.AddClientHeaders();
// Raw streams are accessed at /ws/<streamName>
responseBody := rest.FullRequestNoBody('GET','/ws/btcusdt');
if (rest.LastMethodSuccess = 0) then
begin
Memo1.Lines.Add(rest.LastErrorText);
Exit;
end;
success := ws.ValidateServerHandshake();
if (success <> 1) then
begin
Memo1.Lines.Add(ws.LastErrorText);
Memo1.Lines.Add(responseBody);
Memo1.Lines.Add(rest.ResponseHeader);
Exit;
end;
Memo1.Lines.Add(responseBody);
Memo1.Lines.Add(rest.ResponseHeader);
// POST JSON to subscribe to a stream
// {
// "method": "SUBSCRIBE",
// "params":
// [
// "btcusdt@aggTrade",
// "btcusdt@depth"
// ],
// "id": 1
// }
json := TChilkatJsonObject.Create(Self);
json.UpdateString('method','SUBSCRIBE');
json.UpdateString('params[0]','btcusdt@aggTrade');
json.UpdateString('params[1]','btcusdt@depth');
json.UpdateInt('id',1);
// Send a full message in a single frame
finalFrame := 1;
success := ws.SendFrame(json.Emit(),finalFrame);
if (success <> 1) then
begin
Memo1.Lines.Add(ws.LastErrorText);
Exit;
end;
jsonTradeData := TChilkatJsonObject.Create(Self);
jsonTradeData.EmitCompact := 0;
// Begin reading the trade stream response.
// We'll just read the 1st 10 updates and then exit..
receivedFinalFrame := 0;
numTradesReceived := 0;
while numTradesReceived < 5 do
begin
success := ws.ReadFrame();
if (success <> 1) then
begin
Memo1.Lines.Add('Failed to receive a frame');
Memo1.Lines.Add('ReadFrame fail reason = ' + IntToStr(ws.ReadFrameFailReason));
Memo1.Lines.Add(ws.LastErrorText);
Exit;
end;
// The responses we desire are in Text frames, where the opcode = 1.
if (ws.FrameOpcodeInt = 1) then
begin
receivedJson := ws.GetFrameData();
jsonTradeData.Load(receivedJson);
Memo1.Lines.Add(jsonTradeData.Emit());
numTradesReceived := numTradesReceived + 1;
end;
end;
// Close the websocket connection.
success := ws.SendClose(1,1000,'Closing this websocket.');
if (success <> 1) then
begin
Memo1.Lines.Add(ws.LastErrorText);
Exit;
end;
// Read the Close response.
success := ws.ReadFrame();
if (success <> 1) then
begin
Memo1.Lines.Add('ReadFrame fail reason = ' + IntToStr(ws.ReadFrameFailReason));
Memo1.Lines.Add(ws.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Success.');
// The output of the above code is shown here:
end;