4

与仅通过 AJAX 提供信息相比,使用 TCP 编程的站点(即站点上的某人连接到服务器并通过 TCP 交换信息)如何扩展?假设交换的信息是相同的。

试图澄清:我特别问的是规模:我已经读过,与仅静态提供信息相比,保持数千个 TCP 连接需要资源(哪个?)。我想知道这是否正确。

4

4 回答 4

1

Websockets are not a one-for-one with AJAX; they offer substantially different features. Websockets offers the ability to 'push' data to the client. AJAX works by 'pushing' data and returning a response.

The purpose of WebSockets is to provide a low-latency, bi-directional, full-duplex and long-running connection between a browser and server. WebSockets opens up possibilities with browser applications that were previously unavailable using HTTP or AJAX.

However, there is certainly an overlap in purpose between WebSockets and AJAX. For example, when the browser wants to be notified of server events (i.e. push) either AJAX or WebSockets are both viable options. If your application needs low-latency push events then this would be a factor in favor of WebSockets which would definitely scale better in this scenario. On the other hand, if you need to work with existing frameworks and deployed technologies (OAuth, RESTful API's, proxies, etc.) then AJAX is preferable.

If you don't need the specific benefits that WebSockets provides, then it's probably a better idea to stick with existing techniques like AJAX because this allows you to re-use and integrate with an existing ecosystem of tools, technologies, security mechanisms, knowledge bases that have been developed over the last 7 years.

But overall, Websockets will outperform AJAX by a significant factor.

于 2013-01-26T19:33:20.283 回答
1

WebSockets 是一种允许服务器向客户端推送通知的技术。另一方面,AJAX 是一种拉取技术,这意味着客户端正在向服务器发送请求。

因此,例如,如果您有一个应用程序需要定期从服务器接收通知并更新其 UI,那么 WebSocket 更适合并且更好。使用 AJAX,您将不得不定期向服务器发送请求,以查看服务器上的某些状态是否发生了变化。使用 WebSockets,服务器会通知客户端服务器上发生的某些事件。这将在一个请求中发生。

所以我想这真的取决于你正在开发的应用程序的类型,但是 WebSockets 和 AJAX 是解决不同类型问题的两种完全不同的技术。选择哪一个取决于您的情况。

于 2013-01-26T19:24:27.103 回答
0

我认为 WebSockets 和标准 TCP 连接之间的可伸缩性没有任何区别。WebSocket 是从静态单向管道到双工管道的升级。物理资源完全相同。

WebSockets 的主要优点是它们在端口 80 上运行,因此它避免了大多数防火墙问题,但您必须首先通过标准 HTTP 连接。

于 2013-01-26T20:04:20.450 回答
0

这是一个很好的页面,它清楚地展示了 WebSocket API 与 Ajax 长轮询(尤其是大规模)相比的优势:http ://www.websocket.org/quantum.html

这基本上归结为这样一个事实,即一旦建立了初始 HTTP 握手,数据可以更快地来回传输,因为标头开销大大减少(这就是大多数人所说的双向通信)。

顺便说一句,如果您只需要能够定期从服务器推送数据,但您不需要发出许多客户端发起的请求,那么使用HTML5 服务器发送的事件和偶尔的 Ajax 请求客户端可能正是您所需要的,并且比 WebSocket API 更容易实现。

于 2013-01-27T00:54:31.173 回答