当我的 flutter 应用程序终止时(用户滑动以关闭应用程序),使用 Web RTC 连接的 Websocket 连接突然关闭(以星号显示)
颤振版
Flutter 3.24.0 • 通道稳定 • https://github.com/flutter/flutter.git 框架 • 修订版 80c2e84975(3 个月前) • 2024-07-30 23:06:49 +0700 发动机 • 修订版 b8800d88be 工具 • Dart 3.5.0 • DevTools 2.37.2
使用的Flutter库
sip_ua:https://pub.dev/packages/sip_ua
星号版本
星号20.4.0
我尝试使用flutter后台服务在连接关闭后立即打开连接:
@pragma('vm:entry-point')
static void onStart(ServiceInstance service) async {
print("call background serivce is starting");
SipUAListenerBackground newListener = SipUAListenerBackground();
SipHelperManager().getHelper().addSipUaHelperListener(newListener);
}
还有我的SipUAListenerBackground:
class SipUAListenerBackground implements SipUaHelperListener {
@override
void transportStateChanged(TransportState state) {
// If state is closed
// ...
UaSettings settings = UaSettings()
..webSocketUrl = savedLoginInfo['websocketUrl']
..uri = 'sip:${savedLoginInfo['username']}@${savedLoginInfo['server']}'
..authorizationUser = savedLoginInfo['authorizationUser']
..password = savedLoginInfo['password']
..displayName = savedLoginInfo['displayName']
..userAgent = 'Flutter SIP Client'
..dtmfMode = DtmfMode.RFC2833;
await _sipHelper!.start(settings);
}
}
那么,即使应用程序终止,是否仍可以保持我的 websocket 连接处于活动状态以侦听新的来电?任何帮助将不胜感激
经过一些研究后,我发现在应用程序终止后,我无法保持 websocket 连接处于活动状态,如下所述确保会话终止
因此,我保持其活力的唯一方法是在后台服务中将其初始化为:
@pragma('vm:entry-point')
static void onStartForeground(ServiceInstance service) async {
SipHelperManager().getHelper().start(settings); --> Start websocket connection
}
//and then using it in flutter background service configuration:
static Future<void> initializeService() async {
final service = FlutterBackgroundService();
await service.configure(
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStartForeground,
onBackground: onIosBackground,
),
androidConfiguration: AndroidConfiguration(
autoStart: false,
onStart: onStartForeground,
isForegroundMode: true,
autoStartOnBoot: false,
initialNotificationTitle: 'Your app',
initialNotificationContent: 'Initialing..',
foregroundServiceTypes: [
AndroidForegroundType.phoneCall,
AndroidForegroundType.microphone,
AndroidForegroundType.mediaPlayback //Adjust this type
],
),
);
}