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