Javascript 将async supported设置为true后,使用jersey实现SSE不起作用
我试图在服务器端使用jersey实现SSE。我这里有多个问题: 我的服务器代码如下:Javascript 将async supported设置为true后,使用jersey实现SSE不起作用,javascript,html,jersey,server-sent-events,Javascript,Html,Jersey,Server Sent Events,我试图在服务器端使用jersey实现SSE。我这里有多个问题: 我的服务器代码如下: @GET @Path("message") @Produces(SseFeature.SERVER_SENT_EVENTS) public EventOutput getMessage() { final EventOutput output = new EventOutput(); try { output.write(new OutboundEvent.Builder().
@GET
@Path("message")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getMessage() {
final EventOutput output = new EventOutput();
try {
output.write(new OutboundEvent.Builder().name("custom-message").data(String.class, "mymessage").build());
} catch (IOException e) {
try {
output.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
return output;
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("url");
source.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
最初,我可以使用浏览器网络调试器每秒查看一次数据。但每次服务器发送事件时,服务器日志都会显示如下内容:
警告:尝试将servlet请求置于异步模式已失败
失败。请检查您的servlet配置-所有servlet
请求处理中涉及的实例和Servlet过滤器必须
显式声明对异步请求处理的支持。
java.lang.IllegalStateException:!异步支持
为了解决这个问题,我在web.xml中添加了一个支持异步的标记。现在我的web.xml看起来像:
<display-name>ssetest</display-name>
<servlet>
<servlet-name>ssetest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>package.rest</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>ssetest</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
二,。我无法从客户端代码中读取sse事件。我的客户代码如下:
@GET
@Path("message")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getMessage() {
final EventOutput output = new EventOutput();
try {
output.write(new OutboundEvent.Builder().name("custom-message").data(String.class, "mymessage").build());
} catch (IOException e) {
try {
output.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
return output;
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("url");
source.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
onMessage块永远不会执行。我的客户代码有问题吗
感谢您的帮助!提前感谢:)您的web.xml文件在哪里?还添加了web.xml文件