fix:增加铃声、通话保持定时器
This commit is contained in:
parent
8f7ec774c1
commit
a677def7f3
@ -0,0 +1,53 @@
|
||||
import 'dart:async';
|
||||
|
||||
typedef TimeoutCallback = void Function();
|
||||
|
||||
class TalkPingOverTimeTimerManager {
|
||||
// 构造函数,接受超时时间
|
||||
TalkPingOverTimeTimerManager({required this.timeoutInSeconds});
|
||||
|
||||
// 定义一个可取消的 Timer
|
||||
Timer? _timer;
|
||||
|
||||
// 超时时间(以秒为单位),是 final 的,因此必须在构造函数中初始化
|
||||
final int timeoutInSeconds;
|
||||
|
||||
// 默认超时时间(以秒为单位)
|
||||
static const int defaultTimeoutInSeconds = 5;
|
||||
|
||||
// 超时回调函数
|
||||
TimeoutCallback? _onTimeout;
|
||||
|
||||
// 启动计时器
|
||||
void startTimer() {
|
||||
_cancelTimer(); // 取消任何已存在的计时器
|
||||
_timer = Timer(Duration(seconds: timeoutInSeconds), () {
|
||||
// 超时回调方法
|
||||
_onTimeout?.call();
|
||||
// 清楚定时器
|
||||
_cancelTimer();
|
||||
});
|
||||
}
|
||||
|
||||
// 接收到消息时调用此方法
|
||||
void receiveMessage() {
|
||||
print("Received a message, resetting the timer.");
|
||||
startTimer();
|
||||
}
|
||||
|
||||
// 设置超时回调函数
|
||||
void setOnTimeout(TimeoutCallback? callback) {
|
||||
_onTimeout = callback;
|
||||
}
|
||||
|
||||
// 取消计时器
|
||||
void _cancelTimer() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
}
|
||||
|
||||
// 清理资源
|
||||
void dispose() {
|
||||
_cancelTimer();
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,27 @@
|
||||
import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
import 'package:star_lock/app_settings/app_settings.dart';
|
||||
import 'package:star_lock/talk/other/audio_player_manager.dart';
|
||||
import 'package:star_lock/talk/startChart/constant/udp_constant.dart';
|
||||
import 'package:star_lock/talk/startChart/handle/other/talk_ping_overtime_timer_manger.dart';
|
||||
|
||||
import 'package:star_lock/talk/startChart/proto/generic.pb.dart';
|
||||
import 'package:star_lock/talk/startChart/start_chart_manage.dart';
|
||||
import 'package:star_lock/tools/storage.dart';
|
||||
|
||||
class ScpMessageBaseHandle {
|
||||
final startChartManage = StartChartManage();
|
||||
|
||||
final audioManager = AudioPlayerManager();
|
||||
|
||||
// 通话保持超时监听定时器管理
|
||||
final talkePingOverTimeTimerManager = TalkPingOverTimeTimerManager(
|
||||
timeoutInSeconds: 5,
|
||||
);
|
||||
|
||||
|
||||
|
||||
bool checkGenericRespSuccess(GenericResp genericResp) {
|
||||
if (genericResp == null) return false;
|
||||
final code = genericResp.code;
|
||||
@ -14,4 +33,14 @@ class ScpMessageBaseHandle {
|
||||
void log({required String text}) {
|
||||
AppLog.log('==========${text}');
|
||||
}
|
||||
|
||||
// 播放铃声
|
||||
void playRingtone() async {
|
||||
await audioManager.playRingtone();
|
||||
}
|
||||
|
||||
// 停止播放铃声
|
||||
void stopRingtone() async {
|
||||
await audioManager.stopRingtone();
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,10 +4,8 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:convert/convert.dart';
|
||||
import 'package:fast_rsa/fast_rsa.dart' as fastRsa;
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:pointycastle/export.dart' as pc;
|
||||
import 'package:star_lock/appRouters.dart';
|
||||
import 'package:star_lock/app_settings/app_settings.dart';
|
||||
import 'package:star_lock/flavors.dart';
|
||||
import 'package:star_lock/network/start_chart_api.dart';
|
||||
@ -15,17 +13,12 @@ import 'package:star_lock/talk/startChart/command/message_command.dart';
|
||||
import 'package:star_lock/talk/startChart/constant/ip_constant.dart';
|
||||
import 'package:star_lock/talk/startChart/constant/listen_addr_type_constant.dart';
|
||||
import 'package:star_lock/talk/startChart/constant/payload_type_constant.dart';
|
||||
import 'package:star_lock/talk/startChart/constant/udp_constant.dart';
|
||||
import 'package:star_lock/talk/startChart/entity/heartbeat_response.dart';
|
||||
import 'package:star_lock/talk/startChart/entity/login_response.dart';
|
||||
import 'package:star_lock/talk/startChart/entity/relay_info_entity.dart';
|
||||
import 'package:star_lock/talk/startChart/entity/report_information_data.dart';
|
||||
import 'package:star_lock/talk/startChart/entity/scp_message.dart';
|
||||
import 'package:star_lock/talk/startChart/entity/star_chart_register_node_entity.dart';
|
||||
import 'package:star_lock/talk/startChart/handle/scp_message_handle.dart';
|
||||
import 'package:star_lock/talk/startChart/handle/scp_message_handler_factory.dart';
|
||||
import 'package:star_lock/talk/startChart/proto/gateway_reset.pb.dart';
|
||||
import 'package:star_lock/talk/startChart/proto/generic.pb.dart';
|
||||
import 'package:star_lock/tools/deviceInfo_utils.dart';
|
||||
import 'package:star_lock/tools/storage.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
@ -67,9 +60,13 @@ class StartChartManage {
|
||||
bool isOnlineStartChartServer = false; // 星图是否上线成功
|
||||
int reStartOnlineStartChartServerIntervalTime = 1; // 重新上线星图服务任务间隔(s)
|
||||
Timer? reStartOnlineStartChartServerTimer; // 重新上线定时器
|
||||
int talkPingIntervalTime = 1; // 发送通话保持消息间隔(s)
|
||||
Timer? talkPingTimer; // 发送通话保持消息定时器
|
||||
|
||||
String relayPeerId = ''; // 中继peerId
|
||||
|
||||
bool _calling = false; // 是否处于通话中
|
||||
|
||||
// 星图服务初始化
|
||||
Future<void> init() async {
|
||||
if (isOnlineStartChartServer && _udpSocket != null) {
|
||||
@ -122,6 +119,16 @@ class StartChartManage {
|
||||
}
|
||||
}
|
||||
|
||||
/// 判断是否通话中
|
||||
bool checkIsCalling() {
|
||||
return _calling;
|
||||
}
|
||||
|
||||
/// 设置通话状态
|
||||
void changeCallingStatus(bool status) {
|
||||
_calling = status;
|
||||
}
|
||||
|
||||
// 初始化udp
|
||||
Future<void> _onlineRelayService() async {
|
||||
var addressIListenFrom = InternetAddress.anyIPv4;
|
||||
@ -211,9 +218,57 @@ class StartChartManage {
|
||||
await _sendMessage(message: message);
|
||||
}
|
||||
|
||||
// 发送通话保持消息
|
||||
void sendTalkPingMessage(
|
||||
// 发送同意接听消息
|
||||
void sendTalkAcceptMessage(
|
||||
{required String ToPeerId, required int gatewayId}) async {
|
||||
final message = MessageCommand.talkAcceptMessage(
|
||||
ToPeerId: ToPeerId,
|
||||
FromPeerId: FromPeerId,
|
||||
);
|
||||
await _sendMessage(message: message);
|
||||
}
|
||||
|
||||
// 发送拒绝接听消息
|
||||
void sendTalkRejectMessage({
|
||||
required String ToPeerId,
|
||||
required int gatewayId,
|
||||
}) async {
|
||||
final message = MessageCommand.talkRejectMessage(
|
||||
ToPeerId: ToPeerId,
|
||||
FromPeerId: FromPeerId,
|
||||
);
|
||||
await _sendMessage(message: message);
|
||||
}
|
||||
|
||||
// 回复成功消息
|
||||
void sendGenericRespSuccessMessage(
|
||||
{required String ToPeerId,
|
||||
required String FromPeerId,
|
||||
required int PayloadType}) async {
|
||||
final message = MessageCommand.genericRespSuccessMessage(
|
||||
ToPeerId: ToPeerId,
|
||||
FromPeerId: FromPeerId,
|
||||
PayloadType: PayloadType,
|
||||
);
|
||||
await _sendMessage(message: message);
|
||||
}
|
||||
|
||||
// 回复失败消息
|
||||
void sendGenericRespErrorMessage(
|
||||
{required String ToPeerId,
|
||||
required String FromPeerId,
|
||||
required int PayloadType}) async {
|
||||
final message = MessageCommand.genericRespErrorMessage(
|
||||
ToPeerId: ToPeerId,
|
||||
FromPeerId: FromPeerId,
|
||||
PayloadType: PayloadType,
|
||||
);
|
||||
await _sendMessage(message: message);
|
||||
}
|
||||
|
||||
// 发送通话保持消息
|
||||
Future<void> sendTalkPingMessage(
|
||||
{required String ToPeerId, required String FromPeerId}) async {
|
||||
final message = MessageCommand.talkPingMessage(
|
||||
ToPeerId: ToPeerId,
|
||||
FromPeerId: FromPeerId,
|
||||
@ -597,4 +652,26 @@ class StartChartManage {
|
||||
_log(text: '❌ 处理udp返回数据时遇到错误---> $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 通话保持定时器
|
||||
void startTalkPingMessageTimer() {
|
||||
talkPingTimer ??= Timer.periodic(
|
||||
Duration(
|
||||
seconds: talkPingIntervalTime,
|
||||
),
|
||||
(Timer timer) async {
|
||||
// 重新发送上线消息
|
||||
await sendTalkPingMessage(
|
||||
ToPeerId: ToPeerId,
|
||||
FromPeerId: FromPeerId,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// 停止通话保持
|
||||
void stopTalkPingMessageTimer() {
|
||||
talkPingTimer?.cancel();
|
||||
talkPingTimer = null; // 清除定时器引用
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user