【问题标题】:EventSource stop recieving any update from serverEventSource 停止从服务器接收任何更新
【发布时间】:2019-09-17 06:23:07
【问题描述】:

我有一个关于 EventSource 的奇怪错误。

我有一个服务器,它通过 EventSource 向 UI 永久发送一些事件。直到几天前,一切正常。 最近有一个更新,服务器现在在某个频道上发送新数据。

问题是,由于我尚未发现的原因,有时 EventSource 现在停止工作。

连接仍然是打开的,请求的状态仍然是挂起并且没有关闭,并且控制台上完全没有错误。服务器还在将事件流式传输到 UI。但是 EventSource 不再更新了。我也尝试直接使用 curl 请求,但他没有得到任何更新,直到我手动刷新。

这是我的客户代码 =>

import { Injectable, HostListener, OnDestroy } from '@angular/core';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class SSEService implements OnDestroy {
  private eventSource: EventSource;
  private callbacks: Map<string, (e: any) => void> = new Map<string, (e: any) => void>();

  init(channel: string) {
    if (this.eventSource) { return; }
    console.log('SSE Channel Start');
    this.eventSource = new EventSource(`${environment.SSE_URL}?channel=${channel}`);
  }

  protected callListener(cb: (d) => void): (e) => void {
    const callee = ({data}): void => {
      try {
        const d = JSON.parse(data);
        cb(d.message);
      } catch (e) {
        console.error(e, data);
      }
    };
    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);
}

  unsubscribe(event: string, owner?: string): void {
    if (!this.eventSource) { return; }
    if (!owner) { owner = 'default'; }
    if (!event) { event = 'message'; }
    if (this.callbacks.get(`${event}_${owner}`)) {
      console.log(`Unsubscribed to ⇢ ${event} (owned by: ${owner})`);
      this.eventSource.removeEventListener(event, this.callbacks.get(`${event}_${owner}`));
    }
    this.callbacks.delete(`${event}_${owner}`);
  }

  @HostListener('window:beforeunload')
  onBrowserClose() {
    this.clearAll();
  }

  ngOnDestroy() {
    this.clearAll();
  }

  clearAll() {
    if (this.eventSource) {
      console.log('SSE Channel Closed');
      this.eventSource.close();
      this.eventSource = null;
    }
    this.callbacks = new Map<string, (e: any) => void>();
  }
}

我尝试记录收到的数据,然后尝试捕获...但它从未显示任何错误,它只是停止获取更新。

从服务器发送的数据可能是问题,但服务器不会停止流,所以我的 EventSource 应该崩溃或继续运行。

但现在它正在默默地删除任何更新。

如果你能告诉我可能的原因,我将非常感激

【问题讨论】:

    标签: javascript angular events eventsource


    【解决方案1】:

    事实证明,我的 EventSource 客户端实现是正确的。

    服务器端,我们有 1 个服务器和一个调度器,服务器在同一个频道上发送大量更新,延迟很短。 EventSource 将所有这些消息视为单个大消息,并停止刷新,直到他认为消息结束。但这并没有发生,因此它在尝试下载时卡在了打开状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-10
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多