1

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?

Adhi Sakti
  • 11
  • 4
  • Try the suggested library in this question: http://stackoverflow.com/questions/25181302/websocket-client-implementations-for-delphi – quasoft Feb 24 '16 at 17:49
  • @quasoft I've been tried them both library, and either it didn't work at my case or i don't know how to use it properly – Adhi Sakti Feb 24 '16 at 18:21
  • There's a link to a sample app in Andre's answer to the linked post. Did you look at it? https://github.com/andremussche/DelphiWebsockets/blob/master/DUnit/mtTestWebSockets.pas – Ken White Feb 24 '16 at 22:00
  • I've recently added WebSocket support to xxm: https://github.com/stijnsanders/xxm/tree/master/Delphi/demo2/13%20WebSockets – Stijn Sanders Feb 24 '16 at 22:39
  • @KenWhite I updated my question to show my work – Adhi Sakti Feb 25 '16 at 17:44
  • @StijnSanders I'll check it out – Adhi Sakti Feb 25 '16 at 17:45
  • In case you're interested, I just now also built in [support for EventSource](https://github.com/stijnsanders/xxm/blob/master/Delphi/demo2/12%20Long%20Polling/ES.xxm), and may move to release version 1.2.4 sometime soon. – Stijn Sanders Feb 26 '16 at 21:07

0 Answers0