HTML5 the use of the Server-sent events feature

Source: Internet
Author: User

Client code example

1 // Create a new EventSource object, and then specify the URL of the page that is sending the update.  2varnew EventSource ("http://localhost:8080/test/20150129/test.php"); 3 // The OnMessage event occurs every time an update is received, and the console outputs 4 source.onmessage=function(event) {5    Console.log ( Event.data); 6 };

Server code example

1<?PHP2     //set the "Content-type" header to "Text/event-stream" so that the event stream can be sent3     Header(' Content-type:text/event-stream ');4     $rand=Rand(1,100);5     Echo"Data:this is a test{$rand}\n\n ";6     //to output all the data in the buffer7     Flush();

Precautions:

The server-side output data begins with the "data:" string, ending with \ n

Reference URL:

http://segmentfault.com/q/1010000002315641

Http://www.w3school.com.cn/html5/html_5_serversentevents.asp

Https://developer.mozilla.org/zh-CN/docs/Server-sent_events/Using_server-sent_events

HTML5 the use of the Server-sent events feature

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.