Wangkai8787(讨论 | 贡献) 小无编辑摘要 |
Wangkai8787(讨论 | 贡献) (服务器状态改读雨城通v2接口;修正JSON解码错误误报下线;15秒轮询与未知状态提示(需雨城通接口CORS配套部署)) |
||
| 第1行: | 第1行: | ||
$(function() { | $(function () { | ||
var apiUrl = 'https://yct.shangxiaoguan.top/api/operations/server-status'; | |||
var inFlight = false; | |||
var lastKnown = null; | |||
function showUnknown() { | |||
$('#online-ver').text(''); | |||
if (lastKnown) { | |||
$('#online-player').text('状态未知'); | |||
$('#online-desc').text('本次查询失败;上次确认' + lastKnown + '。正在重试。'); | |||
} else { | |||
$('#online-player').text('状态未知'); | |||
$('#online-desc').text('暂时无法查询服务器状态,稍后自动重试。'); | |||
} | |||
} | |||
function getServerStatus() { | |||
if (inFlight || document.hidden) return; | |||
inFlight = true; | |||
$.ajax({ url: apiUrl, dataType: 'json', cache: false, timeout: 12000 }) | |||
.done(function (data) { | |||
var item = data && data.item; | |||
if (!item || (item.availability !== 'online' && item.availability !== 'offline')) { | |||
showUnknown(); | |||
return; | |||
} | } | ||
var checked = new Date(item.checkedAt); | |||
if (!Number.isFinite(checked.getTime()) || Math.abs(Date.now() - checked.getTime()) > 120000) { | |||
showUnknown(); | |||
return; | |||
} | |||
var time = checked.toLocaleTimeString('zh-CN', { hour12: false }); | |||
$('#online-ver').text(''); | |||
if (item.availability === 'online') { | |||
var count = item.onlinePlayerCount; | |||
$('#online-player').text(Number.isInteger(count) && count >= 0 ? count + ' 人在线' : '服务器在线'); | |||
var latency = typeof item.latencyMs === 'number' && Number.isFinite(item.latencyMs) && item.latencyMs >= 0 | |||
? ',查询延迟 ' + Math.round(item.latencyMs) + ' 毫秒' : ''; | |||
$('#online-desc').text('服务器在线' + latency + '。核对时间 ' + time + '。'); | |||
lastKnown = '在线(' + time + ')'; | |||
} else { | |||
$('#online-player').text('服务暂不可用'); | |||
$('#online-desc').text('雨城通暂时无法连接服务器数据源。核对时间 ' + time + '。'); | |||
lastKnown = '数据源暂不可用(' + time + ')'; | |||
} | |||
}) | |||
.fail(showUnknown) | |||
.always(function () { inFlight = false; }); | |||
} | |||
getServerStatus(); | |||
window.setInterval(getServerStatus, 15000); | |||
document.addEventListener('visibilitychange', function () { | |||
if (!document.hidden) getServerStatus(); | |||
}); | |||
}); | |||
2026年10月29日 (四) 10:15的版本
$(function () {
var apiUrl = 'https://yct.shangxiaoguan.top/api/operations/server-status';
var inFlight = false;
var lastKnown = null;
function showUnknown() {
$('#online-ver').text('');
if (lastKnown) {
$('#online-player').text('状态未知');
$('#online-desc').text('本次查询失败;上次确认' + lastKnown + '。正在重试。');
} else {
$('#online-player').text('状态未知');
$('#online-desc').text('暂时无法查询服务器状态,稍后自动重试。');
}
}
function getServerStatus() {
if (inFlight || document.hidden) return;
inFlight = true;
$.ajax({ url: apiUrl, dataType: 'json', cache: false, timeout: 12000 })
.done(function (data) {
var item = data && data.item;
if (!item || (item.availability !== 'online' && item.availability !== 'offline')) {
showUnknown();
return;
}
var checked = new Date(item.checkedAt);
if (!Number.isFinite(checked.getTime()) || Math.abs(Date.now() - checked.getTime()) > 120000) {
showUnknown();
return;
}
var time = checked.toLocaleTimeString('zh-CN', { hour12: false });
$('#online-ver').text('');
if (item.availability === 'online') {
var count = item.onlinePlayerCount;
$('#online-player').text(Number.isInteger(count) && count >= 0 ? count + ' 人在线' : '服务器在线');
var latency = typeof item.latencyMs === 'number' && Number.isFinite(item.latencyMs) && item.latencyMs >= 0
? ',查询延迟 ' + Math.round(item.latencyMs) + ' 毫秒' : '';
$('#online-desc').text('服务器在线' + latency + '。核对时间 ' + time + '。');
lastKnown = '在线(' + time + ')';
} else {
$('#online-player').text('服务暂不可用');
$('#online-desc').text('雨城通暂时无法连接服务器数据源。核对时间 ' + time + '。');
lastKnown = '数据源暂不可用(' + time + ')';
}
})
.fail(showUnknown)
.always(function () { inFlight = false; });
}
getServerStatus();
window.setInterval(getServerStatus, 15000);
document.addEventListener('visibilitychange', function () {
if (!document.hidden) getServerStatus();
});
});