WebSocket4Net websocket.Send

Tell me what I'm doing wrong? This name does not exist in this context. Show me how to make a request correctly.
C#

class Program
{

     public static void Main(string[] args)
    {

        using (WebSocket websocket = new WebSocket("wss://ws.binaryws.com/websockets/v3?app_id=xxx"))
        {
            websocket.Opened += new EventHandler(websocket_Opened);

            websocket.Closed += new EventHandler(websocket_Closed);
            websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
            websocket.Open();

        }
    }
Tagged:

Comments

  • First, are you using the "using WebSocket4Net;" directive on top?

  • Will Need to see the complete code In order to help.

  • @Gabms said:
    First, are you using the "using WebSocket4Net;" directive on top?

    Yes, of course, I attach my code below.

  • I looked at a lot of examples and wrote my own code from them, but I didn't fully understand how to use this library. Everything works well with WebSocket, but as I understand there are problems with It, so I do it on WebSocket4Net.

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using WebSocket4Net;

    namespace Binary1
    {

    class Program
    {
    
    
    
         public static void Main(string[] args)
        {
    
            using (WebSocket websocket = new WebSocket("wss://ws.binaryws.com/websockets/v3?app_id=хххх"))
            {
                websocket.Opened += new EventHandler(websocket_Opened);
    
                websocket.Closed += new EventHandler(websocket_Closed);
                websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
                websocket.Open();
    
            }
        }
    
    
        private static void websocket_Opened(object sender, EventArgs e)
        {
    
    
    
            string auth_key = "{\"authorize\":  XXXXXXXXX }";
            string dataPUT = "{\"subscribe\": 1, \"proposal\": 1,\"amount\": 20, \"basis\": \"stake\", \"contract_type\": \"PUT\", \"currency\": \"USD\", \"duration\": \"5\", \"duration_unit\": \"t\", \"symbol\": \"frxEURUSD\"}";
            string dataCALL = "{\"subscribe\": 1, \"proposal\": 1,\"amount\": 20, \"basis\": \"stake\", \"contract_type\": \"CALL\", \"currency\": \"USD\", \"duration\": \"5\", \"duration_unit\": \"t\", \"symbol\": \"frxEURUSD\"}";
            string dataTICK = "{\"ticks\": \"frxEURUSD\"}";
    
            websocket.Send(auth_key);
            websocket.Send(dataTICK);
            Console.WriteLine("Sent request: " + auth_key);
            Console.WriteLine("Sent request: " + dataTICK);
            Execute();
        }
    
    
        public async void Execute()
        {
            string balance = "{\"balance\": \"1\", \"subscribe\": \"1\"}";
            await Task.Delay(3000);
            websocket.Send(balance);
            Console.WriteLine(balance);
        }
    
    
    
        private static void websocket_MessageReceived(object sender, MessageReceivedEventArgs msg)
        {
            //HelperClass.addLog(msg.Message);
            Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fff") + "] " + msg.Message);
    
            var json = (JObject)JsonConvert.DeserializeObject(msg.Message);
            var e = json["echo_req"];
            var b = json["buy"];
            var p = json["proposal"];
            var t = json["tick"];
            if (json["balance"] != null)
            {
                Console.WriteLine(e);
            }
            else if (json["error"] != null)
            {
    
                return;
            }
    
            if ((string)e["ticks"] == "frxEURUSD")
            {
                Console.WriteLine(e);
            }
    
        }
    
        private void websocket_Closed(object sender, EventArgs e)
        {
    
            Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] Websocket closed");
        }
    
    }   
    

    }

  • Hi @Fearnt, I recently had some problems connecting to the API, I was using the same package (WebSocket4Net) at the end I changed the package for another one and it worked, it looks like WebSocket4Net has some issues with TLS1.2.

    Please take a look at the following post, you may find it useful,
    https://binary.vanillacommunity.com/discussion/684/error-when-trying-to-connect-to-the-api-recent-api-changes#latest

  • @robaez2 said:
    Hi @Fearnt, I recently had some problems connecting to the API, I was using the same package (WebSocket4Net) at the end I changed the package for another one and it worked, it looks like WebSocket4Net has some issues with TLS1.2.

    Please take a look at the following post, you may find it useful,
    https://binary.vanillacommunity.com/discussion/684/error-when-trying-to-connect-to-the-api-recent-api-changes#latest

    Thanks for the link, but I found how to fix my problem)

Sign In or Register to comment.