public async Task Get() { // get the http response so our event handlers can write to it _response = _httpContextAccessor.HttpContext.Response; // we are streaming messages... _response.Headers.Add("Content-Type", "text/event-stream"); // completion flag ValueWrapper <bool> complete = new ValueWrapper <bool>(false); // spew out our messages StreamingMonitor streamingMonitor = new StreamingMonitor(complete, _game, _response); // make sure to dispose of our streaming monitor when this connection is done _response.RegisterForDispose(streamingMonitor); while (complete.Value == false) { if (_game.IsPlaying) { // TODO: For now, clear these values out manually...timing problems with client need to be fixed streamingMonitor.Clear(); // wait until the game is done Task playingMonitor = new Task(() => { while (_game.IsPlaying) { Thread.Sleep(100); } ; } ); playingMonitor.Start(); await playingMonitor; // Write the endgame message so client knows we are done... _response.WriteAsync($"data: {{\"EndGame\": 1}}\r\r").Wait(); _response.Body.Flush(); } await Task.Delay(200); } }
public StreamingMonitor(ValueWrapper <bool> complete, Game game, HttpResponse response) { _complete = complete; _game = game; _response = response; // wire up event handlers _game.ScoreBoard.ElapsedSecondsUpdated += OnElapsedSecondsUpdated; _game.ScoreBoard.BlueOwnershipSecondsUpdated += OnBlueOwnershipSecondsUpdated; _game.ScoreBoard.BlueSwitchOwnershipSecondsUpdated += OnBlueSwitchOwnershipSecondsUpdated; _game.ScoreBoard.BlueScaleOwnershipSecondsUpdated += OnBlueScaleOwnershipSecondsUpdated; _game.ScoreBoard.BlueVaultScoreUpdated += OnBlueVaultScoreUpdated; _game.ScoreBoard.BlueParkScoreUpdated += OnBlueParkScoreUpdated; _game.ScoreBoard.BlueAutorunScoreUpdated += OnBlueAutorunScoreUpdated; _game.ScoreBoard.BlueClimbScoreUpdated += OnBlueClimbScoreUpdated; _game.ScoreBoard.RedOwnershipSecondsUpdated += OnRedOwnershipSecondsUpdated; _game.ScoreBoard.RedSwitchOwnershipSecondsUpdated += OnRedSwitchOwnershipSecondsUpdated; _game.ScoreBoard.RedScaleOwnershipSecondsUpdated += OnRedScaleOwnershipSecondsUpdated; _game.ScoreBoard.RedVaultScoreUpdated += OnRedVaultScoreUpdated; _game.ScoreBoard.RedParkScoreUpdated += OnRedParkScoreUpdated; _game.ScoreBoard.RedAutorunScoreUpdated += OnRedAutorunScoreUpdated; _game.ScoreBoard.RedClimbScoreUpdated += OnRedClimbScoreUpdated; _game.ScoreBoard.StateOfPlayUpdated += OnStateOfPlayUpdated; }