I'm trying to connect using websocket to Binary.com (a binary trading platform) server. Their API Guide (showed in https://developers.binary.com/demos/) doesn't have any Delphi examples. I've tried to use some library like from websockets.esegece.com and http://github.com/andremussche/DelphiWebsockets but I didn't have any clue how do they work even after I read manual and examples. Some examples show how to create a websocket servers and clients and it works, but I've never been succeed to connect to the Binary.com server. For example when using andremussche's library, I didn't know how to connect using wss protocol and with sgc's library I keep getting Socket Error 10054.
Here are some example API using JavaScript and C# taken from their website
var ws = new WebSocket('wss://ws.binaryws.com/websockets/v3');
ws.onopen = function(evt) {
ws.send(JSON.stringify({ticks:'R_100'}));
};
ws.onmessage = function(msg) {
var data = JSON.parse(msg.data);
console.log('ticks update: %o', data);
};
using System;
using System.Text;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Threading;
using System.Net;
namespace ConsoleApp
{
class Program
{
static async Task SendTicksRequest()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
var ws = new ClientWebSocket();
var uri = new Uri("wss://ws.binaryws.com/websockets/v3");
await ws.ConnectAsync(uri, CancellationToken.None);
var reqAsBytes = Encoding.UTF8.GetBytes("{\"ticks\":\"R_100\"}");
var ticksRequest = new ArraySegment<byte>(reqAsBytes);
await ws.SendAsync(ticksRequest,
WebSocketMessageType.Text,
true,
CancellationToken.None);
var buffer = new ArraySegment<byte>(new byte[1024]);
var result = await ws.ReceiveAsync(buffer, CancellationToken.None);
string response = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
Console.WriteLine(response);
}
static void Main(string[] args)
{
SendTicksRequest();
Console.ReadLine();
}
}
}
Anybody know how to get the same results using Delphi and which library should I use? Any help would be appreciated, thanks.
Answering @Ken White, This my code that didn't work using andremussche's library
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses IdHTTPWebsocketClient;
var client: TIdHTTPWebsocketClient;
procedure TForm1.Button1Click(Sender: TObject);
begin
client := TIdHTTPWebsocketClient.Create(Self);
client.Port := 443;
client.Host := 'ws.binaryws.com';
client.Connect;
client.UpgradeToWebsocket;
end;
end.
It says HTTP Bad Request, seems like the request made with HTTP protocol, not WSS. I also don't know what property to use to write the URL path.
I also try this code
procedure TForm1.Button1Click(Sender: TObject);
var strm: TMemoryStream;
begin
client := TIdHTTPWebsocketClient.Create(Self);
client.Port := 443;
client.Host := 'ws.binaryws.com';
client.SocketIOCompatible := False;
strm := TMemoryStream.Create;
client.Get('wss://ws.binaryws.com/websockets/v3', strm);
end;
It says Unknown Protocol, so what should I do?