使用 HTML5 Web 套接字可以获得哪些 AJAX 无法获得的功能?

发布于 2024-10-22 03:38:05 字数 429 浏览 2 评论 0原文

伊恩·希克森

我期待 iframe 沙箱功能 如果能够的话,对于开发者来说将会是一个很大的福音 起飞。我个人最喜欢的 功能可能是 Web Sockets API,允许双向 与服务器通信,以便 你可以实现游戏、聊天、 遥控器等。

使用 Web 套接字可以获得哪些 AJAX 无法获得的功能?这只是方便,还是更有效率?服务器是否可以向客户端发送数据,而无需等待消息才能响应?

Ian Hickson says:

I expect the iframe sandboxing feature
will be a big boon to developers if it
takes off. My own personal favorite
feature is probably the Web Sockets
API, which allows two-way
communication with a server so that
you can implement games, chatting,
remote controls, and so forth.

What can you get with web sockets that you can't get with AJAX? Is it just convenience, or is it somehow more efficient? Is it that the server can send data to the client, without having to wait for a message so it can respond?

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

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

发布评论

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

评论(2

粉红×色少女 2024-10-29 03:38:05

是的,这都是关于服务器能够将数据推送到客户端的。目前,在不使用 Flash/Silverlight/Java/ActiveX 的情况下模拟双向通信采用以下两种解决方法之一的形式:

  • 传统轮询:客户端频繁向服务器发出小请求,检查更新。即使没有发生更新,客户端也不知道,并且必须不断轮询更新。尽管每个请求可能都是轻量级的,但许多客户端的持续轮询可能会很快累积起来。
  • 长轮询:客户端定期发出更新请求,就像定期轮询一样,但如果尚无可用更新,则服务器不会立即响应并保持连接打开。当更新最终可用时,服务器将其推送到客户端,客户端对其进行操作,然后重复该过程。长轮询提供类似推送的更新解决方案,但基本上是一种自我造成的 DDoS 攻击,对于许多类型的 Web 服务器来说可能会占用大量资源。

借助 WebSocket,您可以获得长轮询的所有响应能力优势,同时显着减少服务器端开销。

Yes, it's all about the server being able to push data to the client. Currently, simulating bi-directional communication without Flash/Silverlight/Java/ActiveX takes the form of one of two workarounds:

  • Traditional polling: Clients make small requests to the server frequently, checking for updates. Even if no update has occurred, the client doesn't know that and must continuously poll for updates. Though each request may be lightweight, constant polling by many clients can add up quickly.
  • Long polling: Clients make periodic requests for updates, like regular polling, but if there are no updates yet available then the server does not respond immediately and holds the connection open. When an update is finally available, the server pushes that down to the client, which acts on it and then repeats that process. Long polling offers push-like update resolution, but is basically a self-inflicted DDoS attack and can be very resource intensive for many types of web servers.

With WebSockets, you get all of the responsiveness advantages of long polling, with dramatically less server-side overhead.

笑看君怀她人 2024-10-29 03:38:05

WebSocket 比 AJAX 调用更高效(并且“更实时”),因为您可以保持连接打开,并且在每次请求和响应后不会发送额外的协议标头和其他内容。看看 这篇文章:

在与
WebSocket,客户端和服务器交换
每帧数据,每帧 2 个字节,
与 8 KB 的 http 相比
当您进行连续轮询时的标题。

WebSockets are more efficient (and "more real-time") than AJAX calls because you keep connection open and don't send extra protocol headers and other stuff after each request and response. Look at this article:

During making connection with
WebSocket, client and server exchange
data per frame which is 2 bytes each,
compared to 8 kilo bytes of http
header when you do continuous polling.

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