简体   繁体   English

通过HTTP SSL的流未刷新

[英]Stream over HTTP SSL is not flushed

I have a web application running behind nginx. 我在nginx后面运行了一个Web应用程序。 Some pages are accessible via http, some others via https. 某些页面可通过http访问,另一些页面可通过https访问。 I have some "pages", which are rather streams as the application does not close the connection and feeds data as they come. 我有一些“页面”,它们是流,因为应用程序不关闭连接并在它们来时提供数据。 The feed then looks like this: Feed如下所示:

 TIME1 MESSAGE1
 TIME2 MESSAGE2
 ...
 TIMEn MESSAGEn

After each line I write "\\n" and then call flush(). 在每一行之后,我写“ \\ n”,然后调用flush()。 Over http, it works correctly and my client can listen to new data. 通过http,它可以正常工作,并且我的客户端可以监听新数据。 However, over https the client is not receiving any data until the connection is closed. 但是,在关闭连接之前,客户端不会通过https接收任何数据。

 ServletOutputStream stream = applicationModel.getOutputStream();
 OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
 BufferedWriter writer = new BufferedWriter(streamWriter);
 while (true) {
     wait();
     writer.write(newMessage);
     writer.flush();
 }

Unless the application is tightly integrated with the web server a flush on the writer will only flush the buffers inside your application, so that the data gets send to the web server. 除非应用程序与Web服务器紧密集成,否则在Writer上进行刷新将仅刷新应用程序内部的缓冲区,以便将数据发送到Web服务器。 Inside the web server there are more buffers, which are necessary to optimize the traffic by sending larger TCP packets and thus decrease the overhead for the data. Web服务器内部有更多缓冲区,通过发送更大的TCP数据包来优化流量,从而减少数据开销是必需的。 And, if you use SSL there is yet another layer to watch, because your data will be encapsulated into an SSL frame which again adds overhead, so it is good to not only have a few bytes payload inside. 而且,如果您使用SSL,则还需要监视另一层,因为您的数据将被封装到SSL帧中,这又增加了开销,因此,不仅要在其中包含几个字节的有效负载,这是很好的。 Finally you have the buffering at the OS kernel, which might defer the sending of a small TCP packet for some time if there is hope that there will be more data. 最终,您在OS内核上有了缓冲,如果希望会有更多的数据,这可能会将发送一个小的TCP数据包的时间推迟一段时间。

Please be aware, that your wish to control the buffers is against a fundamental design concept of HTTP. 请注意,您希望控制缓冲区违反了HTTP的基本设计概念。 HTTP is based on the idea that you have a request from the client to the server and then a response from the server, ideally with a known content-length up-front. HTTP基于这样的想法:您有一个从客户端到服务器的请求,然后有一个来自服务器的响应,理想情况下是预先知道内容长度。 There is no idea in the original design of a response which evolves slowly and where the browser will update the display once new data arrive. 响应的原始设计没有任何想法,响应会缓慢发展,一旦新数据到达,浏览器将在何处更新显示。 The real way to get updates would be instead to let the client send another request and then send the new response back. 获取更新的真正方法是让客户端发送另一个请求,然后将新的响应发送回去。 Another way would be to use WebSockets. 另一种方法是使用WebSockets。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM