亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

HTML5 Server-Sent Events

Server-Sent event - one-way messaging

The Server-Sent event refers to the web page automatically getting updates from the server.

It was also possible to do this before, if the webpage had to ask if an update was available. By sending events through the server, updates can arrive automatically.

Examples: Facebook/Twitter updates, valuation updates, new blog posts, event results, etc.

The following is a sample code on W3School, including client-side JavaScript and server-side PHP code:

var source = new EventSource("demo_sse.php");

source.onmessage = function(event) {

document.getElementById("result").innerHTML += event.data + "< br />";

};

This code continuously obtains data from demo_sse.php and outputs the result to the ID of in the div of result.

<?php

##header('Content-Type: text/event-stream');

header('Cache-Control: no-cache');

$time = date('r');

echo "data: The server time is: {$time}\n\n";

flush();

?>

This code returns the current time of the server to the client. Finally, a result similar to the following is displayed on the client page:

The server time is: Fri, 29 Aug 2016 02:03:21 +0800

The server time is: Fri, 29 Aug 2016 02:03:24 +0800

The server time is: Fri, 29 Aug 2016 02:03:27 +0800

The server time is: Fri, 29 Aug 2016 02: 03:30 +0800

The server time is: Fri, 29 Aug 2016 02:03:33 +0800

...

Receive Server-Sent event notification

EventSource object is used to receive server-sent event notification:

Instance

var source=new EventSource("demo_sse.php");source.onmessage=function(event)
{
document.getElementById("result").innerHTML+=event.data + "<br> ";
};


Example analysis:

Create a new EventSource object, and then specify the URL of the page to send updates (in this case, "demo_sse.php")

Every time an update is received, the onmessage event will occur

When the onmessage event occurs, push the received data into the element with the id "result"

Detect Server-Sent event support

In the following example, we wrote an additional piece of code to detect browser support for server-sent events:

if(typeof(EventSource)!=="undefined")
{
// The browser supports Server-Sent
// Some codes...
}
else
{
// The browser does not support Server-Sent..
}

Server-side code example

In order for the above example to work, you will also need a server that can send data updates (such as PHP and ASP).

The syntax of server-side event streaming is very simple. Set the "Content-Type" header to "text/event-stream". Now you can start sending the event stream.

Example

<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>

ASP code (VB) (demo_sse.asp):

<%
Response.ContentType="text/event-stream"
Response.Expires=-1
Response.Write("data: " & now())
Response.Flush()
%>

Code explanation:

Set the header "Content-Type" to "text/event-stream"

Specify that the page is not cached

Output the sending date (always starting with "data: ")

Refresh output data to the web page




Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>服務(wù)器推送SSE</title> <script type="text/javascript"> $(document).ready(function(){ //檢查瀏覽器支持情況 if(typeof(EventSource)!=="undefined") { //定義個對象,用于初始化事件源,這里用c.php這個頁面實(shí)現(xiàn) var eSource = new EventSource("c.php"); //detect message receipt eSource.onmessage = function(event) { //將收到的數(shù)據(jù)展示到頁面的ID=content元素中 document.getElementById("content").innerHTML += event.data+'<br />'; }; }else { document.getElementById("content").innerHTML="沒有收到服務(wù)端Server-Sent數(shù)據(jù)."; } }); </script> </head> <body> <div id="content"></div> </body> <p>服務(wù)器端c.php 頁面寫在HTML外部</p> </html> <?php // 要聲明頭部 header("Content-Type: text/event-stream"); header("Cache-Control: no-cache"); // 直接打印當(dāng)前時間 echo "data: ".date('Y-m-d H:i:s').PHP_EOL; flush(); ?>
submitReset Code