Server-sent event does not work with jersey SSE(服务器发送的事件不适用于球衣 SSE)
问题描述
我正在尝试使用 Jersey 的 JavaScript SSE.我的资源中有以下代码.我在 Java7 和 Tomcat 7 上托管.我没有收到任何错误.但我在页面上也没有看到数据.
I am trying to use JavaScript SSE from Jersey. I have Following code in my resource. I am hosting on Java7 and Tomcat 7. I dont get any error. But I don't see data either on page.
我调用 /broadcast
来发布数据.它确实显示消息.但是客户端什么也没有.在 Firefox 中,我确实看到 /broadcast
事件被触发了多次.
I call /broadcast
to post data. It does show message. But nothing comes on client. In Firefox, I do see /broadcast
event fired multiple times.
这是我使用的参考.https://jersey.java.net/documentation/latest/sse.html
package net.jigarshah.dse.tracker;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.media.sse.EventOutput;
import org.glassfish.jersey.media.sse.OutboundEvent;
import org.glassfish.jersey.media.sse.SseBroadcaster;
import org.glassfish.jersey.media.sse.SseFeature;
@Singleton
@Path("broadcast")
public class SSEResource {
private SseBroadcaster broadcaster = new SseBroadcaster();
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
public String broadcastMessage(String message) {
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
message = message + "
";
OutboundEvent event = eventBuilder.name("message")
.mediaType(MediaType.TEXT_PLAIN_TYPE)
.data(String.class, message)
.build();
broadcaster.broadcast(event);
System.out.println("broadcasting listen [" +message+ "]");
return "Message was '" + message + "' broadcast.";
}
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput listenToBroadcast() {
System.out.println("will listen");
final EventOutput eventOutput = new EventOutput();
this.broadcaster.add(eventOutput);
return eventOutput;
}
}
我的 Index.html 代码如下.
My Index.html code is as below.
<script type="text/javascript">
var url = "webapi/broadcast";
//var url="http://localhost:8080/trackapp/webapi/broadcast/listen";
var source=new EventSource(url);
source.onerror=function(event)
{
console.log("error [" + source.readyState + "]");
};
source.onopen = function(event){
console.log("eventsource opened!");
};
source.onmessage=function(event)
{
console.log(event.data);
document.getElementById("result").innerHTML+=event.data + "<br>";
};
</script>
推荐答案
我遇到了同样的问题并通过不设置事件名称解决了它(我不知道为什么,但这似乎是解决方案)... 这是代码
I had the same problem and solved it by not setting the name of the event (i don't know why but this seems to be the solution)... here is the code
OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
//WARNING: IF I SET THE NAME OF THE EVENT IT DOES NOT WORK
//eventBuilder.name("message");
eventBuilder.mediaType(MediaType.APPLICATION_JSON_TYPE);
eventBuilder.data(EventData.class, data);
OutboundEvent event = eventBuilder.build();
这篇关于服务器发送的事件不适用于球衣 SSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:服务器发送的事件不适用于球衣 SSE
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01