HTTP Get无法在Angular上工作

时间:2018-07-30 13:35:53

标签: angular typescript http get

我正在使用一个应用程序,其中有一个客户端在Angular中,而服务器在C中。我的HTTP Get出现问题,但找不到。每70毫秒调用一次“ updateStream()”。

public updateStream(): void {
   if (this.isVideoRunning && this.canChangePicture) {
     console.log('put is called');
     this.putData();
   }
}

public getData(): Observable<any> {
   return this.httpClient.get('http://IP_ADRESS:3490/testStream', { responseType: 'arraybuffer' })
}

public putData(): void {
   console.log('put function');
   this.canChangePicture = false;
   let comp: number;
   this.getData().subscribe((resp) => {
      console.log(resp);
      const responseArray: Uint8ClampedArray = new Uint8ClampedArray(resp);
      const newTabTemp: Uint8ClampedArray = new Uint8ClampedArray(this.maxHeight * this.maxWidth * 4);
      comp = 0;
      for (let i = 0; i < this.maxHeight * this.maxWidth * 4; i = i + 4) {
         newTabTemp[i] = responseArray[comp];
         newTabTemp[i + 1] = responseArray[comp];
         newTabTemp[i + 2] = responseArray[comp];
         newTabTemp[i + 3] = 255;
         comp = comp + 1;
     }

  let nouvData: ImageData;
  nouvData = new ImageData(newTabTemp, this.maxWidth, this.maxHeight);
  this.contextVideo.putImageData(nouvData, BORDERX / 2, BORDERY / 2);
  this.canChangePicture = true;
  });
}

我的问题是我的画布几乎不会改变,因此我尝试放置一些“ console.log”,但我发现问题出在某种程度上我的程序无法输入“ this.getData()。subscribe()”。 ”。

 Those are my console.log

您可以看到我的程序继续输入“ putData()”,而不是“ this.getData()。subscribe()...”。我的代码有什么问题以及如何解决?

编辑:

未决请求的屏幕截图 Pending request 时间标签:Timing tab 要求/回应 enter image description here

2 个答案:

答案 0 :(得分:0)

您可以在“网络”标签中单击一个请求,然后查看其为何待处理。

如果已停止,则表明Chrome已将其排队。了解更多about stalling and how to resolve it

如果其服务器没有响应,则需要检查后端。

如果要求允许,可以在上一个完成后调用updateStream()。

如果要求允许,您甚至可以选择Websockets或SSE来实现比HTTP方法更快的通信。

答案 1 :(得分:0)

您有3个console.log

  1. updateStream函数,console.log('put is called');
  2. putData函数console.log('put function')
  3. getData.Subscribe()内的
  4. console.log(resp)

在您的console.log中,我可以看到您的:

Observable <ArrayBuffer>  

HttpClient的响应,这意味着您进行了请求/响应。

您可以在Cromme Dev Tools中显示此响应吗?