import 'dart:async'; import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart'; import '../../shared/data_sources/models/enum/sc_heartbeat_status.dart'; class SCHeartbeatUtils { static Timer? _h; static Timer? _a; static String _c = SCHeartbeatStatus.ONLINE.name; static String? _r; static bool _u = false; ///定时发送心跳 static void scheduleHeartbeat( String status, bool upMick, { String? roomId, }) async { cancelAnchorTimer(); _c = status; _u = upMick; _r = roomId; SCAccountRepository().heartbeat(_c, _u, roomId: _r).whenComplete(() { try { _h ??= Timer.periodic(Duration(seconds: 60), (timer) { SCAccountRepository().heartbeat(_c, _u, roomId: _r); }); } catch (e) {} }); } static void cancelTimer() { _h?.cancel(); _h = null; _r = null; cancelAnchorTimer(); } ///上麦主播发送心跳 static void scheduleAnchorHeartbeat(String roomId) async { cancelAnchorTimer(); scheduleHeartbeat(_c, true, roomId: _r); SCAccountRepository().anchorHeartbeat(roomId).whenComplete(() { _a ??= Timer.periodic(Duration(seconds: 60), (timer) { try { SCAccountRepository().anchorHeartbeat(roomId); } catch (e) {} }); }); } static void cancelAnchorTimer() { _a?.cancel(); _a = null; _u = false; } }