你用AJAX无法获得的HTML5网络套接字有什么用?

时间:2022-08-26 19:01:30

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.

我希望iframe沙盒功能对开发人员来说是一个巨大的好处,如果它起飞的话。我个人最喜欢的功能可能是Web Sockets API,它允许与服务器进行双向通信,以便您可以实现游戏,聊天,远程控制等。

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?

使用AJAX无法获得的Web套接字有什么用?它只是方便,还是以某种方式更有效率?服务器是否可以向客户端发送数据,而不必等待消息以便它可以响应?

2 个解决方案

#1


4  

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:

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

  • 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.
  • 长轮询:客户端定期请求更新,例如定期轮询,但如果没有可用的更新,则服务器不会立即响应并保持连接打开。当最终可用更新时,服务器将其推送到客户端,客户端对其进行操作,然后重复该过程。长轮询提供类似推送的更新解决方案,但基本上是一种自我造成的DDoS攻击,并且对于许多类型的Web服务器来说可能是非常耗费资源的。

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

使用WebSockets,您可以获得长轮询的所有响应优势,同时显着降低服务器端开销。

#2


2  

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:

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

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.

在与WebSocket连接期间,客户端和服务器每帧交换2个字节的数据,而连续轮询时则为8千字节的http报头。

#1


4  

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:

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

  • 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.
  • 长轮询:客户端定期请求更新,例如定期轮询,但如果没有可用的更新,则服务器不会立即响应并保持连接打开。当最终可用更新时,服务器将其推送到客户端,客户端对其进行操作,然后重复该过程。长轮询提供类似推送的更新解决方案,但基本上是一种自我造成的DDoS攻击,并且对于许多类型的Web服务器来说可能是非常耗费资源的。

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

使用WebSockets,您可以获得长轮询的所有响应优势,同时显着降低服务器端开销。

#2


2  

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:

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

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.

在与WebSocket连接期间,客户端和服务器每帧交换2个字节的数据,而连续轮询时则为8千字节的http报头。