作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经创建了 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/
我是一名优秀的程序员,十分优秀!