Files
Web_BLVLOG_Server_Mvc_Prod/WebApplication1/Controllers/HomeController.cs
2025-11-20 16:20:37 +08:00

74 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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>
}
}
}
}