Angular 从websocket可以观察到角度rxjs变化异步

Angular 从websocket可以观察到角度rxjs变化异步,angular,rxjs,behaviorsubject,Angular,Rxjs,Behaviorsubject,嗨,我正在使用angular 7和rxjs async 在我的组件中,我将ngFor与异步观察器一起使用 <item-comp [item]="item" *ngFor="let item of groupsService.selectedItems$ | async; "> </item-comp> 这是可行的,但现在我需要能够更改特定项以响应websocket消息。 我有一个websocket连接,用于处理更新项目的消息。有没有一种方法可以使用rxjs的反应式方法

嗨,我正在使用angular 7和rxjs async

在我的组件中,我将ngFor与异步观察器一起使用

<item-comp [item]="item" *ngFor="let item of groupsService.selectedItems$ | async; ">

</item-comp>
这是可行的,但现在我需要能够更改特定项以响应websocket消息。
我有一个websocket连接,用于处理更新项目的消息。有没有一种方法可以使用rxjs的反应式方法来实现这一点?

您可以为websocket更新创建一个更高阶的函数,然后将其与http请求链接起来

const onUpdate=(items)=>updateFromWebSocket.pipe(
   map(itemUpdates=>{........ return updatedItems}
   startWith(items)
)

selectedItems$.pipe(switchMap(items=>onUpdate(items)).subscribe()

嗨,谢谢你的回复。使用您的解决方案每次从websocket收到消息时都会发出新的http请求?这似乎没有效率,是否有一种方法可以创建一个新流,以某种方式将http结果与websocket更新合并,而无需每次发出http请求?on update是http请求之后的后续流。所以它不会触发http请求。事实上,http请求将切换到侦听套接字消息
public selectedItems$ = this.groupSelected$.pipe(
    switchMap((group: any) => {
        if (!group)
          return new EmptyObservable();

        return this.http.get('/api/'+ group)
          .pipe(
            map((res: any) => {
                return res.items;
              }
            )
          )
      }
    )
  )
const onUpdate=(items)=>updateFromWebSocket.pipe(
   map(itemUpdates=>{........ return updatedItems}
   startWith(items)
)

selectedItems$.pipe(switchMap(items=>onUpdate(items)).subscribe()