简体   繁体   English

如何在javascript中获取所有待处理的http请求?

[英]How to get all the pending http requests in javascript?

Is there a way in javascript or angular2 有没有javascript或angular2的方法

to get the list of pending http requests? 获取待处理的http请求列表?

The goal is to start 'several other processes' 目标是开始'其他几个过程'

according to the fluctuation of this list. 根据这份清单的波动。

Does something like an accessible stack of requests exist? 是否存在类似可访问的请求堆栈?

Thanks. 谢谢。

In fact you can extend the Http class to intercept request execution. 实际上,您可以扩展Http类来拦截请求执行。

import {Injectable} from 'angular2/core';
import {Http,ConnectionBackend,RequestOptions,RequestOptionsArgs,Request} from 'angular2/http';
import 'rxjs/Rx';
import {MonitoringService} from './monitoring.service';

@Injectable()
export class CustomHttp extends Http {
  constructor(backend: ConnectionBackend,
              defaultOptions: RequestOptions,
              private monitoring:MonitoringService) {
    super(backend, defaultOptions);
  }

  request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
    console.log('request...');
    return super.request(url, options);
  }

  get(url: string, options?: RequestOptionsArgs): Observable<Response> {
    console.log('get...');
    return super.get(url, options);
  }

  (...)
}

You can leverage the finally operator to intercept the completion of observables corresponding to HTTP requests. 您可以利用finally运算符来拦截与HTTP请求相对应的observable的完成。 You can increment a property before the call and decrement it in the finally operator. 您可以在调用之前递增属性,并在finally运算符中递减它。

get(url: string, options?: RequestOptionsArgs): Observable<Response> {
  this.monitoring.pendingRequestsNumber++;
  return super.get(url, options).finally(() => {
    this.monitoring.pendingRequestsNumber--;
  });
}

This CustomHttp class can be registered like this. 这个CustomHttp类可以像这样注册。 I added a monitoring service to store (and share) the number of pending requests: 我添加了一个监控服务来存储(和共享)待处理请求的数量:

import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {HTTP_PROVIDERS,Http,XHRBackend,RequestOptions} from 'angular2/http';
import {AppComponent} from './app.component';
import {CustomHttp} from './http.custom';
import {MonitoringService} from './monitoring.service';

bootstrap(AppComponent, [HTTP_PROVIDERS,
  MonitoringService,
  provide(Http, {
    useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, monitory:MonitoringService) => new CustomHttp(backend, defaultOptions, monitory),
    deps: [XHRBackend, RequestOptions, MonitoringService]
  })
]);

I created a plunkr describing the way to implement this approach: https://plnkr.co/edit/qHNn5amI0byci9RMkZyE?p=preview . 我创建了一个plunkr来描述实现这种方法的方法: https ://plnkr.co/edit/qHNn5amI0byci9RMkZyE?p = preview。

You need to be on the backend if you want to do that. 如果你想这样做,你需要在后端。 If your web site is hosted on php then you cannot do it in javascript. 如果您的网站托管在PHP上,那么您无法在javascript中执行此操作。 But if you use node.js then it is certainly possible because node.js uses javascript on the backend. 但是如果你使用node.js那么肯定是可能的,因为node.js在后端使用javascript。

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

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