使用angular的websocket时,是否有使用http客户端服务调用api的需求/优势? [英] Is there any need/advantage of calling an api using the http client service when using websockets in angular?

查看:164
本文介绍了使用angular的websocket时,是否有使用http客户端服务调用api的需求/优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用socket.io库和MEAN堆栈构建一个简单的聊天应用程序来学习如何使用socket.io.

I am trying to learn how to use socket.io by building a simple chat app using the socket.io library and MEAN stack.

通过查看一些开源项目(像这样的),我已经看到客户端,主要是在执行聊天逻辑时,是通过websockets与服务器进行通信,而不是通过angular提供的http客户端服务进行通信.

From looking at some open source projects(like this one) I have seen that the client, mainly when executing the chat logic, communicates with the server via websockets and not the http client service provided by angular.

这是否意味着在使用网络套接字进行实时更新时,无需使用http与服务器进行通信?

Does this mean that when using web-sockets for real time updates there is no need to use http to communicate with the server?

推荐答案

大多数情况下肯定不会.不过,这不是角度问题,只是为正确的事情选择正确的工具.

Most assuredly not. It is not a matter of angular though, it's just choosing the right tool for the right thing.

长话短说:

  • 如果它是请求/响应模型,请使用http.为什么?
  • If it is a request/response model, then use http. Why?
  1. 因为它更容易处理.代理,DNS和负载平衡器不需要额外的配置即可处理. Web套接字可以.
  2. 您已经设置了1,所以这不是问题.您将如何处理缓存,路由,gzip,SEO以及http协议和rest-apis处理的所有现成东西?您构建的所有内容,所有通信都需要有自己的安全考虑,设计模式等.
  3. 您将如何处理Web套接字的状态性质?他们目前仅支持垂直缩放,而其他api则同时支持水平和垂直缩放.

如果您确实需要全双工通信(仅在没有套接字的情况下进行服务器推送),则应将Web套接字的使用限制在真正需要的情况下.

If you really need a full duplex communication (there is server push only without sockets), then you should limit web socket usage to the cases where you really need it.

即使在这种情况下,也要通过类似 signalR 的框架.所有现代的浏览器都支持websocket,但是许多用户仍然没有支持它们的浏览器.在这些情况下,SignalR会退回长时间轮询.如果在所有情况下都使用它,请想象一下,如果使用这样的浏览器,并对每个请求进行长时间轮询,将会发生什么情况.

Even in this case, go through a framework like signalR. All modern browsers support websockets but a lot of users still don't have browsers that do support them. SignalR falls back to long polling in those cases. If you use it on all cases, imagine what would happen if you use such a browser and you apply long polling for each request.

我可以继续,但是我想你明白了.

I could go on, but I think you get the meaning.

这篇关于使用angular的websocket时,是否有使用http客户端服务调用api的需求/优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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