Java 使用SSE在Jersey 2.8中未触发服务器端事件
我尝试使用Jersey SSE官方文档中给出的示例 请参阅以下链接中的“14.5.2.使用EventSource的异步SSE处理” 我的代码如下 客户端代码-Java 使用SSE在Jersey 2.8中未触发服务器端事件,java,web-services,server-sent-events,jersey-2.0,Java,Web Services,Server Sent Events,Jersey 2.0,我尝试使用Jersey SSE官方文档中给出的示例 请参阅以下链接中的“14.5.2.使用EventSource的异步SSE处理” 我的代码如下 客户端代码- public class ClientSSEEventManager { public void WaitForEvents() { // Client client = ClientBuilder.newBuilder() // .register(SseFeatur
public class ClientSSEEventManager {
public void WaitForEvents() {
// Client client = ClientBuilder.newBuilder()
// .register(SseFeature.class).build();
// WebTarget target =
// client.target("http://localhost:8080/server/events");
//
// EventInput eventInput = target.request().get(EventInput.class);
// while (!eventInput.isClosed()) {
// final InboundEvent inboundEvent = eventInput.read();
// if (inboundEvent == null) {
// // connection has been closed
// break;
// }
// System.out.println(inboundEvent.getName() + "; "
// + inboundEvent.readData(String.class));
// }
Client client = ClientBuilder.newBuilder().register(SseFeature.class)
.build();
WebTarget target = client.target("http://localhost:8080/server/events");
EventSource eventSource = EventSource.target(target).build();
EventListener listener = new EventListener() {
@Override
public void onEvent(InboundEvent inboundEvent) {
System.out.println(inboundEvent.getName() + "; "
+ inboundEvent.readData(String.class));
}
};
eventSource.register(listener, "message-to-client");
eventSource.open();
}
}
public class MyApplication extends ResourceConfig {
public MyApplication(){
super(ClientSSEEventManager.class, SseFeature.class);
}
// Set<Class<?>> classes = new HashSet<Class<?>>() {
// /**
// *
// */
// private static final long serialVersionUID = 1L;
//
// { add(ClientSSEEventManager.class);
// }};
//
// @Override
// public Set<Class<?>> getClasses() {
// return classes;
// }
}
客户端的Web.xml具有如下init参数
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.framework.MyApplication</param-value>
</init-param>
但客户端没有打印任何内容。
我是不是错过了什么?
我有一个疑问,client.Target,它应该有“http://:8080/server/events”?或者应该是“http://:8080/events”SSE最终对我来说运行良好。 我们需要做几件事
@Singleton
@Path("/events")
public class NotificationHandler {
@Path("/register/{userName}")
@Produces(SseFeature.SERVER_SENT_EVENTS)
@GET
public @ResponseBody EventOutput registerForAnEventSummary(
@PathParam("userName") String userName) {
}
}
PostMethod postMethod = null;
postMethod = new PostMethod(
resourceBundle.getString("localhost:8080")
+ resourceBundle.getString("applicationnotifier")
+ resourceBundle
.getString("sse/events/broadcast/"));
@Path("/broadcast")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String broadcastNotifications(@FormParam("message") String message) { }
var notificationBaseURL = ""; //The URL Where your services are hosted
function listenAllEvents() {
if ( (EventSource) !== "undefined") {
var source = new EventSource(
notificationBaseURL+"applicationnotifier/sse/events/register/"+loggedInUserName);
source.onmessage = notifyEvent;
} else {
console.log("Sorry no event data sent - ");
}
}
function notifyEvent(event) {
var responseJson = JSON.parse(event.data);
alert("... Notification Received ...");
}
服务器URL取决于应用程序的
上下文路径。如果部署为服务器
,则路径应为http://localhost:8080/server/events
。我会在您的服务器端进行一些登录,并将Jackson日志过滤器添加到您的客户端client.register(new LoggingFilter(LOG,true))代码>以便您可以查看客户端发送/接收的内容。如果我直接在浏览器中打开url,如http:///server/events 然后将调用服务器端getNotificationEvents方法。但是,如果我按照上面代码中所示运行客户端应用程序,则不会调用服务器端getNotificationEvents。好的,查看客户端代码,您正在设置事件处理程序,但从未发出GET请求。@Baldy当我们打开连接时,不是只发出GET请求吗?您能告诉我需要做什么更改吗?同样在上面的示例中,服务器会立即响应客户机,但实际上服务器应该在服务器端发生更改时推送事件。那么服务器如何推送事件呢?我必须为此添加哪些代码?
PostMethod postMethod = null;
postMethod = new PostMethod(
resourceBundle.getString("localhost:8080")
+ resourceBundle.getString("applicationnotifier")
+ resourceBundle
.getString("sse/events/broadcast/"));
@Path("/broadcast")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String broadcastNotifications(@FormParam("message") String message) { }
var notificationBaseURL = ""; //The URL Where your services are hosted
function listenAllEvents() {
if ( (EventSource) !== "undefined") {
var source = new EventSource(
notificationBaseURL+"applicationnotifier/sse/events/register/"+loggedInUserName);
source.onmessage = notifyEvent;
} else {
console.log("Sorry no event data sent - ");
}
}
function notifyEvent(event) {
var responseJson = JSON.parse(event.data);
alert("... Notification Received ...");
}