代理优化
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<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
|
||||
android:name=".MainApplication"
|
||||
|
||||
Submodule libs/rnwalletman updated: 00b2d6d784...e0bf391398
@@ -7,7 +7,8 @@
|
||||
"ios": "react-native run-ios",
|
||||
"lint": "eslint .",
|
||||
"start": "react-native start",
|
||||
"test": "jest"
|
||||
"test": "jest",
|
||||
"postinstall": "patch-package"
|
||||
},
|
||||
"dependencies": {
|
||||
"@react-native-cookies/cookies": "^6.2.1",
|
||||
@@ -15,6 +16,7 @@
|
||||
"buffer": "^6.0.3",
|
||||
"react": "18.2.0",
|
||||
"react-native": "0.72.10",
|
||||
"react-native-background-actions": "^4.0.1",
|
||||
"react-native-device-info": "14.0.4",
|
||||
"react-native-fs": "^2.20.0",
|
||||
"react-native-tcp-socket": "^6.4.1",
|
||||
@@ -35,6 +37,7 @@
|
||||
"eslint": "^8.19.0",
|
||||
"jest": "^29.2.1",
|
||||
"metro-react-native-babel-preset": "0.76.8",
|
||||
"patch-package": "^8.0.1",
|
||||
"prettier": "^2.4.1",
|
||||
"react-test-renderer": "18.2.0",
|
||||
"typescript": "4.8.4"
|
||||
|
||||
15
patches/react-native-background-actions+4.0.1.patch
Normal file
15
patches/react-native-background-actions+4.0.1.patch
Normal 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) {
|
||||
@@ -1,7 +1,12 @@
|
||||
import { WalletType } from 'rnwalletman';
|
||||
|
||||
const DOMAIN = '192.168.1.168:16000';
|
||||
const BASE_URL = `http://${DOMAIN}`;
|
||||
const WS_URL = `ws://${DOMAIN}/ws`;
|
||||
|
||||
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 userId: number = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user