【问题标题】:Angular: RxStomp Watch, Configure, Activate MethodAngular:RxStomp 监视、配置、激活方法
【发布时间】:2021-03-03 20:33:08
【问题描述】:

在 Stomp 中,我们使用了这些方法:

initUserEvents() {

this.stompService.startConnect().then(() => {
      this.stompService.done('init');

this.stompService.subsribe('/channel/login', res => {
      if (res.username !== this.username) {

        this.newConnectedAccounts.push(res.username);

现在我正在使用 RxStomp,但我无法找到使用这些“替换”方法的正确方法:

initUserEvents() {

this.stompService.configure();
    this.stompService.activate(){

    this.stompService.watch('/channel/login', res => {
      if (res.username !== this.username) {
        this.newConnectedAccounts.push(res.username);

我得到的错误是: TS2345:'(res: any) => void' 类型的参数不能分配给'StompHeaders' 类型的参数。 '(res: any) => void' 类型中缺少索引签名。

【问题讨论】:

    标签: angular websocket stomp stompjs


    【解决方案1】:

    查看 RxStomp 文档。它说:

    主要区别在于它将操作公开为 RxJS Observables。例如,当订阅 STOMP 端点时,它会返回一个 Observable,它将流式传输所有接收到的消息。

    除了 beforeConnect,与 @stomp/stompjs Client 中所有回调相关的功能都公开为 Observables/Subjects/BehaviorSubjects。

    换句话说,您不会将回调传递给watch 方法。它返回一个Observable,您可以使用它来获取消息。

    RxStomp API 希望您将标头传递给 watch 方法,而不是回调。这就是为什么它专门告诉你:

    TS2345: Argument of type '(res: any) => void' is not assignable to parameter of type 'StompHeaders'.
    

    【讨论】:

    • 感谢您的回答。这对我帮助很大。最后一个问题:你有什么例子吗?
    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2011-11-12
    • 2014-10-29
    • 2011-07-17
    相关资源
    最近更新 更多