diff --git a/frontend/mining-app/lib/core/network/price_websocket_service.dart b/frontend/mining-app/lib/core/network/price_websocket_service.dart index 43573182..cadf3c1b 100644 --- a/frontend/mining-app/lib/core/network/price_websocket_service.dart +++ b/frontend/mining-app/lib/core/network/price_websocket_service.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'package:flutter/foundation.dart'; import 'package:socket_io_client/socket_io_client.dart' as IO; /// 价格更新数据 @@ -75,8 +74,6 @@ class PriceWebSocketService { /// /// [baseUrl] API 基础地址,例如 https://api.example.com void connect(String baseUrl) { - debugPrint('[PriceWS] 🔌 connect() called with baseUrl: $baseUrl'); - // 构建 WebSocket URL // https://api.example.com -> wss://api.example.com/ws/price _wsUrl = baseUrl @@ -84,8 +81,6 @@ class PriceWebSocketService { .replaceFirst('http://', 'ws://'); _wsUrl = '$_wsUrl/ws/price'; - debugPrint('[PriceWS] 🔌 Constructed WebSocket URL: $_wsUrl'); - _doConnect(); } @@ -96,7 +91,6 @@ class PriceWebSocketService { } _updateStatus(WebSocketStatus.connecting); - debugPrint('[PriceWS] Connecting to $_wsUrl'); _socket = IO.io( _wsUrl, @@ -111,14 +105,12 @@ class PriceWebSocketService { ); _socket!.onConnect((_) { - debugPrint('[PriceWS] ✅ Connected successfully to $_wsUrl'); _updateStatus(WebSocketStatus.connected); _reconnectAttempts = 0; _cancelReconnectTimer(); }); - _socket!.onDisconnect((reason) { - debugPrint('[PriceWS] ❌ Disconnected, reason: $reason'); + _socket!.onDisconnect((_) { _updateStatus(WebSocketStatus.disconnected); // 如果不是主动断开,尝试重连 if (_status != WebSocketStatus.disconnected) { @@ -126,31 +118,26 @@ class PriceWebSocketService { } }); - _socket!.onConnectError((error) { - debugPrint('[PriceWS] ❌ Connect error: $error'); - debugPrint('[PriceWS] URL was: $_wsUrl'); + _socket!.onConnectError((_) { _updateStatus(WebSocketStatus.disconnected); _scheduleReconnect(); }); - _socket!.onError((error) { - debugPrint('[PriceWS] ⚠️ Socket error: $error'); + _socket!.onError((_) { + // 错误处理 }); - // 监听连接超时 _socket!.onConnectTimeout((_) { - debugPrint('[PriceWS] ⏰ Connection timeout'); + // 连接超时处理 }); // 监听价格更新事件 _socket!.on('priceUpdate', (data) { try { - debugPrint('[PriceWS] 📊 Received price update: $data'); final update = PriceUpdate.fromJson(data as Map); - debugPrint('[PriceWS] 📊 Parsed: price=${update.price}, burnMultiplier=${update.burnMultiplier}'); _priceUpdateController.add(update); - } catch (e) { - debugPrint('[PriceWS] ❌ Parse error: $e, data: $data'); + } catch (_) { + // 解析错误处理 } }); @@ -161,20 +148,16 @@ class PriceWebSocketService { /// /// 调用此方法后不会自动重连 void disconnect() { - debugPrint('[PriceWS] 🔌 disconnect() called, current status: $_status'); _cancelReconnectTimer(); _reconnectAttempts = 0; if (_socket != null) { - debugPrint('[PriceWS] 🔌 Disposing socket...'); _socket!.disconnect(); _socket!.dispose(); _socket = null; - debugPrint('[PriceWS] 🔌 Socket disposed'); } _updateStatus(WebSocketStatus.disconnected); - debugPrint('[PriceWS] 🔌 Disconnect complete'); } /// 检查是否已连接 @@ -183,7 +166,6 @@ class PriceWebSocketService { /// 安排重连 void _scheduleReconnect() { if (_reconnectAttempts >= _maxReconnectAttempts) { - debugPrint('[PriceWS] Max reconnect attempts reached'); return; } @@ -200,7 +182,6 @@ class PriceWebSocketService { ); _reconnectAttempts++; - debugPrint('[PriceWS] Reconnect attempt $_reconnectAttempts in ${delay.inSeconds}s'); _reconnectTimer = Timer(delay, () { if (_status != WebSocketStatus.connected) { diff --git a/frontend/mining-app/lib/presentation/pages/asset/asset_page.dart b/frontend/mining-app/lib/presentation/pages/asset/asset_page.dart index 34395010..5bbbafa2 100644 --- a/frontend/mining-app/lib/presentation/pages/asset/asset_page.dart +++ b/frontend/mining-app/lib/presentation/pages/asset/asset_page.dart @@ -48,15 +48,11 @@ class _AssetPageState extends ConsumerState { @override void initState() { super.initState(); - debugPrint('[AssetPage] 📱 initState - connecting WebSocket'); - // 连接 WebSocket _connectWebSocket(); } @override void dispose() { - debugPrint('[AssetPage] 📱 dispose - disconnecting WebSocket'); - // 断开 WebSocket _disconnectWebSocket(); _refreshTimer?.cancel(); super.dispose(); @@ -64,33 +60,25 @@ class _AssetPageState extends ConsumerState { /// 连接 WebSocket void _connectWebSocket() { - debugPrint('[AssetPage] 🔌 _connectWebSocket called'); final wsService = PriceWebSocketService.instance; - // 使用 AppConstants 的 baseUrl - debugPrint('[AssetPage] 🔌 Using baseUrl: ${AppConstants.baseUrl}'); wsService.connect(AppConstants.baseUrl); // 监听价格更新 _priceSubscription = wsService.priceUpdates.listen((update) { - debugPrint('[AssetPage] 📊 Received price update: price=${update.price}, multiplier=${update.burnMultiplier}'); if (mounted) { setState(() { _currentPrice = update.price; _currentBurnMultiplier = update.burnMultiplier; }); - debugPrint('[AssetPage] 📊 State updated: _currentPrice=$_currentPrice, _currentBurnMultiplier=$_currentBurnMultiplier'); } }); - debugPrint('[AssetPage] 🔌 WebSocket listener attached'); } /// 断开 WebSocket void _disconnectWebSocket() { - debugPrint('[AssetPage] 🔌 _disconnectWebSocket called'); _priceSubscription?.cancel(); _priceSubscription = null; PriceWebSocketService.instance.disconnect(); - debugPrint('[AssetPage] 🔌 WebSocket disconnected'); } /// 启动定时器(使用外部传入的每秒增长值)