简体   繁体   English

如何在Angular 2中实现HTTP长轮询

[英]How to implement http long polling in Angular 2

I have a use case as follows: 我有一个用例如下:

  • User selects a video to be uploaded to their profile. 用户选择要上传到其个人资料的视频。
  • Angular sends a request to node.js server which returns an Amazon S3 pre-signed URL. Angular向node.js服务器发送请求,该服务器返回Amazon S3预签名URL。
  • Browser 'directly' uploads the file to S3. 浏览器“直接”将文件上传到S3。
  • Elastictranscoder kicks in to transcode the video. Elastictranscoder加入以对视频进行转码。
  • AWS-SNS follows an https endpoint to inform the node.js back-end of transcoding's completion. AWS-SNS跟随一个https终端节点,以通知node.js后端转码完成。

How to reflect this fact that the video is now available on the Angular side? 如何反映视频现在可以在Angular端获得的事实呢?

I'm doing something similar to the following and it is working fine, but I'm not so sure if the error-case is being handled correctly? 我正在执行与以下类似的操作,并且工作正常,但是我不确定错误情况是否得到正确处理? Should I be doing anything more? 我应该做更多的事情吗?

 startLp(): Observable<any> {
   return this.http
     .get("/getvideostatus?video-id=blah", { headers: this.headers })
     .map(res => {
       return res.json();
     })
     .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}

This is just a regular http request, the only difference is the server not returning the response immediately. 这只是一个常规的http请求,唯一的区别是服务器不会立即返回响应。

Would this constitute a valid http long poll? 这会构成有效的http长轮询吗?

This is what I ended up doing: 这就是我最终要做的事情:

public startLp(): Observable<any> {
let that = this;
let doLp = function(): Observable<any> {
  return that.http
    .get("/getvideostatus?video-id=blah", { headers: that.headers })
    .map(res => {
      return res.json().data
    })
    .catch((error: any) => {                    
      return doLp();
    });
};

return doLp();
}

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

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