Simple C# websocket to access Binance

Hello,
I am programming in C# and I would like to integrate a websocket into my bot project to retrieve information if possible without using a wrapper or adding other APIs.
I looked for an example without finding.
I have a rather amateur level in C # and I have been going around in circles for several days.
Would anyone like to help me?

Not official library but you can check it out, it has websockets functionality.

Thank you for your reply.
I have been trying to use this API for 10 days without success.
My knowledge of C # is not sufficient to understand this class library examples.
I am taking C # courses online to reach the level.
I think if I could find a little piece of code that worked directly it would unblock me.
I had no problem finding any in Node.js and Python but in C # I couldn’t.

Hello!
I’m in the same situation. Have you decided your problem?

Hello,
No, I haven’t found a simple C # code example for the Binance API.
I am thinking of moving towards independent modules in python to place orders.

Hello!
I’ve found the decision. I use the library WebSocketSharp cause it’s very easy.
I can show an example of the simplest Binance stream in C#. It writes the data to a file.

using System;
using System.Text;
using System.IO;
using WebSocketSharp;

namespace Example
{
class Program
{
static void Main(string args)
{
string SocketUrl = “wss://stream.binance.com:9443/ws/ethbtc@kline_5m”;
string LogFile = @"…\stream.txt";

        using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.Default))
        {
            using (WebSocket ws = new WebSocket(SocketUrl))
            {
                Console.WriteLine("Go");
                ws.OnOpen += (sender, e) =>
                    Console.WriteLine("OnOpen");

                ws.OnMessage += (sender, e) =>
                    sw.WriteLine(e.Data);

                ws.OnClose += (sender, e) =>
                    Console.WriteLine("OnClose");

                try
                {
                    ws.Connect();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.ReadKey();
            }
        }
    }
}

}

3 Likes

This was very helpful. Could you achieve the same result using the native C# methods?