The HTML5 server sends an event to allow updates from the server to be obtained.
Server-sent Event-A one-way delivery message that indicates that the Web page automatically obtains updates from the server.
There is an important object in which the EventSource object is used to receive notifications that the server sends events.
Instance:
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<meta charset= "Utf-8"/>
<style type= "Text/css" ></style>
<script type= "Text/javascript" ></script>
<body>
<section>
<p id= "pid" ></p>
</section>
<script>
if (typeof (EventSource)!== "undefined") {//Determine if the browser supports EventSource objects
var Source = new EventSource ("demotimes.aspx");//Create a new source object and then specify the URL of the page that is sending the update
Source.onmessage = function (event) {//each time an update is received, the OnMessage event is born
document.getElementById ("pid"). InnerHTML + = Event.data + "<br/>";// when the OnMessage event occurs, push the received data into the ID "pid" In the element
};
}
else {
document.getElementById ("pid"). InnerHTML = "sorry!, your browser does not support HTML5 server-sendevents events";
}
</script>
</body>
Demotimes.aspx
Response.ContentType = "Text/event-stream";//Set the header ContentType to "Text/event-stream";
Response.Expires = -1;//rules do not cache pages
DateTime dt = datetime.now;//Gets the current date time
Response.Write ("Date:" +dt. Date.tostring ());//Output Send date
Response.Flush ();// refresh The output data to the Web page
Response.End ();//code execution stops here, otherwise it will output other HTML
HTML5 Server Send events (server-send events)