5 Replies - 358 Views - Last Post: 09 May 2021 - 04:44 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 250
  • Joined: 01-July 12

.net core binance.net update view in real time from data model webso

Posted 07 May 2021 - 09:43 AM

Hello all,
I have a .Net Core application and Im using the Binance.Net API. I want to show the trades coming in real time and update my View with the current trades. Im not sure the correct way to do this. Possibly ajax? The data that is coming in is all c# so that makes it harder where I cant use javascript websockets to update the UI. Unless there is a way around this.
I have a View and also a partialview with a listbox in it. Currently as the websocket data comes in I add it to a model and if I click the button in my partial the listbox shows the recent trades. So I am partially there.

public async Task<IActionResult> Index()
        {
            _logger.LogInformation("Start Capture");
            //var price = await _marketFeed.StartBTCCapture();

            //var incomingBTC = await _binanceSocketClient.SubscribeToTradeUpdatesAsync("BTCUSD", (data) =>
            //{
            //    //(data);
            //    // _logger.LogInformation(data.Price.ToString());
            //    Console.WriteLine(data.Price);
            //});
            List<TradeResult> trades = new List<TradeResult>();
            await GetPartial(trades);
            //_logger.LogInformation(price.Data);
            //var conn = new WebSocket();

            //HttpListener httpListener = new HttpListener();
            //httpListener.Prefixes.Add("ws://localhost:50021/StartBTCCapture");
            //httpListener.Start();
            //var buffer = new byte[1024 * 4];

            //HttpListenerContext context = await httpListener.GetContextAsync();
            //if (context.Request.IsWebSocketRequest)
            //{
            //    HttpListenerWebSocketContext webSocketContext = await context.AcceptWebSocketAsync(null);
            //    WebSocket webSocket = webSocketContext.WebSocket;
            //    while (webSocket.State == WebSocketState.Open)
            //    {
            //        await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
            //    }
            //}

            return View();
        }

        public async Task<IActionResult> GetPartial(List<TradeResult> trades)
        {


            var incomingBTC = await _binanceSocketClient.SubscribeToTradeUpdatesAsync("BTCUSD", (data) =>
            {
                //(data);
                // _logger.LogInformation(data.Price.ToString());
                Console.WriteLine(data.Price);
                trades.Add(new TradeResult
                {
                    Price = data.Price
                });

            });

            return PartialView("BoxView", trades);
        }
//Partial
<div class="container">
    <form>
        <table>
            <tr>
                <th>Price</th>
            </tr>
            @foreach(var obj in Model)
            {
                <tr>
                    <td>
                        @obj.Price.ToString()
                    </td>
                </tr>
            }
        </table>
        <input type="submit" value="btn" />
    </form>
</div>


Is This A Good Question/Topic? 0
  • +

Replies To: .net core binance.net update view in real time from data model webso

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16253
  • View blog
  • Posts: 64,719
  • Joined: 12-June 08

Re: .net core binance.net update view in real time from data model webso

Posted 07 May 2021 - 12:36 PM

I've never used binance .net, but looks like there are sockets you can subscribe to and, I think, get data pushed directly where the UI can be updated.

https://github.com/JKorf/Binance.Net
Was This Post Helpful? 0
  • +
  • -

#3 Exceedinglife   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 250
  • Joined: 01-July 12

Re: .net core binance.net update view in real time from data model webso

Posted 07 May 2021 - 09:11 PM

View Postmodi123_1, on 07 May 2021 - 12:36 PM, said:

I've never used binance .net, but looks like there are sockets you can subscribe to and, I think, get data pushed directly where the UI can be updated.

https://github.com/JKorf/Binance.Net


Yeah, there are websockets in the API. I used them for my WPF app. Now i'm building an ASP.Net Core Web Project, The data is coming in as C#. With this I am not sure how to update the View.
A workaround I did for now is I implemented SignalR and called binance directly. With this the data is coming in Javascript and I can update my UI with ajax. so I have it working, is there a way to do it with the c# direction?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7788
  • View blog
  • Posts: 26,018
  • Joined: 05-May 12

Re: .net core binance.net update view in real time from data model webso

Posted 08 May 2021 - 03:52 AM

Data can't come in as C#. C# is not a data format.
Was This Post Helpful? 0
  • +
  • -

#5 Exceedinglife   User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 250
  • Joined: 01-July 12

Re: .net core binance.net update view in real time from data model webso

Posted 09 May 2021 - 04:19 PM

How it is C# is like this.

I use the API and do the API Websocket call.
All the data comes back as c#.



 public class MarketFeed : IMarketFeed
    {
        readonly IBinanceClient _binanceClient;
        readonly IBinanceSocketClient _binanceSocketClient;

        public MarketFeed(IBinanceClient binanceClient, IBinanceSocketClient binanceSocketClient)
        {
            _binanceClient = binanceClient;
            _binanceSocketClient = binanceSocketClient;
        }

        public async Task<CallResult<UpdateSubscription>> StartBTCCapture()
        {
            CallResult<UpdateSubscription> incomingBTC = null;
           // UpdateSubscription incomingBTC = null;
            try
            {
                if (_binanceSocketClient != null)
                {
                    incomingBTC = await  _binanceSocketClient.SubscribeToTradeUpdatesAsync("BTCUSD", (data) =>
                    {
                        //(data);
                        // _logger.LogInformation(data.Price.ToString());
                        Console.WriteLine(data.Price);
                    });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return incomingBTC;

 public async Task<IActionResult> Index()
        {
            _logger.LogInformation("Start Capture");
            var price = await _marketFeed.StartBTCCapture();

            //var incomingBTC = await _binanceSocketClient.SubscribeToTradeUpdatesAsync("BTCUSD", (data) =>
            //{
            //    //(data);
            //    // _logger.LogInformation(data.Price.ToString());
            //    Console.WriteLine(data.Price);
            //});
            List<TradeResult> trades = new List<TradeResult>();
            await GetPartial(trades);
            //_logger.LogInformation(price.Data);
            //var conn = new WebSocket();

            //HttpListener httpListener = new HttpListener();
            //httpListener.Prefixes.Add("ws://localhost:50021/StartBTCCapture");
            //httpListener.Start();
            //var buffer = new byte[1024 * 4];

            //HttpListenerContext context = await httpListener.GetContextAsync();
            //if (context.Request.IsWebSocketRequest)
            //{
            //    HttpListenerWebSocketContext webSocketContext = await context.AcceptWebSocketAsync(null);
            //    WebSocket webSocket = webSocketContext.WebSocket;
            //    while (webSocket.State == WebSocketState.Open)
            //    {
            //        await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
            //    }
            //}

            return View();
        }

        }



Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7788
  • View blog
  • Posts: 26,018
  • Joined: 05-May 12

Re: .net core binance.net update view in real time from data model webso

Posted 09 May 2021 - 04:44 PM

The data is not coming back as C#. Your websocket subscription is written in C#. There should be nothing preventing you from doing an equivalent websocket subscription in Javascript instead to get the data where you believe you need it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1