Delphi DLL
Delphi DLL
Read Bitfinex WebSocket Ticker Channel
See more WebSocket Examples
Subscribes to the public Bitfinex websocket ticker channel and receives ticker updates.Chilkat Delphi DLL Downloads
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
success: Boolean;
ws: HCkWebSocket;
rest: HCkRest;
responseBody: PWideChar;
json: HCkJsonObject;
finalFrame: Boolean;
receivedFinalFrame: Boolean;
numUpdatesReceived: Integer;
receivedText: PWideChar;
begin
success := False;
// 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 api.bitfinex.com
success := CkRest_Connect(rest,'api.bitfinex.com',443,True,False);
CkWebSocket_UseConnection(ws,rest);
CkWebSocket_AddClientHeaders(ws);
responseBody := CkRest__fullRequestNoBody(rest,'GET','/ws');
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;
// After connecting, the bitfinex websocket server will send
// an info message that contains the actual version of the websocket stream.
// Receive that message..
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;
// We should get this:
// {"event":"info","version":1.1,"platform":{"status":1}}
Memo1.Lines.Add(CkWebSocket__getFrameData(ws));
// Subscribe to the public ticker feed.
// See https://docs.bitfinex.com/docs for more information.
json := CkJsonObject_Create();
CkJsonObject_AppendString(json,'event','subscribe');
CkJsonObject_AppendString(json,'channel','ticker');
CkJsonObject_AppendString(json,'pair','BTCUSD');
finalFrame := True;
success := CkWebSocket_SendFrame(ws,CkJsonObject__emit(json),finalFrame);
// Read the response.
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;
// Examine the response
// We should get this:
// {"event":"subscribed","channel":"ticker","chanId":2751,"pair":"BTCUSD"}
Memo1.Lines.Add(CkWebSocket__getFrameData(ws));
// Begin reading the ticker feed.
// We'll just read the 1st 5 updates and then exit..
receivedFinalFrame := False;
numUpdatesReceived := 0;
while numUpdatesReceived < 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
receivedText := CkWebSocket__getFrameData(ws);
Memo1.Lines.Add(receivedText);
// Should receive a line of text such as this:
// [2751,7349,36.34269559,7349.1,41.01777063,-116.2,-0.0156,7349.1,22188.26055319,7560,7270.5]
numUpdatesReceived := numUpdatesReceived + 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.');
CkWebSocket_Dispose(ws);
CkRest_Dispose(rest);
CkJsonObject_Dispose(json);
end;