使用Http / 2从javascript进行api调用 [英] Use Http/2 to make api calls from javascript

查看:529
本文介绍了使用Http / 2从javascript进行api调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道大多数浏览器都支持http / 2加载页面,但这是否意味着我可以在使用XmlHttpRequest进行api调用时利用它?

I know most browsers support http/2 for loading the pages but does this mean I can leverage it when I make api calls using XmlHttpRequest ?

更具体地说我的问题如果我使用XmlHttpRequest进行2次调用来获取数据,是否确保它们都使用下面的相同tcp连接?我读过的任何文档都没有指明关于http2支持XmlHttpRequest的任何内容,或者我如何显式打开http2连接,利用这个进行一些调用然后关闭连接。

More specifically my question is if I make 2 calls to fetch data using XmlHttpRequest does it ensure that both of them use the same tcp connection underneath ? None of the documentation I read any where specifies any thing about http2 support for XmlHttpRequest or how I can explicitly open a http2 connection, make some calls leveraging this and then close the connection.

java中的okHttp,jetty和其他库提供了支持它的客户端库。但是javascript的支持还不清楚。

The okHttp, jetty and other libraries in java offer client libraries to support this. But the javascript support is not clear.

推荐答案

当浏览器和服务器都支持HTTP / 2时,它将被用于所有来自浏览器的呼叫 - 包括XHR。您无需在javascript中执行任何特殊操作即可启用此功能。这是关于HTTP / 2实现方式的一个好处。

When the browser and server both support HTTP/2 then it will be used for all calls from the browser - including XHR. You don't need to do anything special in your javascript to enable this. That's one of the great things about the way HTTP/2 was implemented.

如果您要问如何确保连接保持打开状态,因为您希望重用连接然后 http / 2规范说:

If you are asking how to ensure the connection is held open for as you want to reuse the connection then the http/2 spec says:


HTTP / 2连接是持久的。为了获得最佳性能,预计客户不会关闭连接,直到
确定不需要与服务器进行进一步通信
(例如,当用户离开某个特定用户时)网页)
或直到服务器关闭连接。

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从javascript进行api调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆