如何仅从 signalR 获取少数项目的通知? (EF核心和Angular8)

How to get notification only for few items from signalR? (EF core and Angular 8)

在我们的 .Net 核心和 Angular 8 应用程序中,我们已经实现了 SignalR。我们需要不同事件的实时通知。

事件(在我们的应用程序中)是一个动态创建的项目。 (生日活动、周年纪念活动等)

假设用户可以打开任何事件。并且很少有民意调查和聊天正在进行中。每当用户在事件中更改某些内容时,我需要通知打开相同事件的其他客户。

目前,来自所有事件的通知到达所有客户端(无论他们打开的事件如何)

public class NotificationHub : Hub    {
    public async Task UpdateVote(int eventId, int pollId, int pollOptionId)
    {
        await Clients.Others.SendAsync("VoteUpdated", eventId, pollId, pollOptionId);
    }
}

Angular:

private CreateConnection() {
    this._hubConnection = new HubConnectionBuilder()
      .withUrl(environment.base + 'NotificationHub')
      .build();
  }

  private StartConnection(): void {
    this._hubConnection
      .start()
      .then(() => {
        this.connectionIsEstablished = true;
        console.log('Hub connection started');
        this.connectionEstablished.emit(true);
      })
      .catch(err => {
        console.log('Error while establishing connection, retrying...');
        setTimeout(function () { this.StartConnection(); }, 5000);
      });
  }

UpdateVote(eventId, pollId, pollOptionId) {
    this._hubConnection.invoke('UpdateVote', eventId, pollId, pollOptionId);
  }

private RegisterOnServerEvents(): void {
    this._hubConnection.on('VoteUpdated', (eventId, pollId, pollOptionId) => {
      this.VoteUpdated.emit({ eventId, pollId, pollOptionId });
    });
  }

我正在加载网站时打开连接。

我需要执行什么才能仅通知客户他们已打开的事件?

将它们添加到一个组中。就像每当用户访问事件时将它们添加到 "Event-" + eventId 之类的组中,并且每当发生更改时仅通知该组。记得在他们关闭该页面时从组中删除

public async AddToTrackGroup(string eventId) {
        return Groups.Add(Context.ConnectionId, "event-" + eventId);
}

public async Task UpdateEventViewer(string eventId)
{
    Clients.Group("event-" + eventId).notifyEventChanged();
}