杀死页面 和切换页面 释放控制权限
This commit is contained in:
@@ -3,6 +3,7 @@ import { wsManager } from '../WebSocketManager.ts';
|
||||
import {
|
||||
getControlRight,
|
||||
getJurisdiction,
|
||||
releaseControl
|
||||
} from '../../api/device.ts';
|
||||
|
||||
import envConfig from '../../../env';
|
||||
@@ -17,7 +18,7 @@ export default class DeviceController {
|
||||
envConfig.WS_URL || 'wss://ri.satabot.com/ws/';
|
||||
|
||||
this.connectedDeviceSn = null;
|
||||
this.currentDevice = null; // 保存当前设备
|
||||
this.currentDevice = null;
|
||||
|
||||
this.requestDeviceId = '';
|
||||
|
||||
@@ -30,7 +31,7 @@ export default class DeviceController {
|
||||
this.locationInfo = {};
|
||||
|
||||
this.controlTimeout = null;
|
||||
this.keepAliveInterval = null; // 后台保活定时器
|
||||
this.keepAliveInterval = null;
|
||||
|
||||
this.onDeviceInfoUpdate = null;
|
||||
this.onLocationInfoUpdate = null;
|
||||
@@ -46,6 +47,7 @@ export default class DeviceController {
|
||||
this.handleWsMessage = this.handleWsMessage.bind(this);
|
||||
this.handleWsStatus = this.handleWsStatus.bind(this);
|
||||
this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
|
||||
this.handleBeforeUnload = this.handleBeforeUnload.bind(this);
|
||||
|
||||
this.sendGamepad = throttle(
|
||||
this.sendGamepad.bind(this),
|
||||
@@ -58,36 +60,34 @@ export default class DeviceController {
|
||||
this.unSubMessage = wsManager.onMessage(this.handleWsMessage);
|
||||
this.unSubStatus = wsManager.onStatusChange(this.handleWsStatus);
|
||||
|
||||
// ========== 【集成】页面可见性监听 ==========
|
||||
document.addEventListener('visibilitychange', this.handleVisibilityChange);
|
||||
|
||||
// ========== 【集成】后台保活定时 ==========
|
||||
//this.startKeepAlive();
|
||||
document.addEventListener('beforeunload', this.handleBeforeUnload);
|
||||
}
|
||||
|
||||
// ========== 【集成】页面切前台重连 ==========
|
||||
handleVisibilityChange() {
|
||||
// 切后台:关闭 WS
|
||||
if (document.visibilityState === 'hidden') {
|
||||
console.log('🔌 页面切后台,关闭 WebSocket');
|
||||
//this.release();
|
||||
console.log('?? 页<>面进入后台,释放控制权限');
|
||||
|
||||
wsManager.close();
|
||||
this.connectedDeviceSn = null;
|
||||
|
||||
|
||||
this.isControlled = false;
|
||||
this.onControlStatusChange?.(false);
|
||||
}
|
||||
if (document.visibilityState === 'visible' && this.currentDevice) {
|
||||
if (!this.currentDevice?.serialNumber) return;
|
||||
console.log('✅ 页面切回前台,自动重连 WebSocket');
|
||||
console.log('? 页面切回前台,自动重连 WebSocket');
|
||||
setTimeout(() => {
|
||||
this.connectDevice(this.currentDevice);
|
||||
}, 500);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 【集成】后台保活 ==========
|
||||
handleBeforeUnload() {
|
||||
console.log('?? 浏览器即将关闭/刷新,释放控制权限');
|
||||
this.release();
|
||||
}
|
||||
|
||||
startKeepAlive() {
|
||||
this.keepAliveInterval = setInterval(() => {
|
||||
if (
|
||||
@@ -95,47 +95,54 @@ export default class DeviceController {
|
||||
this.wsStatus !== 'connected' &&
|
||||
this.currentDevice?.serialNumber
|
||||
) {
|
||||
console.log('🔄 后台保活:尝试重连 WebSocket');
|
||||
console.log('?? 后台保活:重新连接 WebSocket');
|
||||
this.connectDevice(this.currentDevice);
|
||||
}
|
||||
}, 8000);
|
||||
}
|
||||
release() {
|
||||
if (this.isControlled && this.connectedDeviceSn) {
|
||||
const data = {
|
||||
platform: "web",
|
||||
type: 1
|
||||
};
|
||||
const url = `${envConfig.baseURL}/forward/device/releaseControl`;
|
||||
|
||||
try {
|
||||
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
|
||||
navigator.sendBeacon(url, blob);
|
||||
} catch (e) {
|
||||
console.error('Failed to send releaseControl beacon:', e);
|
||||
}
|
||||
|
||||
async release() {
|
||||
console.log('?? 释放控制权限');
|
||||
//if (this.isControlled && this.connectedDeviceSn) {
|
||||
const data = {
|
||||
platform: 'web',
|
||||
type: 1,
|
||||
deviceId: this.connectedDeviceSn,
|
||||
};
|
||||
const url = `${envConfig.baseURL}/forward/device/releaseControl`;
|
||||
console.log('?? 释放控制权限1111');
|
||||
|
||||
try {
|
||||
await releaseControl(data);
|
||||
console.log('?? 释放控制权限1111222');
|
||||
|
||||
const blob = new Blob([JSON.stringify(data)], { type: 'application/json' });
|
||||
navigator.sendBeacon(url, blob);
|
||||
console.log('✅ 已发送 releaseControl 请求');
|
||||
} catch (e) {
|
||||
console.error('Failed to send releaseControl beacon:', e);
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.clearControlTimeout();
|
||||
|
||||
// 清理保活
|
||||
if (this.keepAliveInterval) {
|
||||
clearInterval(this.keepAliveInterval);
|
||||
this.keepAliveInterval = null;
|
||||
}
|
||||
|
||||
// 移除监听
|
||||
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
|
||||
document.removeEventListener('beforeunload', this.handleBeforeUnload);
|
||||
|
||||
this.release();
|
||||
|
||||
this.unSubMessage?.();
|
||||
this.unSubStatus?.();
|
||||
//this.release();
|
||||
|
||||
//wsManager.close();
|
||||
this.connectedDeviceSn = null;
|
||||
|
||||
}
|
||||
|
||||
handleWsStatus(status) {
|
||||
@@ -151,7 +158,6 @@ export default class DeviceController {
|
||||
async connectDevice(device) {
|
||||
if (!device) return;
|
||||
|
||||
// 保存当前设备,用于保活/重连
|
||||
this.currentDevice = device;
|
||||
if (!device.serialNumber) return;
|
||||
|
||||
@@ -166,25 +172,9 @@ export default class DeviceController {
|
||||
wsManager.close();
|
||||
|
||||
try {
|
||||
//const res = await getControlRight({
|
||||
// userName: this.userInfo.username,
|
||||
// sourceType: 1,
|
||||
// token: this.userInfo.token,
|
||||
//});
|
||||
|
||||
//this.hasControlRight = res?.code === 200 && res.data == true;
|
||||
//this.onHasControlRightChange?.(this.hasControlRight);
|
||||
|
||||
wsManager.connect(sn, this.wsUrl);
|
||||
this.connectedDeviceSn = sn;
|
||||
|
||||
//if (!this.hasControlRight) {
|
||||
// this.messageApi.warning(i18n.t('devices.fixed2OthersControlFixed2'));
|
||||
// return;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
await this.getControl(sn);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
@@ -256,7 +246,7 @@ export default class DeviceController {
|
||||
}
|
||||
|
||||
handleWsMessage(msg) {
|
||||
console.log('📨 收到消息:', msg);
|
||||
console.log('?? 收到消息:', msg);
|
||||
switch (msg.type) {
|
||||
case 'deviceInfo': {
|
||||
const data = {};
|
||||
@@ -304,19 +294,17 @@ export default class DeviceController {
|
||||
case "device_task_change": {
|
||||
if (msg.deviceId != this.connectedDeviceSn) return;
|
||||
this.onIsFinished?.(true);
|
||||
console.log('🔌 控制结果任务变更,', msg);
|
||||
console.log('?? 任务结束收到,', msg);
|
||||
|
||||
}
|
||||
//完成点
|
||||
case "device_task_arrive_point": {
|
||||
if (msg.deviceId != this.connectedDeviceSn) return;
|
||||
|
||||
this.handleArrivedPoint?.(msg.entity);
|
||||
console.log('🔌 到达完成点,', msg);
|
||||
console.log('?? 收到抵达,', msg);
|
||||
|
||||
}
|
||||
|
||||
|
||||
case 'switchPermission': {
|
||||
const platform = (msg.platform || '').toLowerCase();
|
||||
if (!platform || platform === 'null') return;
|
||||
|
||||
Reference in New Issue
Block a user