简体   繁体   English

Angular2:http - 实际长轮询

[英]Angular2: http - actual long polling

I have a 3rd party service to implement, that provides restful API with long polling... they are serving live events, and displaying it as quick as possible is crucial. 我有第三方服务要实现,它提供了长时间轮询的宁静API ......它们正在为现场活动提供服务,并且尽可能快地显示它是至关重要的。

Sadly that service does not provide push notifications, so we need to deal with what we have.... 遗憾的是,该服务不提供推送通知,因此我们需要处理我们拥有的内容....

so one of the APIs has a long-polling functionality, so in theory the idea is, I query the API, establish an open channel for 30 seconds and wait for the changes... (and publish the changes on the FE)... and then timeout the connection, establish a new one and repeat the process. 所以其中一个API具有长轮询功能,所以理论上我的想法是,我查询API,建立一个30秒的开放通道并等待更改......(并在FE上发布更改)。然后超时连接,建立一个新连接并重复该过程。

Should be pretty straight forward.... 应该很直接....

But so far, I couldn't find anything in angular's docs about long polling... the only thing I found was polling in connection with rxJS... so setting an inteval on how often I query the API... (which in my case would be every 30s)... but nothing about on leaving the channel open and listen for the changes... 但到目前为止,我在angular的文档中找不到任何关于长轮询的内容......我发现的唯一一件事是与rxJS相关的轮询...所以设置一个关于我查询API的频率的... ...(在我的情况是每30秒一次)......但没有关于打开频道并听取变化的事情......

found this thred: How to implement http long polling in Angular 2 发现这个问题: 如何在Angular 2中实现http长轮询

but it is not talking about this problem. 但它不是在谈论这个问题。

I don't want to end up querying the API every second. 我不希望每秒都查询API。

Any ideas? 有任何想法吗?

I considered implementing signalR (which might not really help here), but it relies on jQuery... which I don't want to add to the bundle unless is 100% necessary! 我考虑过实现signalR(在这里可能没什么帮助),但是它依赖于jQuery ...除非是100%必要,否则我不想添加到包中!

I think you misunderstood the concept of long-pulling.. Long-pulling where the client polls the server requesting new information. 我认为你误解了长拉的概念。长拉动客户端轮询服务器请求新信息。 The server holds the request open until new data is available. 服务器保持请求打开,直到有新数据可用。 Once available, the server responds and sends the new information. 一旦可用,服务器响应并发送新信息。 When the client receives the new information, it immediately sends another request, and the operation is repeated. 当客户端收到新信息时,它会立即发送另一个请求,并重复该操作。 This effectively emulates a server push feature. 这有效地模拟了服务器推送功能。

在此输入图像描述

It you want to keep the connection alive you need to use webSoket. 您希望保持连接活动,您需要使用webSoket。 WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time. WebSockets提供客户端和服务器之间的持久连接,双方可以使用它始终开始发送数据。 The client establishes a WebSocket connection through a process known as the WebSocket handshake. 客户端通过称为WebSocket握手的进程建立WebSocket连接。 This process starts with the client sending a regular HTTP request to the server 此过程从客户端向服务器发送常规HTTP请求开始

在此输入图像描述

But I didn't understood from your question why you can't send another fetch request when the long-pulling completes, something like: 但是我没有从你的问题中理解为什么当长拉完成时你不能发送另一个获取请求,例如:

myLongPullingFunc(){
this.http.get(longPullingURL)
.subscribe(res=>{
  if(sholdFetchData){
    useFetchedData(res);
    this.myLongPullingFunc();
   }else
      doSomethingElse()
    })
 }

You can read more about long-pulling here and on webSokects here 您可以在此处阅读有关长期拉动和webSokects的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM