HTML5 Server Send events (server-send events)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.