tcper

终于找到一个用 rxjs 的场景

  •  1
     
  •   tcper · Sep 22, 2024 · 2056 views
    This topic created in 627 days ago, the information mentioned may be changed or developed.

    websocket ,如果直接写的话,各种onmessage onopen处理代码写的比较恶心

    另外,写个onclose onerror重连,里面清理一下状态,那代码就无可避免很多上下文变量

    而用 rxjs 就比较省心

    const subject = webSocket('ws://localhost:8081');
    
    subject.pipe(
      retry(10)
    ).subscribe({
      next: msg => console.log('message received: ' + msg), // Called whenever there is a message from the server.
      error: err => {
        console.log("err", err)
      }, 
      complete: () => console.log('complete') // Called when connection is closed (for whatever reason).
     });
    }
    

    断了重连 10 次,比较省心。

    3 replies    2024-10-23 11:07:08 +08:00
    NerdHND
        1
    NerdHND  
       Oct 11, 2024
    前几天还在沉迷 rxjs, 但是尝试几天发现真的可以用的地方好少, 大部分情况没这么复杂的请求. 以后做 ws webrtc 看看不知道有没有场景.
    tcper
        2
    tcper  
    OP
       Oct 12, 2024
    @NerdHND ws, sse 都比较适合。
    别的场景要看你对 rxjs 的熟练程度,不熟练是不会想到用 rxjs 。
    另外 rxjs 的异步怎么和 react/vue 变量的同步,和状态管理的同步也是个问题。
    minglanyu
        3
    minglanyu  
       Oct 23, 2024
    rxjs 的场景确实需要慢慢挖掘
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   927 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 20:42 · PVG 04:42 · LAX 13:42 · JFK 16:42
    ♥ Do have faith in what you're doing.