Jersey服务器已发送事件不与Accept-Encoding gzip一起使用

时间:2015-04-07 20:46:43

标签: angularjs jersey jersey-2.0 server-sent-events

我使用Jersey 2.17进行SSE - 我根据文档进行了演示:https://jersey.java.net/documentation/latest/sse.html

在我的测试中,它适用于简单的curl请求(curl --verbose" localhost:9000 / api / broadcast"),但如果我添加-H 'Accept-Encoding: gzip, deflate'(以复制我的javascript代码) )它不起作用(最终超时'转移关闭,剩余未完成的读取数据')。我猜这个球衣说它的GZIPed响应,但实际上它没有。

我真正的问题是AngularJS javascript代码总是添加 Accept-Encoding ... 标头。

我正在寻找能够

的解决方案
  1. 防止接受编码:gzip,deflate'在初始化EventSource()对象时将其添加为请求标头
  2. 设置Jersey以正确处理/ gzip发送给客户端的事件。
  3. 更新 - 我注意到如果我在Java代码中关闭EventOutput对象,我的事件都会被发送。

    这是使用curl时的控制台输出:

    curl --verbose "localhost:9000/api/broadcast"  -H 'Accept-Encoding: gzip, deflate'
    
    * Hostname was NOT found in DNS cache
    *   Trying ::1...
    * connect to ::1 port 9000 failed: Connection refused
    *   Trying 127.0.0.1...
    * Connected to localhost (127.0.0.1) port 9000 (#0)
    > GET /api/broadcast HTTP/1.1
    > User-Agent: curl/7.37.1
    > Host: localhost:9000
    > Accept: */*
    > Accept-Encoding: deflate, gzip
    > 
    < HTTP/1.1 200 OK
    * Server Apache-Coyote/1.1 is not blacklisted
    < server: Apache-Coyote/1.1
    < vary: Accept-Encoding
    < content-encoding: deflate
    < content-type: text/event-stream
    < transfer-encoding: chunked
    < date: Tue, 07 Apr 2015 20:35:42 GMT
    < connection: close
    < 
    * transfer closed with outstanding read data remaining
    * Closing connection 0
    curl: (18) transfer closed with outstanding read data remaining
    

    作为参考,我的AngularJS应用程序中的js代码是:

        $scope.sseSource = new EventSource('/api/ptt/1/status');
        console.log( $scope.sseSource);
        $scope.sseSource.onerror = function (event) {
            console.log("onerror");
        }
        $scope.sseSource.onopen = function (event) {
            console.log("onopen");
        }
        $scope.sseSource.onmessage = function (event) {
            console.log("onmessage");
        }
    

0 个答案:

没有答案