5

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

// 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

4

2 回答 2

4

该函数也不需要包装:

    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);};

会做的一样好,不会那么混乱。

于 2016-07-07T15:21:14.647 回答
4

这两个参数在手册reconnect中描述为和,都是可以使用的函数。abort

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

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

有了这种理解,我们看到该示例尝试在以指数方式(100 毫秒、200 毫秒、400 毫秒等)增加最多 60 秒的等待之间重新连接。如果重新连接尝试失败,则再次调用重新连接策略函数。

于 2016-07-07T14:50:53.977 回答