using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.WebSockets;
using System.Threading;
using System.Net;
namespace ambil_spot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string data = "{\"ticks\":\"R_100\"}";
var bws = new BinaryWS();
bws.Connect().Wait();
bws.SendRequest(data).Wait();
bws.StartListen();
}
}
class BinaryWS
{
private ClientWebSocket ws = new ClientWebSocket();
private Uri uri = new Uri("wss://ws.binaryws.com/websockets/v3");
public async Task SendRequest(string data)
{
while (this.ws.State == WebSocketState.Connecting) { };
if (this.ws.State != WebSocketState.Open)
{
throw new Exception("Connection is not open.");
}
var reqAsBytes = Encoding.UTF8.GetBytes(data);
var ticksRequest = new ArraySegment<byte>(reqAsBytes);
await this.ws.SendAsync(ticksRequest,
WebSocketMessageType.Text,
true,
CancellationToken.None);
//Console.WriteLine("The request has been sent: ");
// Console.WriteLine(data);
//Console.WriteLine("\r\n \r\n");
textBox1.Text += "The request has been sent: " + "\r\n";
}
public async Task StartListen()
{
WebSocketReceiveResult result;
while (this.ws.State == WebSocketState.Open)
{
var buffer = new ArraySegment<byte>(new byte[1024]);
do
{
result = await this.ws.ReceiveAsync(new ArraySegment<byte>(buffer.Array), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
//Console.WriteLine("Connection Closed!");
textBox1.Text += "Connection Closed!" + "\r\n";
break;
}
else
{
var str = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
//Console.WriteLine("Received Data at: " + DateTime.Now);
// Console.WriteLine(str);
// Console.WriteLine("\r\n");
textBox1.Text += str + "\r\n";
}
} while (!result.EndOfMessage);
}
}
public async Task Connect()
{
//Console.WriteLine("Prepare to connect to: " + this.uri.ToString());
//Console.WriteLine("\r\n");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
await ws.ConnectAsync(uri, CancellationToken.None);
//Console.WriteLine("The connection is established!");
//Console.WriteLine("\r\n");
textBox1.Text += "The connection is established!" + "\r\n";
}
}
Comments
@marinus please paste the error
i make like this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.WebSockets;
using System.Threading;
using System.Net;
namespace ambil_spot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
@marinus what error you are facing with this code can you point out or print that error? As starting point you can refer code here as well https://developers.binary.com/demos/
You need to authorize first before requesting tick stream/history. Check this https://developers.binary.com/api/#authorize
@charmx no its not needed, tick stream/history are unauthorized calls
thanks, @Raunak and @charmx i fixed that;