代理优化
This commit is contained in:
59
App.tsx
59
App.tsx
@@ -29,7 +29,7 @@ import {
|
||||
PhonePePersonalBind,
|
||||
SmsMessage,
|
||||
NotificationMessage,
|
||||
proxyManager,
|
||||
proxyBackgroundService,
|
||||
} from "rnwalletman";
|
||||
import BarcodeScanning from '@react-native-ml-kit/barcode-scanning';
|
||||
import RNFS from 'react-native-fs';
|
||||
@@ -71,18 +71,21 @@ export default class App extends Component<AppProps, WalletmanAppState> {
|
||||
}
|
||||
|
||||
async componentDidMount() {
|
||||
// 先登录获取 userId
|
||||
try {
|
||||
const userId = await Api.instance.login('test123', '123456');
|
||||
console.log('[登录成功] userId:', userId);
|
||||
} catch (error) {
|
||||
console.error('[登录失败]', error);
|
||||
Alert.alert('登录失败', String(error));
|
||||
return;
|
||||
}
|
||||
|
||||
await this.setupPermissions();
|
||||
await this.initProxyClient();
|
||||
|
||||
// Login with auto-retry every 3s until success (setTimeout, non-blocking)
|
||||
const doLogin = () => {
|
||||
Api.instance.login('test123', '123456')
|
||||
.then(async (userId) => {
|
||||
console.log('[Login] userId:', userId);
|
||||
await this.startProxyClient();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('[Login] retry in 3s:', error);
|
||||
setTimeout(doLogin, 3000);
|
||||
});
|
||||
};
|
||||
doLogin();
|
||||
this.appStateSubscription = AppState.addEventListener('change', this.handleAppStateChange);
|
||||
}
|
||||
|
||||
@@ -94,11 +97,9 @@ export default class App extends Component<AppProps, WalletmanAppState> {
|
||||
|
||||
handleAppStateChange = (nextAppState: AppStateStatus) => {
|
||||
if (nextAppState === 'background' || nextAppState === 'inactive') {
|
||||
console.log('[AppState] 应用进入后台,关闭代理');
|
||||
this.stopProxyClient();
|
||||
console.log('[AppState] 应用进入后台,代理服务继续运行');
|
||||
} else if (nextAppState === 'active') {
|
||||
console.log('[AppState] 应用回到前台,重连代理');
|
||||
this.initProxyClient();
|
||||
console.log('[AppState] 应用回到前台');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,32 +122,30 @@ export default class App extends Component<AppProps, WalletmanAppState> {
|
||||
});
|
||||
}
|
||||
|
||||
async initProxyClient() {
|
||||
async startProxyClient() {
|
||||
try {
|
||||
this.clientId = DeviceInfo.getUniqueIdSync();
|
||||
const userId = Api.instance.getUserId();
|
||||
console.log('[Proxy] 初始化客户端:', this.clientId, 'userId:', userId);
|
||||
const { clientId } = this;
|
||||
console.log('[Proxy] 初始化后台服务:', clientId, 'userId:', userId);
|
||||
|
||||
await proxyManager.start({
|
||||
wsUrl: 'ws://192.168.1.117:16001/ws',
|
||||
await proxyBackgroundService.start({
|
||||
wsUrl: Api.WS_URL,
|
||||
clientId: this.clientId || '',
|
||||
userId: userId,
|
||||
debug: true,
|
||||
// debug: true,
|
||||
heartbeatInterval: 10000,
|
||||
reconnectInterval: 5000,
|
||||
reconnectMaxAttempts: 3,
|
||||
reconnectMaxAttempts: Infinity,
|
||||
onConnected: () => {
|
||||
console.log('[Proxy] 客户端已连接');
|
||||
console.log('[Proxy] 后台服务已连接');
|
||||
},
|
||||
onDisconnected: () => {
|
||||
console.log('[Proxy] 客户端已断开');
|
||||
console.log('[Proxy] 后台服务已断开');
|
||||
},
|
||||
onError: (error: string) => {
|
||||
console.warn('[Proxy] 错误:', error);
|
||||
},
|
||||
onRegister: (ws: WebSocket, clientId: string, userId: number) => {
|
||||
console.log('[Proxy] 客户端已注册:', clientId, userId);
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Proxy] 初始化失败:', error);
|
||||
@@ -155,10 +154,10 @@ export default class App extends Component<AppProps, WalletmanAppState> {
|
||||
|
||||
stopProxyClient() {
|
||||
try {
|
||||
proxyManager.stop();
|
||||
console.log('[Proxy] 客户端已断开');
|
||||
proxyBackgroundService.stop();
|
||||
console.log('[Proxy] 后台服务已停止');
|
||||
} catch (error) {
|
||||
console.error('[Proxy] 断开失败:', error);
|
||||
console.error('[Proxy] 停止失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user