@@ -32,6 +32,7 @@ Notifications.setNotificationHandler({
3232
3333const API_BASE = 'http://111.170.6.103:9999' ;
3434const SCHEDULE_API = `${ API_BASE } /api/daily.php` ;
35+ const WS_URL = 'ws://111.170.6.103:9999/ws' ; // WebSocket 地址
3536
3637function 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
0 commit comments