控制逻辑加入后台保活 以防哪个浏览器且后台websocket断联
This commit is contained in:
@@ -16,6 +16,7 @@ export default class DeviceController {
|
||||
envConfig.WS_URL || 'wss://ri.satabot.com/ws/';
|
||||
|
||||
this.connectedDeviceSn = null;
|
||||
this.currentDevice = null; // 保存当前设备
|
||||
|
||||
this.requestDeviceId = '';
|
||||
|
||||
@@ -26,8 +27,8 @@ export default class DeviceController {
|
||||
this.wsStatus = 'disconnected';
|
||||
this.wsDeviceInfo = {};
|
||||
|
||||
|
||||
this.controlTimeout = null;
|
||||
this.keepAliveInterval = null; // 后台保活定时器
|
||||
|
||||
this.onDeviceInfoUpdate = null;
|
||||
this.onControlStatusChange = null;
|
||||
@@ -38,30 +39,62 @@ export default class DeviceController {
|
||||
|
||||
this.handleWsMessage = this.handleWsMessage.bind(this);
|
||||
this.handleWsStatus = this.handleWsStatus.bind(this);
|
||||
this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
|
||||
|
||||
this.sendGamepad = throttle(
|
||||
this.sendGamepad.bind(this),
|
||||
50,
|
||||
{
|
||||
leading: true,
|
||||
trailing: true,
|
||||
},
|
||||
{ leading: true, trailing: true }
|
||||
);
|
||||
}
|
||||
|
||||
init() {
|
||||
this.unSubMessage = wsManager.onMessage(
|
||||
this.handleWsMessage,
|
||||
);
|
||||
this.unSubMessage = wsManager.onMessage(this.handleWsMessage);
|
||||
this.unSubStatus = wsManager.onStatusChange(this.handleWsStatus);
|
||||
|
||||
this.unSubStatus = wsManager.onStatusChange(
|
||||
this.handleWsStatus,
|
||||
);
|
||||
// ========== 【集成】页面可见性监听 ==========
|
||||
document.addEventListener('visibilitychange', this.handleVisibilityChange);
|
||||
|
||||
// ========== 【集成】后台保活定时 ==========
|
||||
this.startKeepAlive();
|
||||
}
|
||||
|
||||
// ========== 【集成】页面切前台重连 ==========
|
||||
handleVisibilityChange() {
|
||||
if (document.visibilityState === 'visible' && this.currentDevice) {
|
||||
console.log('✅ 页面切回前台,自动重连 WebSocket');
|
||||
setTimeout(() => {
|
||||
this.connectDevice(this.currentDevice);
|
||||
}, 500);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 【集成】后台保活 ==========
|
||||
startKeepAlive() {
|
||||
this.keepAliveInterval = setInterval(() => {
|
||||
if (
|
||||
this.connectedDeviceSn &&
|
||||
this.wsStatus !== 'connected' &&
|
||||
this.currentDevice
|
||||
) {
|
||||
console.log('🔄 后台保活:尝试重连 WebSocket');
|
||||
this.connectDevice(this.currentDevice);
|
||||
}
|
||||
}, 8000);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.clearControlTimeout();
|
||||
|
||||
// 清理保活
|
||||
if (this.keepAliveInterval) {
|
||||
clearInterval(this.keepAliveInterval);
|
||||
this.keepAliveInterval = null;
|
||||
}
|
||||
|
||||
// 移除监听
|
||||
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
|
||||
|
||||
this.unSubMessage?.();
|
||||
this.unSubStatus?.();
|
||||
|
||||
@@ -70,27 +103,25 @@ export default class DeviceController {
|
||||
|
||||
handleWsStatus(status) {
|
||||
this.wsStatus = status;
|
||||
|
||||
this.onWsStatusChange?.(status);
|
||||
|
||||
if (status === 'disconnected') {
|
||||
this.isControlled = false;
|
||||
|
||||
this.onControlStatusChange?.(false);
|
||||
}
|
||||
}
|
||||
|
||||
async connectDevice(device) {
|
||||
if (!device) return;
|
||||
const sn =
|
||||
device.serialNumber || device.device_sn;
|
||||
|
||||
// 保存当前设备,用于保活/重连
|
||||
this.currentDevice = device;
|
||||
|
||||
const sn = device.serialNumber || device.device_sn;
|
||||
|
||||
this.connectedDeviceSn = null;
|
||||
|
||||
this.wsDeviceInfo = {};
|
||||
|
||||
this.isControlled = false;
|
||||
|
||||
this.onControlStatusChange?.(false);
|
||||
|
||||
wsManager.close();
|
||||
@@ -102,12 +133,8 @@ export default class DeviceController {
|
||||
token: this.userInfo.token,
|
||||
});
|
||||
|
||||
|
||||
this.hasControlRight = res?.code === 200;
|
||||
|
||||
this.onHasControlRightChange?.(
|
||||
this.hasControlRight,
|
||||
);
|
||||
this.onHasControlRightChange?.(this.hasControlRight);
|
||||
|
||||
if (!this.hasControlRight) {
|
||||
this.messageApi.warning('其他端正在控制');
|
||||
@@ -115,13 +142,11 @@ export default class DeviceController {
|
||||
}
|
||||
|
||||
wsManager.connect(sn, this.wsUrl);
|
||||
|
||||
this.connectedDeviceSn = sn;
|
||||
|
||||
await this.getControl(sn);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
this.messageApi.error('权限校验失败');
|
||||
}
|
||||
}
|
||||
@@ -133,35 +158,23 @@ export default class DeviceController {
|
||||
deviceId: serialnum,
|
||||
});
|
||||
|
||||
|
||||
this.isControlled =
|
||||
res?.code === 200 &&
|
||||
res?.data?.remoteControl === true;
|
||||
|
||||
this.onControlStatusChange?.(
|
||||
this.isControlled,
|
||||
);
|
||||
this.isControlled = res?.code === 200 && res?.data?.remoteControl === true;
|
||||
this.onControlStatusChange?.(this.isControlled);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
this.isControlled = false;
|
||||
|
||||
this.onControlStatusChange?.(false);
|
||||
}
|
||||
}
|
||||
|
||||
requestControl() {
|
||||
if (this.isRequestControl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.isRequestControl) return;
|
||||
if (!this.hasControlRight) {
|
||||
this.messageApi.warning('无控制权限');
|
||||
return;
|
||||
}
|
||||
|
||||
this.isRequestControl = true;
|
||||
|
||||
this.onRequestControlStatusChange?.(true);
|
||||
|
||||
this.messageApi.loading({
|
||||
@@ -170,22 +183,14 @@ export default class DeviceController {
|
||||
duration: 0,
|
||||
});
|
||||
|
||||
this.sendPermissionRequest(
|
||||
true,
|
||||
'switchPermission',
|
||||
);
|
||||
|
||||
this.sendPermissionRequest(true, 'switchPermission');
|
||||
this.clearControlTimeout();
|
||||
|
||||
this.controlTimeout = setTimeout(() => {
|
||||
if (!this.isRequestControl) return;
|
||||
|
||||
this.isRequestControl = false;
|
||||
|
||||
this.onRequestControlStatusChange?.(false);
|
||||
|
||||
this.messageApi.destroy('control');
|
||||
|
||||
this.messageApi.error('控制请求超时');
|
||||
}, 10000);
|
||||
}
|
||||
@@ -202,10 +207,7 @@ export default class DeviceController {
|
||||
type,
|
||||
switchResult: data,
|
||||
token: this.userInfo.token,
|
||||
deviceId:
|
||||
type === 'switchPermission'
|
||||
? this.connectedDeviceSn
|
||||
: this.requestDeviceId,
|
||||
deviceId: type === 'switchPermission' ? this.connectedDeviceSn : this.requestDeviceId,
|
||||
userName: this.userInfo.username,
|
||||
});
|
||||
}
|
||||
@@ -214,15 +216,11 @@ export default class DeviceController {
|
||||
switch (msg.type) {
|
||||
case 'deviceInfo': {
|
||||
const data = {};
|
||||
|
||||
msg.data.forEach((item) => {
|
||||
data[item.name] = item.value;
|
||||
});
|
||||
|
||||
this.wsDeviceInfo = data;
|
||||
|
||||
this.onDeviceInfoUpdate?.(data);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -230,64 +228,36 @@ export default class DeviceController {
|
||||
if (msg.isAuth) {
|
||||
this.getControl(this.connectedDeviceSn);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'switchResult': {
|
||||
console.log(msg, "msg2222222222")
|
||||
|
||||
this.clearControlTimeout();
|
||||
|
||||
this.isRequestControl = false;
|
||||
|
||||
this.onRequestControlStatusChange?.(false);
|
||||
|
||||
if (
|
||||
msg.success === false ||
|
||||
msg.switchResult === false
|
||||
) {
|
||||
if (msg.success === false || msg.switchResult === false) {
|
||||
this.isControlled = false;
|
||||
|
||||
this.onControlStatusChange?.(false);
|
||||
|
||||
this.messageApi.error({
|
||||
key: 'control',
|
||||
content: msg.message || '控制失败',
|
||||
});
|
||||
|
||||
this.messageApi.error({ key: 'control', content: msg.message || '控制失败' });
|
||||
return;
|
||||
}
|
||||
|
||||
this.isControlled = true;
|
||||
|
||||
this.onControlStatusChange?.(true);
|
||||
|
||||
this.messageApi.success({
|
||||
key: 'control',
|
||||
content: '控制成功',
|
||||
});
|
||||
|
||||
this.messageApi.success({ key: 'control', content: '控制成功' });
|
||||
break;
|
||||
}
|
||||
|
||||
case 'switchPermission': {
|
||||
console.log(msg, "msg111111111111111")
|
||||
const platform = (
|
||||
msg.platform || ''
|
||||
).toLowerCase();
|
||||
|
||||
if (!platform || platform === 'web' || platform == "null") {
|
||||
return;
|
||||
}
|
||||
const platform = (msg.platform || '').toLowerCase();
|
||||
if (!platform || platform === 'web' || platform === 'null') return;
|
||||
|
||||
this.requestDeviceId = msg.deviceId;
|
||||
|
||||
this.onPermissionRequest?.({
|
||||
title: '权限请求',
|
||||
content: `${msg.platform} 请求控制设备`,
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -311,20 +281,12 @@ export default class DeviceController {
|
||||
}
|
||||
|
||||
sendGamepad(state) {
|
||||
if (
|
||||
!this.isControlled ||
|
||||
!this.connectedDeviceSn
|
||||
) {
|
||||
return;
|
||||
}
|
||||
console.log(state, "state")
|
||||
|
||||
if (!this.isControlled || !this.connectedDeviceSn) return;
|
||||
wsManager.send({
|
||||
type: 'remoteControl',
|
||||
remoteData: state,
|
||||
token: this.userInfo.token,
|
||||
userName: this.userInfo.username,
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user