2017-04-26 78 views
0

我已經編寫了一個基本的Java應用程序,使用Jersey SSE客戶端來使用從Node.js服務器流式傳輸的SSE(服務器發送的事件)。但是,我無法收到這些事件。要驗證的服務器組件工作正常,我用curl如下:Jersey SSE客戶端沒有收到事件

curl -v -H "Accept: text/event-stream" http://localhost:8080/events/ 

我得到如下回應:

* Trying ::1... 
* Connected to localhost (::1) port 8080 (#0) 
> GET /events/ HTTP/1.1 
> Host: localhost:8080 
> User-Agent: curl/7.43.0 
> Accept: text/event-stream 
> 
< HTTP/1.1 200 OK 
< X-Powered-By: Express 
< Content-Type: text/event-stream 
< Cache-Control: no-cache 
< Connection: keep-alive 
< Date: Wed, 26 Apr 2017 17:12:00 GMT 
< Transfer-Encoding: chunked 
< 

event: ping 
data: 0.6637400726922664 

event: ping 
data: 0.8538157046725585 

在Java我SSE客戶端的代碼如下(Java的8,澤西島客戶端1.19.3,澤西媒介SSE 2.25.1):

import org.glassfish.jersey.media.sse.EventListener; 
import org.glassfish.jersey.media.sse.EventSource; 
import org.glassfish.jersey.media.sse.InboundEvent; 
import org.glassfish.jersey.media.sse.SseFeature; 

import javax.ws.rs.client.Client; 
import javax.ws.rs.client.ClientBuilder; 
import javax.ws.rs.client.WebTarget; 

public class NodeCaptureSSE { 

    public static void main(String[] args) { 

    Client client = ClientBuilder.newBuilder().register(SseFeature.class).build(); 
    WebTarget target = client.target("http://localhost:8080/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, "ping"); 
    eventSource.open(); 
    System.out.println("Connected to SSE source..."); 
    try { 
     Thread.sleep(25_000); 
    } 
    catch (InterruptedException ie) { 
     System.err.println("Exception: " + ie.getMessage()); 
    } 
    eventSource.close(); 
    System.out.println("Closed connection to SSE source"); 
    } 
} 

我在屏幕上看到的唯一輸出是:

Connected to SSE source... 

後面是25秒後的退出。由於服務器正在流式傳輸命名事件(「ping」),我指定在將偵聽器註冊到事件源時。省略第二個參數到eventSource.register(...);沒有做任何事情,但我沒有想到。我還刪除了網址中的結尾/,但這會產生404 Not Found(如預期的那樣)。我希望能夠朝正確的方向發展。

回答

2

你的代碼適合我。事實上,我正在拼命尋找這個代碼,所以感謝^ _ ^哈哈哈。無論如何,我有一個SSE事件流輸出時間事件,這裏是我的輸出看起來像你的代碼(我改變ping時間事件附加到我的服務器)。

Connected to SSE source... 
time-event; Time is Thu Jul 20 10:10:37 CDT 2017 
time-event; Time is Thu Jul 20 10:10:41 CDT 2017 

問題是最有可能與您的事件流,您使用指向它,而不是與你的客戶端Java代碼的URL。我注意到的另一件事是,在你的curl命令中我沒有看到那裏的字符集規範。確保你的字符集設置爲UTF-8。這是我的CMD輸出的樣子: enter image description here

這是我用來生成流的代碼。

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.xml.ws.http.HTTPException; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.PasswordAuthentication; 
import java.util.Calendar; 



    @SuppressWarnings("java.io.IOException") 
    @WebServlet("/SSEServlet") 
    public class SSEServlet extends HttpServlet { 
     private static final long serialVersionUID = 1L; 
     private boolean cancel; 

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { 

      System.out.println("You just entered the doGetMethod"); 
      response.setContentType("text/event-stream"); 
      response.setCharacterEncoding("UTF-8"); 
      PrintWriter printWriter = null; 
      response.getBufferSize(); 


      while(true){ 
       try{ 
        System.out.println("You just entered the while loop"); 
        double randomNumber = Math.random()*10000; 
        printWriter = response.getWriter(); 

        printWriter.print("event: time-event" + "\n"); 
        printWriter.print("data: " + "Time is " + Calendar.getInstance().getTime() + "\n\n"); 
        response.flushBuffer(); 
        Thread.sleep((long)randomNumber); 

       } catch (IOException | InterruptedException e){ 
        e.printStackTrace(); 
        break; 
       } 
      } 
      System.out.println("Connection was aborted"); 
     } 
    } 
+1

有沒有辦法讓連接長時間打開?,澤西島客戶端會像瀏覽器一樣工作嗎? –

相關問題