首先将您的中心事件作为可观察的。我调整了 this code 以对抗 ASP.NET Core SignalR。
然后创建一个史诗来启动集线器(我正在使用 typescript),在启动时在某处分派该操作。
const startNotificationHubEpic: Epic<RootAction, RootAction, RootState, Services> = (action$, store, { notificationHub }) => {
return action$.pipe(
filter(isActionOf(startNotificationHub.request)),
switchMap(action =>
from(notificationHub.start()).pipe(
map(() => startNotificationHub.success()),
catchError(err => of(startNotificationHub.failure(err)))
)));
}
最后使用 startNotificationHub.success 操作开始在集线器上为您感兴趣的每个事件监听 rsjx 事件流。
const listenForMessageEpic: Epic<RootAction, RootAction, RootState, Services> = (action$, store, { notificationHub }) => {
return action$.pipe(
filter(isActionOf(startNotificationHub.success)),
switchMap(act =>
notificationHub.on("Message", (messag: string) => ({message})).pipe(
map(notif => onMessage(notif))
)));
}
反过来,onMessage 操作可以被另一个史诗使用或在 reducer 中用于将通知数据传递给组件。