gpt4 book ai didi

ios - 当页面不在焦点时,Safari 由于空闲/不活动而丢弃 Web Socket 连接

转载 作者:行者123 更新时间:2023-12-01 16:22:13 27 4
gpt4 key购买 nike

我们的应用程序面临这个问题,仅在 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;
}
}
}
}
}

最佳答案

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

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

围绕这个限制有一些黑客攻击,但事实是这个限制对用户有好处,不应该被规避。

阅读 the technical note in the link更多细节。

关于ios - 当页面不在焦点时,Safari 由于空闲/不活动而丢弃 Web Socket 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343792/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com