【问题标题】:Safari dropping Web Socket connection due to idle/inactivity when page not in focus当页面不在焦点时,Safari 由于空闲/不活动而丢弃 Web Socket 连接
【发布时间】:2024-01-22 06:27:01
【问题描述】:

我们的应用程序面临这个问题,仅在 Safari 浏览器中,尤其是在 iOS 设备上。

当前行为

不确定这是否是一个已知问题(我尝试搜索但一无所获)。如果页面/选项卡不在焦点上,则 Mac 版 Safari 似乎会因为不活动/空闲而默默地丢弃 Web 套接字连接。 最大的问题是,iOS X 在移动端非常持久。

重现步骤

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

预期行为 Websockets 应该通过心跳功能保持活跃。在其他浏览器中没有看到这种行为,所以不太可能是代码。

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

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

【问题讨论】:

  • 无论如何,您可能都需要处理损坏的 websocket。如果用户只是在浏览器打开的情况下挂起计算机并稍后恢复它,其他浏览器也会发生这种情况。

标签: ios sockets websocket safari


【解决方案1】:

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

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

这个限制有一些技巧,但事实是这个限制对用户有好处,不应该被规避。

阅读the technical note in the link了解更多详情。

【讨论】:

    最近更新 更多