server-sent events
时间: 2023-04-24 19:01:14 浏览: 125
Server-sent events是一种基于HTTP的服务器推送技术,它允许服务器向客户端发送事件流,而无需客户端发起请求。这种技术可以用于实时通信、实时更新和实时监控等场景。在使用Server-sent events时,服务器会向客户端发送一个HTTP响应,其中包含一个MIME类型为text/event-stream的数据流。客户端通过监听这个数据流,可以接收到服务器发送的事件。Server-sent events相对于WebSocket来说,更加轻量级,适用于一些简单的实时通信场景。
相关问题
Server-Sent Events demo
以下是一个基本的Server-Sent Events示例:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Server-Sent Events Demo</title>
<meta charset="UTF-8">
</head>
<body>
<div id="sse">
<h1>Server-Sent Events Demo</h1>
</div>
<script>
if(typeof(EventSource)!=="undefined") {
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("sse").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("sse").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
</body>
</html>
```
PHP代码:
```
<?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();
?>
```
在上面的示例中,HTML页面通过JavaScript创建一个新的EventSource对象,该对象连接到一个名为“demo_sse.php”的PHP文件。PHP文件发送一个包含服务器时间的数据消息,并在每次发送消息后进行缓冲刷新,以确保消息立即传输给客户端。在HTML页面中,每当从服务器接收到新的消息时,将其添加到页面中的“sse”元素中。
Server-Sent Events springboot demo
以下是一个基于Spring Boot的Server-Sent Events示例:
首先,在pom.xml文件中添加以下依赖项:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
接下来,创建一个名为“ServerSentEventController”的新类,该类将处理Server-Sent Events请求:
```
import java.time.LocalTime;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@RestController
public class ServerSentEventController {
@GetMapping(value = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter serverSentEvent() {
SseEmitter emitter = new SseEmitter();
LocalTime currentTime = LocalTime.now();
// 发送当前时间
emitter.send(SseEmitter.event().data("Current time: " + currentTime.toString()));
// 定时发送时间
Thread thread = new Thread(() -> {
try {
while (true) {
Thread.sleep(5000);
currentTime = LocalTime.now();
emitter.send(SseEmitter.event().data("Current time: " + currentTime.toString()));
}
} catch (Exception e) {
emitter.complete();
}
});
thread.start();
return emitter;
}
}
```
在上面的代码中,我们创建了一个名为“serverSentEvent”的控制器方法,该方法返回一个SseEmitter对象,该对象将用于发送Server-Sent Events。在这个方法中,我们首先发送当前时间,然后设置一个线程,每隔5秒发送一次当前时间,直到连接关闭或发生异常为止。
最后,在Spring Boot应用程序的主类上添加@EnableWebMvc注释:
```
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@EnableWebMvc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
现在,您可以启动应用程序并访问“http://localhost:8080/sse”来查看Server-Sent Events的示例。您应该能够在浏览器中看到当前时间,并且每隔5秒钟更新一次。