Example #1
0
 public ActionResult GetCalculations()
 {
     try
     {
         return(Ok(JsonConvert.SerializeObject(calcManager.GetCalulations(10))));
     }
     catch (Exception ex)
     {
         logger.Log(LogLevel.Error, ex.Message);
         throw new Exception("Failed To Retrieve Calculations From The Underlying Repo");
     }
 }
        private async Task ListenForEvents(WebSocket webSocket)
        {
            var lastIndexSentToClient = 0; // represents that highest calculation index we have sent to the client

            while (webSocket.State == WebSocketState.Open)
            {
                var latestCalculations = calcManager.GetCalulations(10); // Get the 10 most recent calculations
                if (latestCalculations.Count != 0)                       // if the count is 0, the repo is empty, we don't need to do anything
                {
                    var highestIndexInRepo = latestCalculations.Max(x => x.Index);

                    if (lastIndexSentToClient < highestIndexInRepo) // If we have an entry in the repo with a higher index then what was last sent to the client, we need to send the data.
                    {
                        lastIndexSentToClient = highestIndexInRepo;
                        var calcAsJson = JsonConvert.SerializeObject(latestCalculations);
                        var bytes      = Encoding.ASCII.GetBytes(calcAsJson);

                        var arraySegment = new ArraySegment <byte>(bytes);
                        await webSocket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
                    }
                }
                await Task.Delay(500);
            }
        }