Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
I have a .Net Core application and I’m using the Binance.Net API. I want to show the trades coming in real time and update my View with the current trades. I’m 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.

C#
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>


What I have tried:

I have websockets started, it comes in the as c#. I then add it to a model in the controller. When I click a button on the partialview it shows the model data that has been added in the controller.
I want it to update as they come in
Posted
Comments
BillWoodruff 7-May-21 20:19pm    
consider posting an issue on https://github.com/JKorf/Binance.Net/issues ?
TheBigBearNow 9-May-21 21:21pm    
I did post one there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900