服务器发送的事件丢失连接

时间:2015-08-13 11:38:19

标签: java jersey server-sent-events

我正在关注this tutorial,我将返回的数据放在Chrome的控制台日志中。这是我在客户端的代码:

$(document).ready(function() {  

  //check for browser support
  if(typeof(EventSource)!=="undefined") {

    var source = new EventSource("URL/sse");
    //detect message receipt       
    source.addEventListener("message", function(e) {
        console.log(e.data);            
    }, false);

    source.addEventListener("open", function(e) {
        console.log("Connection was opened.");
    }, false);


    source.addEventListener("error", function(e) {
        if (e.readyState == EventSource.CLOSED) {
            console.log("Connection lost.");
        }            
    }, false);
  }
  else {
    // No support
  }
});

数据还可以。当我访问网页时,我的控制台日志每3秒打开和关闭一次连接:

mypage.js:39 Connection was opened.
mypage.js:15 [{"John":"1","Doe":"2"}]
mypage.js:44 Error - Connection lost.
// 3 seconds
mypage.js:39 Connection was opened.
mypage.js:15 [{"John":"1","Doe":"2"}]
mypage.js:44 Error - Connection lost.
// 3 seconds
mypage.js:39 Connection was opened.
mypage.js:15 [{"John":"1","Doe":"2"}]
mypage.js:44 Error - Connection lost.
...

这是我的服务器端代码:

@Singleton
@Path("/sse")
public class SSEResource {

  @GET
  @Produces(SseFeature.SERVER_SENT_EVENTS)
  public EventOutput getServerSentEvents() {
    final EventOutput eventOutput = new EventOutput();

    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
                eventBuilder.name("message");

                final String myString = getData.toString(); // Some data from db

                eventBuilder.data(String.class, myString);

                final OutboundEvent event = eventBuilder.build();
                eventOutput.write(event);

            } catch (Exception 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;
  }
}

根据我的理解,连接应保持打开,直到客户决定关闭它。我究竟做错了什么?这是对的吗?

1 个答案:

答案 0 :(得分:1)

您的服务器代码正在关闭EventOutput。

toBinaryString

这会导致客户端重新连接并再次启动进程。

我不是百分百肯定你在做什么。如果要在线程中发送更多数据,则无需关闭EventOutput。

根据规范,您可以发送HTTP 204 No Content响应代码以防止客户端重新连接,但我不知道如何使用Jersey SSE执行此操作。当我遇到这个问题时,我正在寻找答案。

在您的情况下,您可以发送一条消息,使客户端关闭连接,或阻止其重新连接。