EventSource使我所有其他呼叫处于待处理状态

时间:2019-06-05 01:47:32

标签: javascript events server-sent-events

我在带有事件源的项目中使用SSE

  init(channel: string) {
    if (this.eventSource) { return; }

    this.eventSource = new EventSource(`${environment.SSE_URL}?channel=${channel}`);
    this.eventSource.onmessage = (e) => {console.log('Connected');};
  }

然后在另一个地方我订阅频道

  protected callListener(cb: (d) => void): (e) => void {
    const callee = ({data}): void => {
      const d = JSON.parse(data);
      cb(d.message);
    };
    return callee;
  }

  private addEventToEventSrc(event: string, callback: any, owner: string): void {
    console.log(`Subscribed to ⇢ ${event} (owned by: ${owner})`);
    const cb = this.callListener(callback);
    this.eventSource.addEventListener(event, cb);
    if (!this.callbacks.get(`${event}_${owner}`)) {
        this.callbacks.set(`${event}_${owner}`, cb);
    }
  }

  subscribe(event: string, callback: any, owner?: string): void {
    if (!this.eventSource) { return; }
    if (!owner) { owner = 'default'; }
    if (!event) { event = 'message'; }
    this.addEventToEventSrc(event, callback, owner);
}

问题是当我打开EventSource时,对服务器或任何其他服务器的任何其他请求都将无限地挂起。如果我直接命中服务器,他的确会做出响应,但是fron端部分将永远停留在每个api调用的待处理状态。

0 个答案:

没有答案