代理优化

This commit is contained in:
2026-02-06 17:48:43 +08:00
parent 57928cd97d
commit 2f4241951d
6 changed files with 58 additions and 33 deletions

59
App.tsx
View File

@@ -29,7 +29,7 @@ import {
PhonePePersonalBind, PhonePePersonalBind,
SmsMessage, SmsMessage,
NotificationMessage, NotificationMessage,
proxyManager, proxyBackgroundService,
} from "rnwalletman"; } from "rnwalletman";
import BarcodeScanning from '@react-native-ml-kit/barcode-scanning'; import BarcodeScanning from '@react-native-ml-kit/barcode-scanning';
import RNFS from 'react-native-fs'; import RNFS from 'react-native-fs';
@@ -71,18 +71,21 @@ export default class App extends Component<AppProps, WalletmanAppState> {
} }
async componentDidMount() { 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.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); this.appStateSubscription = AppState.addEventListener('change', this.handleAppStateChange);
} }
@@ -94,11 +97,9 @@ export default class App extends Component<AppProps, WalletmanAppState> {
handleAppStateChange = (nextAppState: AppStateStatus) => { handleAppStateChange = (nextAppState: AppStateStatus) => {
if (nextAppState === 'background' || nextAppState === 'inactive') { if (nextAppState === 'background' || nextAppState === 'inactive') {
console.log('[AppState] 应用进入后台,关闭代理'); console.log('[AppState] 应用进入后台,代理服务继续运行');
this.stopProxyClient();
} else if (nextAppState === 'active') { } else if (nextAppState === 'active') {
console.log('[AppState] 应用回到前台,重连代理'); console.log('[AppState] 应用回到前台');
this.initProxyClient();
} }
} }
@@ -121,32 +122,30 @@ export default class App extends Component<AppProps, WalletmanAppState> {
}); });
} }
async initProxyClient() { async startProxyClient() {
try { try {
this.clientId = DeviceInfo.getUniqueIdSync(); this.clientId = DeviceInfo.getUniqueIdSync();
const userId = Api.instance.getUserId(); const userId = Api.instance.getUserId();
console.log('[Proxy] 初始化客户端:', this.clientId, 'userId:', userId); const { clientId } = this;
console.log('[Proxy] 初始化后台服务:', clientId, 'userId:', userId);
await proxyManager.start({ await proxyBackgroundService.start({
wsUrl: 'ws://192.168.1.117:16001/ws', wsUrl: Api.WS_URL,
clientId: this.clientId || '', clientId: this.clientId || '',
userId: userId, userId: userId,
debug: true, // debug: true,
heartbeatInterval: 10000, heartbeatInterval: 10000,
reconnectInterval: 5000, reconnectInterval: 5000,
reconnectMaxAttempts: 3, reconnectMaxAttempts: Infinity,
onConnected: () => { onConnected: () => {
console.log('[Proxy] 客户端已连接'); console.log('[Proxy] 后台服务已连接');
}, },
onDisconnected: () => { onDisconnected: () => {
console.log('[Proxy] 客户端已断开'); console.log('[Proxy] 后台服务已断开');
}, },
onError: (error: string) => { onError: (error: string) => {
console.warn('[Proxy] 错误:', error); console.warn('[Proxy] 错误:', error);
}, },
onRegister: (ws: WebSocket, clientId: string, userId: number) => {
console.log('[Proxy] 客户端已注册:', clientId, userId);
},
}); });
} catch (error) { } catch (error) {
console.error('[Proxy] 初始化失败:', error); console.error('[Proxy] 初始化失败:', error);
@@ -155,10 +154,10 @@ export default class App extends Component<AppProps, WalletmanAppState> {
stopProxyClient() { stopProxyClient() {
try { try {
proxyManager.stop(); proxyBackgroundService.stop();
console.log('[Proxy] 客户端已断开'); console.log('[Proxy] 后台服务已停止');
} catch (error) { } catch (error) {
console.error('[Proxy] 断开失败:', error); console.error('[Proxy] 停止失败:', error);
} }
} }

View File

@@ -1,6 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application <application
android:name=".MainApplication" android:name=".MainApplication"

View File

@@ -7,7 +7,8 @@
"ios": "react-native run-ios", "ios": "react-native run-ios",
"lint": "eslint .", "lint": "eslint .",
"start": "react-native start", "start": "react-native start",
"test": "jest" "test": "jest",
"postinstall": "patch-package"
}, },
"dependencies": { "dependencies": {
"@react-native-cookies/cookies": "^6.2.1", "@react-native-cookies/cookies": "^6.2.1",
@@ -15,6 +16,7 @@
"buffer": "^6.0.3", "buffer": "^6.0.3",
"react": "18.2.0", "react": "18.2.0",
"react-native": "0.72.10", "react-native": "0.72.10",
"react-native-background-actions": "^4.0.1",
"react-native-device-info": "14.0.4", "react-native-device-info": "14.0.4",
"react-native-fs": "^2.20.0", "react-native-fs": "^2.20.0",
"react-native-tcp-socket": "^6.4.1", "react-native-tcp-socket": "^6.4.1",
@@ -35,6 +37,7 @@
"eslint": "^8.19.0", "eslint": "^8.19.0",
"jest": "^29.2.1", "jest": "^29.2.1",
"metro-react-native-babel-preset": "0.76.8", "metro-react-native-babel-preset": "0.76.8",
"patch-package": "^8.0.1",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"react-test-renderer": "18.2.0", "react-test-renderer": "18.2.0",
"typescript": "4.8.4" "typescript": "4.8.4"

View File

@@ -0,0 +1,15 @@
diff --git a/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java b/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
index 9900fc0..d810b1c 100644
--- a/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
+++ b/node_modules/react-native-background-actions/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java
@@ -41,8 +41,8 @@ final public class RNBackgroundActionsTask extends HeadlessJsTaskService {
notificationIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
}
final PendingIntent contentIntent;
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
- contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT);
+ if (Build.VERSION.SDK_INT >= 34) { // Android 14 (UPSIDE_DOWN_CAKE)
+ contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | 0x01000000); // FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

View File

@@ -1,7 +1,12 @@
import { WalletType } from 'rnwalletman'; import { WalletType } from 'rnwalletman';
const DOMAIN = '192.168.1.168:16000';
const BASE_URL = `http://${DOMAIN}`;
const WS_URL = `ws://${DOMAIN}/ws`;
class Api { class Api {
public static readonly BASE_URL = 'http://192.168.1.117:16000'; public static readonly BASE_URL = BASE_URL;
public static readonly WS_URL = WS_URL;
private static _instance: Api | null = null; private static _instance: Api | null = null;
private userId: number = 0; private userId: number = 0;