当页面未处于焦点时,Safari 由于空闲/不活动而断开 Web Socket 连接

Sli*_*nce 5 sockets safari websocket ios

我们的应用程序仅在 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;
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mys*_*yst 1

这是iOS 功能,可以保护用户免受代码耗尽电池的影响......

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

有一些针对此限制的黑客攻击,但事实是该限制对用户有利,不应该被规避。

请阅读链接中的技术说明以了解更多详细信息。