在 ajax 中使用 websocket/socket.io 的缺点是什么? [英] What is the disadvantage of using websocket/socket.io where ajax will do?

查看:33
本文介绍了在 ajax 中使用 websocket/socket.io 的缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前也有人问过类似的问题,他们都得出了AJAX不会过时的结论.但是 ajax 在哪些方面比 websockets 更好?

Similar questions have been asked before and they all reached the conclusion that AJAX will not become obsolete. But in what ways is ajax better than websockets?

使用 socket.io,很容易退回到 Flash 或长轮询,因此浏览器兼容性似乎不是问题.

With socket.io, it's easy to fall back to flash or long polling, so browser compatibility seems to be a non-issue.

Websockets 是双向的.ajax 会发出异步请求,websocket 客户端会向服务器发送消息.POST/GET 参数可以用 JSON 编码.

Websockets are bidirectional. Where ajax would make an asynchronous request, websocket client would send a message to the server. The POST/GET parameters can be encoded in JSON.

那么使用 100% websockets 有什么问题呢?如果每个访问者都与服务器保持一个持久的 websocket 连接,那会比在整个访问会话中发出几个 ajax 请求更浪费吗?

So what is wrong with using 100% websockets? If every visitor maintains a persistent websocket connection to the server, would that be more wasteful than making a few ajax requests throughout the visit session?

推荐答案

我觉得这样比较浪费.对于每个连接的客户端,您都需要某种对象/功能/代码/服务器上与该客户端配对的任何内容.套接字处理程序或文件描述符,或者您的服务器设置为处理连接.

I think it would be more wasteful. For every connected client you need some sort of object/function/code/whatever on the server paired up with that one client. A socket handler, or a file descriptor, or however your server is setup to handle the connections.

使用 AJAX,您不需要服务器端资源到客户端的 1:1 映射.与服务器端资源相比,您的客户端数量可以更少地依赖于扩展.即使 node.js 也有它可以处理和保持打开的连接数的限制.

With AJAX you don't need a 1:1 mapping of server side resource to client. Your # of clients can scale less dependently than your server-side resources. Even node.js has its limitations to how many connections it can handle and keep open.

要考虑的另一件事是某些 AJAX 响应也可以缓存.随着规模的扩大,您可以添加一个 HTTP 缓存来帮助减少频繁的 AJAX 请求带来的负载.

The other thing to consider is that certain AJAX responses can be cached too. As you scale up you can add an HTTP cache to help reduce the load from frequent AJAX requests.

这篇关于在 ajax 中使用 websocket/socket.io 的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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