欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
    以下实例,我们编写了一段额外的代码来检测服务器发送事件的浏览器支持情况:
 
if(typeof(EventSource)!=="undefined")
{
    // 浏览器支持 Server-Sent
    // 一些代码.....
}
else
{
    // 浏览器不支持 Server-Sent..
}
 
    服务器端代码实例
 
    为了让上面的例子可以运行,您还需要能够发送数据更新的服务器(比如PHP和ASP)。
 
    服务器端事件流的语法是非常简单的。把"Content-Type"报头设置为"text/event-stream"。现在,您可以开始发送事件流了。
 
    实例
 
<?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代码(VB)(demo_sse.asp):
 
<%
Response.ContentType="text/event-stream"
Response.Expires=-1
Response.Write("data: " & now())
Response.Flush()
%>
 
 
    代码解释:
 
    把报头"Content-Type"设置为"text/event-stream"
 
    规定不对页面进行缓存
 
    输出发送日期(始终以"data:"开头)
 
    向网页刷新输出数据

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h63146.shtml