I have an ASP.NET MVC application and I am using server-sent events. The application works fine but I am having some questions on how it works. Below is the controller code.
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
ViewBag.Message = "SSE WITH ASP.NET MVC";
return View();
}
public ActionResult Message()
{
var result = string.Empty;
var sb = new StringBuilder();
sb.AppendFormat("data: {0}\n\n", DateTime.Now.ToString());
return Content(sb.ToString(), "text/event-stream");
}
}
And below is the view code.
<script>
function contentLoaded() {
var source = new EventSource('home/message');
//var ul = $("#messages");
source.onmessage = function (e) {
var li = document.createElement("li");
var returnedItem = e.data;
li.textContent = returnedItem;
$("#messages").append(li);
}
};
window.addEventListener("DOMContentLoaded", contentLoaded, false);
</script>
<h2>@ViewBag.Message</h2>
<p>
SSE WITH ASP.NET MVC
</p>
<ul id="messages">
</ul>
My questions are: 1. The time gets updated only every 3 seconds. Why is it so? 2. How to determine how often the controller action will be called?