使用 Http/2 从 javascript 拨打 api 电话
Use Http/2 to make api calls from javascript
我知道大多数浏览器都支持 http/2 加载页面,但这是否意味着我可以在使用 XmlHttpRequest 进行 api 调用时利用它?
更具体地说,我的问题是,如果我使用 XmlHttpRequest 进行 2 次调用以获取数据,是否可以确保它们都使用相同的 tcp 连接? None 我在任何地方阅读的文档都指定了有关对 XmlHttpRequest 的 http2 支持或我如何显式打开 http2 连接、利用它进行一些调用然后关闭连接的任何事情。
java 中的 okHttp、jetty 和其他库提供客户端库来支持这一点。但是java脚本支持不清楚。
当浏览器和服务器都支持 HTTP/2 时,它将用于来自浏览器的所有调用 - 包括 XHR。您无需在 javascript 中执行任何特殊操作即可启用此功能。这是 HTTP/2 实施方式的一大优点。
如果您询问如何确保连接保持打开状态,因为您想要重用连接,那么 http/2 spec 会说:
HTTP/2 connections are persistent. For best performance, it is
expected that clients will not close connections until it is
determined that no further communication with a server is necessary
(for example, when a user navigates away from a particular web page)
or until the server closes the connection.
...
Servers are encouraged to maintain open connections for as long as
possible but are permitted to terminate idle connections if necessary.
我知道大多数浏览器都支持 http/2 加载页面,但这是否意味着我可以在使用 XmlHttpRequest 进行 api 调用时利用它?
更具体地说,我的问题是,如果我使用 XmlHttpRequest 进行 2 次调用以获取数据,是否可以确保它们都使用相同的 tcp 连接? None 我在任何地方阅读的文档都指定了有关对 XmlHttpRequest 的 http2 支持或我如何显式打开 http2 连接、利用它进行一些调用然后关闭连接的任何事情。
java 中的 okHttp、jetty 和其他库提供客户端库来支持这一点。但是java脚本支持不清楚。
当浏览器和服务器都支持 HTTP/2 时,它将用于来自浏览器的所有调用 - 包括 XHR。您无需在 javascript 中执行任何特殊操作即可启用此功能。这是 HTTP/2 实施方式的一大优点。
如果您询问如何确保连接保持打开状态,因为您想要重用连接,那么 http/2 spec 会说:
HTTP/2 connections are persistent. For best performance, it is expected that clients will not close connections until it is determined that no further communication with a server is necessary (for example, when a user navigates away from a particular web page) or until the server closes the connection.
...
Servers are encouraged to maintain open connections for as long as possible but are permitted to terminate idle connections if necessary.