终于找到一个用 rxjs 的场景

查看 10|回复 0
作者:tcper   
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 次,比较省心。
您需要登录后才可以回帖 登录 | 立即注册

返回顶部