Html 简明教程
HTML - Server Sent Events
Server Sent Events 是一种在不需要页面刷新或发出请求的情况下将数据从服务器发送到网页的方法。这些事件对于创建实时应用程序很有用,例如聊天、新闻源或通知。使用 SSE,我们可以连续从我们的 Web 服务器向访问者的浏览器推送 DOM 事件。
Server Sent Events are a way of sending data from a server to a web page, without requiring the page to refresh or make requests. These events are useful for creating real-time applications, such as chat, news feeds, or notifications. Using SSE, we can push DOM events continuously from our web server to the visitor’s browser.
事件流方法会打开与服务器的持久连接,并在有新信息可用时向客户端发送数据,从而无需连续轮询。服务器发送的事件标准化了我们从服务器向客户端流式传输数据的方式。
The event streaming approach opens a persistent connection to the server, sending data to the client when new information is available, eliminating the need for continuous polling. Server-sent events standardize how we stream data from the server to the client.
How to use SSE in Web Application?
要在 Web 应用程序中使用服务器发送事件,我们需要向文档添加 <eventsource> 元素。<eventsource> 元素的 src 属性应指向一个 URL,该 URL 提供一个持久 HTTP 连接,该连接发送包含事件的数据流。此外,此 URL 指向一个 PHP、PERL 或任何 Python 脚本,该脚本将负责持续发送事件数据。
To use Server-Sent Events in a web application, we need to add an <eventsource> element to the document. The src attribute of <eventsource> element should point to an URL which provides a persistent HTTP connection that sends a data stream containing the events. Furthermore, the URL points to a PHP, PERL or any Python script which would take care of sending event data consistently.
Instance
以下是一段 Web 应用程序的示例 HTML 代码,它将期望服务器时间。
Following is a sample HTML code of web application which would expect server time.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
/* Define event handling logic here */
</script>
</head>
<body>
<div id="sse">
<eventsource src="/cgi-bin/ticker.cgi" />
</div>
<div id="ticker">
<TIME>
</div>
</body>
</html>
Server Side Script for SSE
服务器端脚本应发送 Content-type 头部并指定 text/event-stream 类型,如下所示。
A server side script should send Content-type header specifying the type text/event-stream as follows.
print "Content-Type: text/event-stream\n\n";
设置 Content-Type 后,服务器端脚本将发送一个 Event: 标签,后跟事件名称。以下代码段将以 Server-Time 作为以换行符结尾的事件名称。
After setting Content-Type, server side script would send an Event: tag followed by event name. Following code snippet would send Server-Time as event name terminated by a new line character.
print "Event: server-time\n";
最后一步是使用 Data : 标签发送事件数据,该标签后跟由换行符结尾的整数值或字符串值,如下所示 −
Final step is to send event data using Data: tag which would be followed by integer of string value terminated by a new line character as follows −
$time = localtime();
print "Data: $time\n";
最后,以下是由 Perl 编写的完整的 ticker.cgi:
Finally, following is complete ticker.cgi written in Perl −
#!/usr/bin/perl
print "Content-Type: text/event-stream\n\n";
while(true){
print "Event: server-time\n";
$time = localtime();
print "Data: $time\n";
sleep(5);
}
Handle Server-Sent Events
让我们修改我们的 Web 应用程序来处理服务器发送的事件。以下是最终示例。
Let us modify our web application to handle server-sent events. Following is the final example.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">document.getElementsByTagName("eventsource")[0].addEventListener("server-time", eventHandler, false);
function eventHandler(event) {
// Alert time sent by the server
document.querySelector('#ticker').innerHTML = event.data;
}
</script>
</head>
<body>
<div id="sse">
<eventsource src="/cgi-bin/ticker.cgi" />
</div>
<div id="ticker" name="ticker"> [TIME] </div>
</body>
</html>
在测试 Server-Sent 事件之前,建议您确保您的网络浏览器支持此概念。
Before testing Server-Sent events, I would suggest that you make sure your web browser supports this concept.