简体   繁体   English

Java SSE 服务器与 com.sun.net.httpserver.HttpServer

[英]Java SSE Server with com.sun.net.httpserver.HttpServer

How can implement SSE Server with com.sun.net.httpserver.HttpServer如何用com.sun.net.httpserver.HttpServer实现 SSE Server
I wrote simple HttpHandler like this for testing connection:我写了这样简单的HttpHandler来测试连接:

public class SseResource implements HttpHandler {

    @Override
    public void handle(HttpExchange exchange) throws IOException {
        Headers responseHeaders = exchange.getResponseHeaders();
        responseHeaders.add("Content-Type", "text/event-stream");
        responseHeaders.add("Connection", "keep-alive");
        responseHeaders.add("Transfer-Encoding", "chunked");
        responseHeaders.add("X-Powered-By", "Native Application Server");

        exchange.sendResponseHeaders(200, responseHeaders.size());
        OutputStream writer = exchange.getResponseBody();
        for (int i = 0; i < 10; i++) {
            writer.write("event: count\n".getBytes()); // <-- Connection Closed at this line 
            writer.write(("data: " + i + "\n").getBytes());
            writer.write("\n\n".getBytes());
            writer.flush();
            sleep();
        }

        writer.close();
    }


    public static void sleep() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}    

Does not work.不工作。
I test connection with Linux curl command:我用 Linux curl命令测试连接:

curl -Ni http://localhost:8080/stream

HTTP/1.1 200 OK
Connection: keep-alive
X-powered-by: Native Application Server
Date: Thu, 10 Jun 2021 05:27:56 GMT
Transfer-encoding: chunked
Content-type: text/event-stream
Content-length: 4

curl: (18) transfer closed with outstanding read data remaining

Did I misunderstand about SSE server?我对 SSE 服务器有误解吗? or does my code have a problem?还是我的代码有问题?

response size should be zero.响应大小应为零。
I compare my response headers with result of this project: https://github.com/enkot/SSE-Fake-Server我将我的响应标头与此项目的结果进行比较: https://github.com/enkot/SSE-Fake-Server

exchange.sendResponseHeaders(200, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为com.sun.net.httpserver.HttpServer(Java)创建动态上下文 - Create dynamically contexts for com.sun.net.httpserver.HttpServer (Java) 使用com.sun.net.httpserver.HttpServer进行Comet / cometd - Using com.sun.net.httpserver.HttpServer for comet/cometd com.sun.net.httpserver.HttpServer是否支持管道传输? - Does com.sun.net.httpserver.HttpServer support pipelining? 性能:com.sun.net.httpserver.HttpServer与Jetty - Performance: com.sun.net.httpserver.HttpServer vs. jetty com.sun.net.httpserver.httpserver 的可修改请求标头 - Modifiable request headers with com.sun.net.httpserver.httpserver 如何关闭com.sun.net.httpserver.HttpServer? - How to shutdown com.sun.net.httpserver.HttpServer? Eclipse无法识别com.sun.net.httpserver.HttpServer包 - Eclipse cant recognize com.sun.net.httpserver.HttpServer package 无法使用com.sun.net.HTTPServer.httpserver - Can not use the com.sun.net.HTTPServer.httpserver 在当前线程中启动java com.sun.net.httpserver.HttpServer侦听器,然后每个连接应具有一个线程 - Start java com.sun.net.httpserver.HttpServer listener in current thread, connections then should have one thread each 使用com.sun.net.httpserver.HttpServer时不调用ServletContextListener - ServletContextListener isn't invoked when using com.sun.net.httpserver.HttpServer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM