gpt4 book ai didi

.net - 如何将 signalR 与 Redux-observable 一起使用?

转载 作者:行者123 更新时间:2023-12-04 02:58:10 26 4
gpt4 key购买 nike

我已经创建了 Angular SPA 应用程序。我在服务器端使用 ASP.net Core,客户端状态由 redux-observable 使用 action-reducer-epics 处理。

结构如下:我配置了存储、根史诗和根 reducer ,然后每个组件都有自己的史诗、 reducer 和服务文件。

我想在 redux-observable 中容纳 signalR,但无法正确集成它。

最佳答案

首先将您的中心事件作为可观察对象。我改编了this code对抗 ASP.NET Core SignalR。

然后创建史诗来启动集线器(我使用的是 typescript ),在启动时在某处分派(dispatch)该操作。

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 中用于将通知数据传递给组件。

关于.net - 如何将 signalR 与 Redux-observable 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51879400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com