簡體   English   中英

在客戶端GWT上處理服務器發送的事件

[英]Deal with server sent event on client side GWT

我需要為服務器發送事件實現前端。 我使用GWT,但找不到用於創建SSE偵聽器的任何解決方案。 每當數據更改時,我需要從服務器推送數據並在客戶端上接收它。 所以現在我有這樣的事情:

private void method() {
    final EventSource eventSource = EventSource.newEventSourceIfSupported();
    if (null != eventSource) {
        eventSource.setListener(this);
        eventSource.open(GWT.getHostPageBaseURL() + "rest/myresource");
    }
}

@Override
public void onOpen(EventSource eventSource) {
    Window.alert("Open");

}

@Override
public void onClose(EventSource eventSource) {
    Window.alert("onClose");

}

@Override
public void onMessage(EventSource eventSource, String lastEventId, String type, String data) {
    Window.alert("lastEventId: " + lastEventId);
    Window.alert("type: " + type);
    Window.alert("data: " + data);

}

@Override
public void onError(EventSource eventSource) {
    Window.alert("onError");

}

我的類實現了EventSourceListener但是它不起作用。 實際上,此代碼僅在打開連接時起作用,但是無法從服務器接收任何消息。 有人知道如何使用GWT在客戶端上接收數據來解決該問題嗎?

GWT存在許多用於push back services例如GWT Event Services 在此處輸入鏈接描述

為了使服務器向客戶端發起請求,您將需要使用WebSockets和目前only supported by Chrome.實驗性HTML5功能only supported by Chrome.

或者,為了模擬這種交互,可以使用rocket-gwt project在GWT中提供的Comet (長輪詢)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM