传递给 JS 客户端的无法解释的参数 reconnectionStrategy

Unexplained arguments passed to JS client reconnectionStrategy

我正在研究 Diffusion JS API 的一些代码示例,但我不理解有关重新连接的示例。 reconnectionStrategy 的 startabort 参数是什么?

// Create a reconnection strategy that applies an exponential back-off
var reconnectionStrategy = (function() {
    return function(start, abort) {
        var wait = Math.min(Math.pow(2, attempts++) * 100, maximumAttemptInterval);

        // Wait and then try to start the reconnection attempt
        setTimeout(start, wait);
    };
})();

// Connect to the server.
diffusion.connect({
    host : 'diffusion.example.com',
    port : 443,
    secure : true,
    principal : 'control',
    credentials : 'password',
    reconnect : {
        timeout : maximumTimeoutDuration,
        strategy : reconnectionStrategy
    }
}).then(function(session) {

取自https://github.com/pushtechnology/diffusion-examples/blob/master/js/examples/reconnect.js

这两个参数描述为reconnectabortin the manual,都是可以使用的函数。

  • start/reconnect - 将启动重新连接尝试。指示客户端尝试另一个连接。

  • abort - 可能会被调用以中止重新连接,在这种情况下客户端将被关闭。如果您认为进一步的尝试将毫无结果或不受欢迎,请调用此方法。

通过这种理解,我们看到该示例尝试在等待之间重新连接,等待时间呈指数增长(100 毫秒、200 毫秒、400 毫秒等),最长可达 60 秒。如果重新连接尝试失败,则重新调用重新连接策略函数。

函数也不需要包装:

    var reconnectionStrategy = function(start, abort) {
    var wait = Math.min(Math.pow(2, attempts++) * 100, maximumAttemptInterval);

    // Wait and then try to start the reconnection attempt
    setTimeout(start, wait);};

效果一样好,而且不会那么混乱。