Created
November 18, 2018 12:15
-
-
Save sergioprates/6bbf4b252cb9355dd0d6cc3482e6e3bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DotNetCoreStreaming.Enums; | |
using DotNetCoreStreaming.Models; | |
using DotNetCoreStreaming.Results; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Newtonsoft.Json; | |
using System.Collections.Concurrent; | |
using System.IO; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace DotNetCoreStreaming.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class ClienteController : ControllerBase | |
{ | |
private static ConcurrentBag<StreamWriter> _clients; | |
static ClienteController() | |
{ | |
_clients = new ConcurrentBag<StreamWriter>(); | |
} | |
[HttpPost] | |
public IActionResult Post(Cliente cliente) | |
{ | |
//Fazer o Insert | |
EnviarEvento(cliente, EventoEnum.Insert); | |
return Ok(); | |
} | |
[HttpPut] | |
public IActionResult Put(Cliente cliente) | |
{ | |
//Fazer o Update | |
EnviarEvento(cliente, EventoEnum.Update); | |
return Ok(); | |
} | |
private static async Task EnviarEvento(object dados, EventoEnum evento) | |
{ | |
foreach (var client in _clients) | |
{ | |
string jsonEvento = string.Format("{0}\n", JsonConvert.SerializeObject(new { dados, evento })); | |
await client.WriteAsync(jsonEvento); | |
await client.FlushAsync(); | |
} | |
} | |
[HttpGet] | |
[Route("Streaming")] | |
public IActionResult Stream() | |
{ | |
return new PushStreamResult(OnStreamAvailable, "text/event-stream", HttpContext.RequestAborted); | |
} | |
private void OnStreamAvailable(Stream stream, CancellationToken requestAborted) | |
{ | |
var wait = requestAborted.WaitHandle; | |
var client = new StreamWriter(stream); | |
_clients.Add(client); | |
wait.WaitOne(); | |
StreamWriter ignore; | |
_clients.TryTake(out ignore); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment