74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using System.Diagnostics;
|
||
using System.Text;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.AspNetCore.SignalR;
|
||
using WebApplication1.Models;
|
||
|
||
namespace WebApplication1.Controllers
|
||
{
|
||
[Route("api/[controller]/[action]")]
|
||
public class HomeController : Controller
|
||
{
|
||
private readonly ILogger<HomeController> _logger;
|
||
|
||
private readonly IHubContext<tui> _hubContext;
|
||
public HomeController(ILogger<HomeController> logger,IHubContext<tui> tui)
|
||
{
|
||
_logger = logger;
|
||
this._hubContext = tui;
|
||
}
|
||
|
||
public IActionResult Index()
|
||
{
|
||
return View();
|
||
}
|
||
|
||
[HttpPost()]
|
||
public IActionResult getindex()
|
||
{
|
||
|
||
_hubContext.Clients.All.SendAsync("ReceiveMessage", "aaaaaaaaaaaaaaaa");
|
||
return Json("11111111111");
|
||
}
|
||
|
||
public IActionResult Privacy()
|
||
{
|
||
return View();
|
||
}
|
||
|
||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||
public IActionResult Error()
|
||
{
|
||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||
}
|
||
|
||
|
||
[HttpGet()]
|
||
public async Task StreamSSEvents()
|
||
{
|
||
HttpContext.Response.ContentType = "text/event-stream";
|
||
HttpContext.Response.Headers.Add("Cache-Control", "no-cache");
|
||
HttpContext.Response.Headers.Add("Connection", "keep-alive");
|
||
|
||
for (int i = 0; i < 10; i++)
|
||
{
|
||
//var data = new
|
||
//{
|
||
// id = Guid.NewGuid().ToString(),
|
||
// eventmm = "111",
|
||
// data = "22222"
|
||
//};
|
||
|
||
//var responseData = $"id: {data.id} ";
|
||
//responseData += $"event: {data.eventmm} ";
|
||
//responseData += $"data: 111111111111111 ";
|
||
|
||
var responseData = "aaaa"+i.ToString();
|
||
await HttpContext.Response.WriteAsync(responseData, Encoding.UTF8);
|
||
await Task.Delay(2000); // <20>ȴ<EFBFBD>2<EFBFBD><32><EFBFBD>ٷ<EFBFBD><D9B7><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>¼<EFBFBD>
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|