如何在Angular 2中实现http长轮询 [英] How to implement http long polling in Angular 2

查看:1306
本文介绍了如何在Angular 2中实现http长轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例如下:


  • 用户选择要上传到他们个人资料的视频。

  • Angular向node.js服务器发送请求,返回Amazon S3预先签名的URL。

  • 浏览器直接将文件上传到S3。

  • Elastictranscoder开始对视频进行转码。

  • AWS-SNS跟随https端点通知node.js转码完成的后端。

  • 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.
  • Browser 'directly' uploads the file to S3.
  • Elastictranscoder kicks in to transcode the video.
  • AWS-SNS follows an https endpoint to inform the node.js back-end of transcoding's completion.

如何反映这个视频现在可以在Angular方面获得的事实?

How to reflect this fact that the video is now available on the Angular side?

I我正在做类似以下的事情并且工作正常,但我不确定错误案例是否正确处理?我应该做更多吗?

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'));
}

这只是一个常规的http请求,唯一的区别是服务器没有返回立即回应。

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?

推荐答案

这就是我最终要做的事情:

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();
}

这篇关于如何在Angular 2中实现http长轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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