使用WebSockets,有没有对AJAX的地方吗? [英] With websockets, is there a place for AJAX?

查看:110
本文介绍了使用WebSockets,有没有对AJAX的地方吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设一个实时的应用程序中使用节点。我使用socket.io来驱动我的实时交互,但jQuery的加载,所以我必须AJAX提供给我。我最初使用socket.io服务器和客户端之间我的所有通信。

I'm currently building a realtime application using Node. I'm using socket.io to power my real-time interactions, but have jQuery loaded, so I have AJAX available to me. I initially used socket.io for all my communication between the server and client.

我开始认为AJAX可能更适合于某些情况下,像这样的RESTful交易异步的,因为我没有写在我的插座来处理每个新的事务的独立消息的情况下,以及编写基于REST路由。

I'm starting to think that AJAX might be better suited for certain cases like doing RESTful transactions asynchronously, because I don't have to write a separate message case in my socket to handle each new transaction as well as write the RESTful routing.

我想知道如果我在的东西,或者如果它最好用插座性能还是其他什么东西,我没有想过。

I'm wondering if I am on to something or if its best to use sockets for performance or something else I'm not thinking about.

谢谢! 马特·穆勒

推荐答案

是的,的WebSockets( RFC 6455 )和Ajax有很大的不同,并服务于不同的目的。

Yes, WebSockets (RFC 6455) and Ajax are quite different and serve different purposes.

正如你所说,使用Ajax可以做RESTful请求。这意味着,你可以利用例如像现有的HTTP基础设施代理为缓存请求,并使用条件get请求。 Ajax请求可能是相当重量级的,因为每一个Ajax请求包含HTTP头,包括饼干。

As you say, with Ajax you can do RESTful requests. This means that you can take advantage of existing HTTP-infrastructure like e.g. proxies to cache requests and use conditional get requests. Ajax request may be quite heavy-weight since every Ajax request contains HTTP headers and include cookies.

的WebSockets设计用于低延迟双向通信。按照设计,的WebSockets都有每个消息中非常小的开销。例如。的WebSockets消息不必包括任何HTTP标头,并且可在将来被用于VoIP和在两个方向流

WebSockets is designed for low latency bi-directional communication. By design, WebSockets has very little overhead in each message. E.g. WebSockets messages doesn't have to include any HTTP Headers, and may in future be used for VoIP and streaming in both directions.

另一个区别是,阿贾克斯可以是无国籍服务器上使用。例如。如果你有多个服务器的网络负载平衡,任何服务器可以处理Ajax请求,即使重新启动后(或升级)。的WebSocket的是连接和使用的状态服务器,因此它可能是更难使用多台服务器吧。

Another difference is that Ajax can be used with stateless servers. E.g. if you have your web load balanced with multiple servers, any server can handle an Ajax request, even after reboot (or upgrade). Websocket's are "connected" and use a stateful server, so it may be harder to use multiple servers with it.

还有服务器发送的事件,是类似于WebSockets的,在该服务器可以将数据推送到客户机(其可以不使用Ajax未经黑客来完成(例如,彗星)),而且它也可以处理自动重新连接。但它是仅在一个方向(服务器到客户端)的消息。请参见 HTML5服务器端事件:EventSource的与包装的WebSocket

There is also Server Sent Events, that are similar to WebSockets, in that the server can push data to the client (which can't be done with Ajax without hacks (e.g. comet)), and it can also handle automatic reconnections. But it's only for messages in one direction (server to client). See HTML5 Server-Side Event: EventSource vs. wrapped WebSocket.

这篇关于使用WebSockets,有没有对AJAX的地方吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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