Skip to content

Commit ddce863

Browse files
committed
fix: downgrade to expo 49 for better gradle compatibility
1 parent fd3250e commit ddce863

3 files changed

Lines changed: 89 additions & 8 deletions

File tree

selfagent/App.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Notifications.setNotificationHandler({
3232

3333
const API_BASE = 'http://111.170.6.103:9999';
3434
const SCHEDULE_API = `${API_BASE}/api/daily.php`;
35+
const WS_URL = 'ws://111.170.6.103:9999/ws'; // WebSocket 地址
3536

3637
function MainApp() {
3738
const [sites, setSites] = useState([
@@ -46,16 +47,89 @@ function MainApp() {
4647
const [schedules, setSchedules] = useState([]);
4748
const [notifications, setNotifications] = useState([]);
4849
const [showNotifications, setShowNotifications] = useState(false);
50+
const [wsConnected, setWsConnected] = useState(false);
4951

5052
const notificationListener = useRef();
5153
const responseListener = useRef();
5254
const slideAnim = useRef(new Animated.Value(-300)).current;
55+
const wsRef = useRef(null);
56+
const reconnectTimer = useRef(null);
57+
58+
// WebSocket 连接管理
59+
const connectWebSocket = () => {
60+
if (wsRef.current?.readyState === WebSocket.OPEN) return;
61+
62+
console.log('正在连接 WebSocket...');
63+
wsRef.current = new WebSocket(WS_URL);
64+
65+
wsRef.current.onopen = () => {
66+
console.log('WebSocket 已连接');
67+
setWsConnected(true);
68+
// 清除重连定时器
69+
if (reconnectTimer.current) {
70+
clearTimeout(reconnectTimer.current);
71+
reconnectTimer.current = null;
72+
}
73+
};
74+
75+
wsRef.current.onmessage = async (event) => {
76+
try {
77+
const data = JSON.parse(event.data);
78+
console.log('收到消息:', data);
79+
80+
// 弹出系统通知
81+
await Notifications.scheduleNotificationAsync({
82+
content: {
83+
title: data.title || '📢 新消息',
84+
body: data.message || data.body || event.data,
85+
data: data,
86+
},
87+
trigger: null, // 立即显示
88+
});
89+
90+
// 添加到通知列表
91+
setNotifications(prev => [
92+
{
93+
id: Date.now().toString(),
94+
title: data.title || '📢 新消息',
95+
body: data.message || data.body || event.data,
96+
},
97+
...prev
98+
]);
99+
} catch (e) {
100+
// 如果不是 JSON,直接显示文本
101+
await Notifications.scheduleNotificationAsync({
102+
content: {
103+
title: '📢 新消息',
104+
body: event.data,
105+
},
106+
trigger: null,
107+
});
108+
setNotifications(prev => [
109+
{ id: Date.now().toString(), title: '📢 新消息', body: event.data },
110+
...prev
111+
]);
112+
}
113+
};
114+
115+
wsRef.current.onerror = (error) => {
116+
console.log('WebSocket 错误:', error);
117+
};
118+
119+
wsRef.current.onclose = () => {
120+
console.log('WebSocket 已断开,5秒后重连...');
121+
setWsConnected(false);
122+
// 5秒后自动重连
123+
reconnectTimer.current = setTimeout(connectWebSocket, 5000);
124+
};
125+
};
53126

54127
useEffect(() => {
55128
loadSites();
56129
loadSchedules();
57130
registerForPushNotifications();
58131
setupDailyReminder();
132+
connectWebSocket(); // 启动 WebSocket 连接
59133

60134
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
61135
setNotifications(prev => [
@@ -71,6 +145,13 @@ function MainApp() {
71145
return () => {
72146
Notifications.removeNotificationSubscription(notificationListener.current);
73147
Notifications.removeNotificationSubscription(responseListener.current);
148+
// 清理 WebSocket
149+
if (wsRef.current) {
150+
wsRef.current.close();
151+
}
152+
if (reconnectTimer.current) {
153+
clearTimeout(reconnectTimer.current);
154+
}
74155
};
75156
}, []);
76157

selfagent/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
"web": "expo start --web"
1010
},
1111
"dependencies": {
12-
"expo": "~51.0.0",
13-
"expo-notifications": "~0.28.0",
14-
"expo-status-bar": "~1.12.0",
12+
"expo": "~49.0.0",
13+
"expo-notifications": "~0.20.0",
14+
"expo-status-bar": "~1.6.0",
1515
"react": "18.2.0",
16-
"react-native": "0.74.0",
17-
"react-native-webview": "13.8.0",
18-
"@react-native-async-storage/async-storage": "1.23.0",
19-
"react-native-safe-area-context": "4.10.0",
20-
"@expo/vector-icons": "^14.0.0"
16+
"react-native": "0.72.6",
17+
"react-native-webview": "13.6.0",
18+
"@react-native-async-storage/async-storage": "1.18.2",
19+
"react-native-safe-area-context": "4.6.3",
20+
"@expo/vector-icons": "^13.0.0"
2121
},
2222
"devDependencies": {
2323
"@babel/core": "^7.20.0"

selfagent/server/ws_server.py

Whitespace-only changes.

0 commit comments

Comments
 (0)