我在使用Jetty设置SSE环境时遇到问题。我使用Chrome(44.0.2403.107 m)和FF(39.0)作为客户端浏览器。
我在Jetty中创建了一个servlet(见下文),并使用WebAppContext以编程方式使用它。我也尝试使用Jetty提供的EventSourceServlet
,但是,这也没有用。
在客户端,我创建了一个简单的EventSource
并尝试获取消息。虽然curl
提供了正确的输出(请参阅下面的输出),但FF和Chrome不会按预期触发事件。在终止服务器之后,事件似乎在浏览器中弹出(在请求超时之后)。
在使用Jetty实施SSE时,有人能指出我的错误吗?
我也认识到jetty在异步写入方面使用了特殊的write
方法。但是,在EventSourceServlet
中没有使用这些方法。我是否必须使用SSE才能工作?另外,我的servlet中的哪些点可以省略flush
?
我创建的servlet:
package de.rub.lps.sophie.agenten.sample.webui.servlet;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.AsyncContextState;
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = -8049945594360149181L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(req.getSession(true).getId());
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/event-stream");
resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
resp.flushBuffer();
AsyncContextState async = (AsyncContextState) req.startAsync();
System.out.println(async);
new Thread() {
public void run() {
try {
for (int i = 0; i < 3; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ServletOutputStream outputStream = async.getResponse().getOutputStream();
if (outputStream.isReady()) {
String message = "data: refresh\n\n";
outputStream.write(message.getBytes(StandardCharsets.UTF_8), 0, message.length());
outputStream.flush();
async.getResponse().flushBuffer();
} else {
System.out.println("not ready!");
}
System.out.println("wrote!");
}
} catch (IOException e) {
async.complete();
e.printStackTrace();
}
};
}.start();
}
}
我如何启动Jetty并使用servlet添加WebAppContext(包含在一个大的try / catch中)的摘录:
HandlerCollection handlerCollection = new HandlerCollection(true);
server = new Server(8888);
server.setHandler(handlerCollection);
server.start();
WebAppContext context = new WebAppContext();
handlerCollection.addHandler(context);
context.setContextPath("/test");
context.setResourceBase("/myPath");
ServletHolder servletHolder = new ServletHolder(new TestServlet());
servletHolder.setAsyncSupported(true);
context.addServlet(servletHolder, "/test");
context.start()
curl
的输出:
> GET /Agentname@134.147.100.87:1099/JADE/test HTTP/1.1
> Host: 127.0.0.1:8888
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Wed, 29 Jul 2015 09:08:05 GMT
< Set-Cookie: JSESSIONID=aw6ro34htalm1uf5huix9bxo6;Path=/test/test
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Content-Type: text/event-stream;charset=utf-8
< Transfer-Encoding: chunked
< Server: Jetty(9.3.1.v20150714)
<
data: refresh
data: refresh
data: refresh
[... and so on...]
我在浏览器中使用的test.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script>
var eventSource = new EventSource('test');
eventSource.onmessage = function(event) {
console.log(event);
window.alert(event.data);
};
</script>
</body>
</html>