简体   繁体   English

持久连接的HTTP分块的替代方案?

[英]Alternatives to HTTP chunking for persistent connections?

According to the W3C specification on Server-Sent Events : 根据服务器发送事件W3C规范

Authors are also cautioned that HTTP chunking can have unexpected negative effects on the reliability of this protocol. 还警告作者,HTTP分块可能会对此协议的可靠性产生意外的负面影响。 Where possible, chunking should be disabled for serving event streams unless the rate of messages is high enough for this not to matter. 在可能的情况下,应该禁止为事件流提供服务的分块,除非消息的速率足够高,以至于无所谓。

How exactly would I go about doing this server-side in node.js? 我将如何在node.js中完成此服务器端工作? Setting the Content-Length header isn't really an option because the connection should ideally never close. 设置Content-Length标头并不是真正的选择,因为理想情况下,连接永远不会关闭。 Would I set the response header Transfer-Encoding to identity or remove the Transfer-Encoding header entirely? 我将响应标头Transfer-Encodingidentity还是完全删除Transfer-Encoding标头?

Node will use chunked encoding automatically if you don't explicitly provide a length or manually set a transfer encoding. 如果您未明确提供长度或手动设置传输编码,则Node将自动使用chunked编码。 It would also be good to set Connection: close too as you obviously won't be keeping the connection open after when the server finishes sending events. 最好将Connection: close设置为Connection: close因为在服务器完成事件发送之后,您显然不会保持打开状态。

res.setHeader('Transfer-Encoding', 'identity');
res.setHeader('Connection', 'close');

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

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