Convert C# code to use WEbsocket4net becasue of

Hi

I was wondering if someone can help me convert this script for C# which uses websockets to rather use Websocket4net because as i read Websockets causes timeout issues.

using System;
using System.Text;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Threading;
using System.Net;

namespace BinaryWSDemo
{
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,
            System.Net.WebSockets.WebSocketMessageType.Text,
            true,
            CancellationToken.None);

        Console.WriteLine("The request has been sent: ");
        Console.WriteLine(data);
        Console.WriteLine("\r\n \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!");
                    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");
                }

            } 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");
    }

    static void Main(string[] args)
    {
        string authorize = "{\"authorize\":\"xxxxxxxxxxx\"}";
        string proposal_open_contract = "{\"portfolio\": 1}";
        string transactionsubscribe = "{\"transaction\": 1,\"subscribe\":1}";

        var bws = new BinaryWS();
        bws.Connect().Wait();

        bws.SendRequest(authorize).Wait();
        bws.SendRequest(proposal_open_contract).Wait();
        bws.SendRequest(transactionsubscribe).Wait();

        bws.StartListen().Wait();

        Console.ReadLine();
    }
}

}

Sign In or Register to comment.