-http2的含义是什么,而websockets则不提供客户使用的API?

发布于 2025-02-06 20:23:06 字数 726 浏览 1 评论 0原文

https://stackoverflow.com/a/a/42465368/462608

Websockets仍然可以在哪里?最大的是服务器 - >浏览器推出的二进制数据。 http/2确实允许服务器 - >浏览器按下二进制数据,但在浏览器js中不会暴露出来。对于按下音频和视频帧等应用程序,这是使用Websocket的原因。

当他们说推动二进制数据未在浏览器JS中暴露的二进制数据时,这是什么意思?我要求一些例子来帮助我理解这一点。

https://stackoverflow.com/a/47108355

,因此http2推动实际上是您的浏览器和服务器之间的某些内容,而WebSocket确实会揭示客户端(JavaScript,如果在浏览器上运行)和应用程序代码(在服务器上运行)可以使用的API,以实时传输实时传输实时传输数据。

我请求一些示例,以使我了解http2 push的含义实际上是您的浏览器和服务器和WebSockets(socket.io)具有API之间的含义,客户端可以使用。

这意味着什么?

https://stackoverflow.com/a/42465368/462608

Where might websockets still have a place? The big one is server->browser pushed binary data. HTTP/2 does allow server->browser pushed binary data, but it isn't exposed in browser JS. For applications like pushing audio and video frames, this is a reason to use websockets.

What does it mean when they say that that pushed binary data isn't exposed in browser JS? I request some example to help me understand this.

https://stackoverflow.com/a/47108355

And so HTTP2 push is really something between your browser and server, while Websockets really expose the APIs that can be used by both client (javascript, if its running on browser) and application code (running on server) for transferring real time data.

I request some examples to make me understand the meaning of HTTP2 push is really something between your browser and server and Websockets (socket.io) has API which can be used by clients.

What does all this mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤凫 2025-02-13 20:23:06

在这里,我们谈论的是双向(bidi)消息传递 server client 。在您的情况/问题中,客户是浏览器

要了解发生了什么,我们应该了解什么是HTTP/2和Websocket。

  • 它们俩都是与应用程序层
  • HTTP/2相关的协议是HTTP/1的继任者。尽管WebSocket与HTTP不同,并且是为了在客户端和服务器之间提供全双工通信而创建的。
  • http/2 支持双向流但是您 not not 从JavaScript HTTP/2访问此流
  • 可以使用此流将数据“将”数据推向浏览器。但是,推送的数据只能由浏览器处理,而您的JavaScript对此一无所知。服务器可以使用它来推动其他资源。例如,浏览器请求index.html和index.html返回的index.html和服务器,也请索引。CSS,因为它知道浏览器会请求index.css。因此,当浏览器开始解析index.html并在index.css上查找链接时,它将从缓存中获取。
  • WebSocket提供双向流,您可以访问JavaScript的此流。因此,您可以使用 发送 send 将消息推向服务器的方法。反之亦然,服务器可以使用相同的流/频道将消息推向浏览器,

因此现在我们可以尝试回答问题

当他们说推动二进制数据未在浏览器JS中暴露的二进制数据时,这是什么意思?

这意味着,如果服务器对您的请求“推”其他数据,则您将在JavaScript中不知道它。

fetch('/index.html') 
// Server respond with index.html data and
// "Push" index.css data but in response you get only index.html data
.then((res) => res.text()).then(console.log) // Only index.html data

,因此http2推动实际上是您的浏览器和服务器之间的某些内容,而WebSocket确实会揭示客户端(JavaScript,如果在浏览器上运行)和应用程序代码(在服务器上运行)可以使用的API,以实时传输实时传输实时传输数据。

http/2“按”'其他数据可以由浏览器使用来实现浏览器缓存。您可以阅读有关它的信息在这里
中的属性来获取服务器数据的任何推动

websocket.addEventListener('message', (data) => {
  console.log(data) // Pushed by server data
})

使用WebSocket,您可以使用WebSocket onMessage javaScript 但是 !浏览器提供允许您使用http/2流但以仅阅读模式使用。这意味着您可以订阅服务器事件,但无法将事件从客户端发送到服务器。因此,它仅在一个方向(从服务器 - >到客户端)上工作。另外,如果服务器不支持http/1 http/1.x上的http/2浏览器后备

Here, we are talking about bidirectional(bidi) message passing between server and client. In your case/question, client is a Browser.

To understand what's going on, we should understand what are HTTP/2 and WebSocket.

  • Both of them are protocols related to Application layer
  • HTTP/2 is successor of HTTP/1. While WebSocket is a distinct from HTTP and was created to provide full-duplex communication between client and server.
  • HTTP/2 supports bidirectional stream but you don't have access to this stream from JavaScript
  • HTTP/2 can use this stream to "Push" data to the browser. But pushed data will be processed by only by Browser and your JavaScript would not know anything about it. It can be used by a server to push additional resources. For example Browser requested index.html and server with index.html returns also index.css because it knows that Browser will request index.css. So when browser start to parse index.html and find link on index.css it will take it from the cache.
  • WebSocket provide bidirectional stream, and you have access to this stream from JavaScript. So you can use send method to push a message to the server. And vice versa, the server can push a message to the browser using the same stream/channel

So now we can try to answer on questions

What does it mean when they say that that pushed binary data isn't exposed in browser JS?

It means that if server "Push" additional data on your request you will not know about it in JavaScript.

fetch('/index.html') 
// Server respond with index.html data and
// "Push" index.css data but in response you get only index.html data
.then((res) => res.text()).then(console.log) // Only index.html data

And so HTTP2 push is really something between your browser and server, while Websockets really expose the APIs that can be used by both client (javascript, if its running on browser) and application code (running on server) for transferring real time data.

HTTP/2 "Push"'ed additional data can be used by Browser to fulfil the Browser cache. You can read about it here.
With WebSocket you can get any pushed by server data using WebSocket onmessage property in JavaScript

websocket.addEventListener('message', (data) => {
  console.log(data) // Pushed by server data
})

BUT! Browser provide Server Sent Event(SSE) functionality which allow you to use HTTP/2 stream but in read-only mode. It means that you can subscribe to server events, but could not send events from the client to the server. So it works only in one direction (from server -> to client). Also, if server doesn't support HTTP/2 browser fallback on HTTP/1.x for SSE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文