我試圖發送通知到我的網站。 我的EventSource不會保持打開狀態。它關閉並無限期地開放。 它不會保持打開幾秒鐘。春天和事件來源
的Javascript
this.subscribe = function(eid, cid) {
if (!!window.EventSource) {
//no authentication. Be carreful
source = new EventSource(config.apiUrl + "v1/e/" + eid + "/c/" + cid + "/sse");
source.onerror = error;
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
source.addEventListener('open', function(e) {
// Connection was opened.
console.log(e.data);
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
// Connection was closed.
}
}, false);
} else {
// Result to xhr polling :(
console.log("No notifications available");
}
}
的Java部分
SseEmitter subscribeUpdates() {
SseEmitter sseEmitter = new SseEmitter();
synchronized (this.sseEmitters) {
this.sseEmitters.add(sseEmitter);
sseEmitter.onCompletion(() -> {
synchronized (this.sseEmitters) {
this.sseEmitters.remove(sseEmitter);
}
});
}
return sseEmitter;
}
任何想法?
在此先感謝。
沒有任何線索,我設法做不同的事情。 我有一個NodeJS服務器,提供像SSE,Websocket和其他RealTime的東西。 我的Spring應用程序將事件發送給nodeJS,並將它發送給客戶端。