当页面不清晰时,由于空闲/不活动,Safari断开了Web套接字连接

时间:2018-11-16 18:52:55

标签: ios sockets websocket safari

我们只能在野生动物园浏览器中,尤其是在iOS设备上,使用我们的应用程序来面对这个问题。

当前行为

不确定这是否是已知问题(我尝试搜索但未发现任何问题)。如果页面/选项卡不清晰,则由于不活动/闲置,Mac版Safari似乎正在默默地断开Web套接字连接。 最大的问题是在移动iOS X中是非常持久的。

复制步骤

打开Safari>网站加载>将Safari置于空闲状态,然后打开任何应用程序或锁定设备。 唤醒后,Safari正在关闭连接,并且不再显示数据,我们在请求数据的模块中进行了无限加载。

预期行为 Websocket应该通过心跳功能保持活动状态。在其他浏览器中看不到这种行为,因此不太可能是代码。

这可能是某种省电功能会覆盖/忽略心跳吗?

import 'whatwg-fetch';
import Config from "../config/main";
import WS from "./websocket";
import Helpers from "./helperFunctions";

var Zergling = (function (WS, Config) {
    'use strict';

    var Zergling = {};

    var subscriptions = {}, useWebSocket = false, sessionRequestIsInProgress = false, loginInProgress = false,
        uiLogggedIn = false, // uiLogggedIn is the login state displayed in UI (sometimes it differs from real one, see delayedLogoutIfNotRestored func)
        authData, session, connectionAvailable, isLoggedIn, longPollUrl;

    Zergling.loginStates = {
        LOGGED_OUT: 0,
        LOGGED_IN: 1,
        IN_PROGRESS: 2
    };

    Zergling.codes = { // Swarm response codes
        OK: 0,
        SESSION_LOST: 5,
        NEED_TO_LOGIN: 12
    };

    function getLanguageCode (lng) {
        if (Config.swarm.languageMap && Config.swarm.languageMap[lng]) {
            return Config.swarm.languageMap[lng];
        }
        return lng;
    }

    //helper func for fetch
    function checkStatus (response) {
        if (response.status >= 200 && response.status < 300) {
            return response;
        } else {
            var error = new Error(response.statusText);
            error.response = response;
            throw error;
        }
    }

    //helper func for fetch
    function parseJSON (response) {
        return response.json();
    }

    /**
     * @description returns randomly selected(taking weight into consideration) long poll url
     * @returns {String} long polling URL
     */
    function getLongPollUrl () {
        if (!longPollUrl) {
            longPollUrl = Helpers.getWeightedRandom(Config.swarm.url).url;
            console.debug('long Polling URL selected:', longPollUrl);
        }
        return longPollUrl;
    }

    /**
     * @description
     * Applies the diff on object
     * properties having null values in diff are removed from  object, others' values are replaced.
     *
     * Also checks the 'price' field for changes and adds new field 'price_change' as sibling
     * which indicates the change direction (1 - up, -1 down, null - unchanged)
     *
     * @param {Object} current current object
     * @param {Object} diff    received diff
     */
    function destructivelyUpdateObject (current, diff) {
        if (current === undefined || !(current instanceof Object)) {
            throw new Error('wrong call');
        }

        for (var key in diff) {
            if (!diff.hasOwnProperty(key)) continue;
            var val = diff[key];
            if (val === null) {
                delete current[key];
            } else if (typeof val !== 'object') {
                current[key] = val;
            } else { // diff[key] is Object
                if (typeof current[key] !== 'object' || current[key] === null) {
                    current[key] = val;
                } else {
                    var hasPrice = (current[key].price !== undefined);
                    var oldPrice;
                    if (hasPrice) {
                        oldPrice = current[key].price;
                    }
                    destructivelyUpdateObject(current[key], val);
                    if (hasPrice) {
                        current[key].price_change = (val.price === oldPrice) ? null : (oldPrice < val.price) * 2 - 1;
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是and iOS feature that protects users against code draining their battery ...

应使用iOS的推送通知系统执行后台应用程序的推送通知,而不要保持打开的连接处于活动状态。

围绕此限制有很多技巧,但事实是该限制对用户有利,不应绕开。

阅读the technical note in the link了解更多详细信息。