插件push.uniapp.js 断线重连问题

Doogeli

问题描述

uniapp vue2 APP端push.uniapp.js经常会有断线的情况,看了下push.uniapp.js里面的源码是有心跳机制,也有断线重连的。但不太懂,我在APP.VUE加上的,收消息是正常的,但是会有一种情况,把APP弹到后台后,我大概5分钟以上,再重新弹起来原来的APP页面,这个时候websocket就会断掉,接收不到消息,我试着用在app.vue 里面的onShow写检测,

if (this.$connection.state === 'disconnected') {
                // 显示加载中
                this.$tip.loading = true;
                // 重连
                this.$connection.connect();
                setTimeout(() => {
                      if (this.$connection.state === 'connected') {
                        // 连接成功,关闭加载提示框
                        this.$tip.loading = false;
                      }
                }, 3000);  // 延迟 5 秒后检查连接状态
              }

我是这样写的,写的弹回的时候,检测连接是否关闭,关闭就重新连接。但是没有效果?
看到源码底下又有一个类似的连接,不大清楚究竟是哪个?JS比较菜,望大佬指点下

function Connection(options) {
    this.dispatcher = new Dispatcher();
    __extends(this, this.dispatcher);
    var properies = ['on', 'off', 'emit'];
    for (var i in properies) {
        this[properies[i]] = this.dispatcher[properies[i]];
    }
    this.options = options;
    this.state = 'initialized'; //initialized connecting connected disconnected
    this.doNotConnect = 0;
    this.reconnectInterval = 1;
    this.connection = null;
    this.reconnectTimer = 0;
    this.connect();
}

Connection.prototype.updateNetworkState = function(state){
    var old_state = this.state;
    this.state = state;
    if (old_state !== state) {
        this.emit('state_change', { previous: old_state, current: state });
    }
};

Connection.prototype.connect = function () {
    this.doNotConnect = 0;
    if (this.networkState == 'connecting' || this.networkState == 'established') {
        console.log('networkState is ' + this.networkState + ' and do not need connect');
        return;
    }
    if (this.reconnectTimer) {
        clearTimeout(this.reconnectTimer);
        this.reconnectTimer = 0;
    }

    this.closeAndClean();

    var options = this.options;
    var _this = this;
    _this.updateNetworkState('connecting');
    var cb = function(){
        uni.onSocketOpen(function (res) {
            _this.reconnectInterval = 1;
            if (_this.doNotConnect) {
                _this.updateNetworkState('closing');
                uni.closeSocket();
                return;
            }
            _this.updateNetworkState('established');
            if (options.onOpen) {
                options.onOpen(res);
            }
        });

        if (options.onMessage) {
            uni.onSocketMessage(options.onMessage);
        }

        uni.onSocketClose(function (res) {
            _this.updateNetworkState('disconnected');
            if (!_this.doNotConnect) {
                _this.waitReconnect();
            }
            if (options.onClose) {
                options.onClose(res);
            }
        });

        uni.onSocketError(function (res) {
            _this.close();
            if (!_this.doNotConnect) {
                _this.waitReconnect();
            }
            if (options.onError) {
                options.onError(res);
            }
        });
    };
    uni.connectSocket({
        url: options.url,
        fail: function (res) {
            console.log('uni.connectSocket fail');
            console.log(res);
            _this.updateNetworkState('disconnected');
            _this.waitReconnect();
        },
        success: function() {

        }
    });
    cb();
}

Connection.prototype.connect = function () {
    this.doNotConnect = 0;
    if (this.state === 'connected') {
        console.log('networkState is "' + this.state + '" and do not need connect');
        return;
    }
    if (this.reconnectTimer) {
        clearTimeout(this.reconnectTimer);
        this.reconnectTimer = 0;
    }

    this.closeAndClean();

    var options = this.options;

    this.updateNetworkState('connecting');

    var _this = this;
    var cb = function(){
        uni.onSocketOpen(function (res) {
            _this.reconnectInterval = 1;
            if (_this.doNotConnect) {
                _this.updateNetworkState('disconnected');
                uni.closeSocket();
                return;
            }
            if (options.onOpen) {
                options.onOpen(res);
            }
        });

        if (options.onMessage) {
            uni.onSocketMessage(options.onMessage);
        }

        uni.onSocketClose(function (res) {
            _this.updateNetworkState('disconnected');
            if (!_this.doNotConnect) {
                _this.waitReconnect();
            }
            if (options.onClose) {
                options.onClose(res);
            }
        });

        uni.onSocketError(function (res) {
            _this.close();
            if (!_this.doNotConnect) {
                _this.waitReconnect();
            }
            if (options.onError) {
                options.onError(res);
            }
        });
    };
    uni.connectSocket({
        url: options.url+'/app/'+options.app_key,
        fail: function (res) {
            console.log('uni.connectSocket fail');
            console.log(res);
            _this.updateNetworkState('disconnected');
            _this.waitReconnect();
        },
        success: function() {

        }
    });
    cb();
}
527 1 0
1个回答

walkor
🔝