我的条件满足后,如何用Automatic Reconnect处理或销毁?无论我try 什么,该方法每次都会执行.

const connection = new signalR.HubConnectionBuilder()
      .withUrl(socketServerUrl, {
        withCredentials: false,
        skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets,
      })
      .withAutomaticReconnect({
        nextRetryDelayInMilliseconds: ({ elapsedMilliseconds }) => {
          // function content
        },
      })
      .configureLogging(signalR.LogLevel.Information)
      .build();

    if (isGameSessionExpired) {
      // Looking for something like that, but didn't work
      connection.stop();
      connection.withAutomaticReconnect(null);
      connection = null;
    }

推荐答案

重要的是要注意,一旦connection被声明为const,它就不能被重新分配到null.

我刚刚优化了代码,一切对我来说都很好,你可以试试.

let allowReconnect = true; // allow reconnect by default

const connection = new signalR.HubConnectionBuilder()
  .withUrl(socketServerUrl, {
    withCredentials: false,
    skipNegotiation: true,
    transport: signalR.HttpTransportType.WebSockets,
  })
  .withAutomaticReconnect({
    nextRetryDelayInMilliseconds: ({ elapsedMilliseconds }) => {
      if (!allowReconnect) {
        return null; // don't reconnect any more
      }
      // other logic here if you have
    },
  })
  .configureLogging(signalR.LogLevel.Information)
  .build();

if (isGameSessionExpired) {
  allowReconnect = false; // update the allowReconnect flag to prevent the reconnect behavior
  connection.stop(); // stop the connection
}

Javascript相关问答推荐

如何在NightWatch.js测试中允许浏览器权限?

微软Edge Select 间隙鼠标退出问题

过滤对象数组并动态将属性放入新数组

在页面上滚动 timeshift 动垂直滚动条

单击子元素时关闭父元素(JS)

为什么!逗号和空格不会作为输出返回,如果它在参数上?

在286之后恢复轮询

IF语句的计算结果与实际情况相反

在数组中查找重叠对象并仅返回那些重叠对象

VSCode中出现随机行

如何利用CSS中的隐藏元素实现平滑扩展和防止网格行间隙

如何找到带有特定文本和测试ID的div?

Cherrio JS返回父div的所有图像SRC

我怎样才能得到一个数组的名字在另一个数组?

为什么我的Navbar.css没有显示在本地主机页面上?

为什么我的SoupRequest";被重置为初始值,以及如何修复它?

如何在Reaction中设置缺省值, Select 下拉列表,动态追加剩余值?

material UI自动完成全宽

REACT-本机错误:错误类型错误:无法读取未定义的容器的属性

在执行console.log(new X())时记录一个字符串