我正在尝试将服务器发送事件添加到mule 3.6 ESB流的REST组件中。我无法将数据返回到浏览器。我确实连接到java方法,但是在将EventSource发送回之后,浏览器中发生了错误。任何帮助,将不胜感激。
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8082" doc:name="HTTP Listener Configuration"/>
<flow name="RestService" >
<http:listener config-ref="HTTP_Listener_Configuration" path="/*" doc:name="HTTP">
<http:response-builder>
<http:header headerName="Access-Control-Allow-Origin" value="*"/>
<http:header headerName="Access-Control-Allow-Methods" value="GET,POST"/>
</http:response-builder>
</http:listener>
<jersey:resources doc:name="REST">
<component>
<spring-object bean="RestService"/>
</component>
</jersey:resources>
</flow>
使用Jersey 2 SSE上的示例的Java代码
@GET
@Path("/notifications")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getNotificationEvents() {
final EventOutput eventOutput = new EventOutput();
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
// ... code that waits 1 second
final OutboundEvent.Builder eventBuilder
= new OutboundEvent.Builder();
eventBuilder.name("message-to-client");
eventBuilder.data(String.class,
"Hello world " + i + "!");
final OutboundEvent event = eventBuilder.build();
eventOutput.write(event);
}
} catch (IOException e) {
throw new RuntimeException(
"Error when writing the event.", e);
} finally {
try {
eventOutput.close();
} catch (IOException ioClose) {
throw new RuntimeException(
"Error when closing the event output.", ioClose);
}
}
}
}).start();
return eventOutput;
}
Javascript连接代码
var notifications= new EventSource("http://localhost:8082/notifications");
notifications.onopen = function (e) {
console.log("Waiting For Message..");
};
notifications.onerror = function (e) {
console.log("Error");
console.log(e);
};
notifications.onmessage=function (e) {
console.log(e);
};
Javascript控制台记录onopen等待消息。然后出现错误和e事件。气泡:错误cancelBubble:否可取消:falsecurrentTarget:EventSourcedefaultPrevented:否eventPhase:0路径:数组[0]returnValue:正确srcElement:EventSource目标:EventSource时间戳:1433120172045类型:“错误”
我知道这是一个老话题,但是我已经组合了一个Mule 4