SignalR IReconnectPolicy示例

问题描述

我正在开发使用SignalR的.net核心应用。

在javascript客户端中,有一个HubConnectionBuilder。

其中一个选项是“ withAutomaticaReconnect()”

此方法的第二次重载采用“ IReconnectPolicy”,其定义如下:

export interface IReconnectPolicy {
/** Called after the transport loses the connection.
 *
 * @param {number} previousRetryCount The number of consecutive failed reconnect attempts so far.
 *
 * @param {number} elapsedMilliseconds The amount of time in milliseconds spent reconnecting so far.
 *
 * @returns {number | null} The amount of time in milliseconds to wait before the next reconnect attempt. `null` tells the client to stop retrying and close.
 */
nextRetryDelayInMilliseconds(previousRetryCount: number,elapsedMilliseconds: number): number | null;

}

但是我不明白。连接时应该如何使用?我是否应该创建一个具有这些属性的对象,然后将该对象传递给它?

还是回调,回调将接收那些参数?

我问的原因是失去与集线器的连接有问题,我想确保它配置牢固。

这是我目前使用此代码的方式:

 async connect() {
    if (!this.connection) {
        this.connection = new signalR.HubConnectionBuilder()
            .withUrl("/csractivityhub",{ accessTokenFactory: () => this.authService.getAccessToken() })
            .withAutomaticReconnect()
            .configureLogging(signalR.LogLevel.Information)
            .build();
        this.connection.on("onSubscribed",this.onSubscribed.bind(this));
        this.connection.on("onPublished",this.onPublished.bind(this));
        this.connection.on("onCsrActivity",this.onCsrActivity.bind(this));
        await this.connection.start().catch(err => console.error(err.toString()));
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)