GCDAsyncSocket后台VOIP [英] GCDAsyncSocket background VOIP
问题描述
我正在尝试实施这个(为VoIP使用配置套接字)使用这个(CocoaAsyncSocket)。据我所知,第1步我做了,将VOIP添加到plist中的后台服务,下面应该是第2步(配置应用程序的一个套接字用于VoIP使用)
I'm trying to implement this(Configuring Sockets for VoIP Usage) using this(CocoaAsyncSocket). To the best of my knowledge step 1 I have done, adding VOIP to background services in the plist, and below should be step 2 (Configure one of the app’s sockets for VoIP usage)
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)connectedPort
{
[CCV setLocalMode:FALSE];
[socket performBlock:^{
[socket enableBackgroundingOnSocket];
}];
但其余的步骤我无法弄清楚。
如果我这样做
But the rest of the steps I cannot figure out. If I do
- (void)applicationDidEnterBackground:(UIApplication *)application
{
expirationHandler = ^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
};
bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (1) {
sleep(1);
//NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
if ([rootViewController isIncomingCall] && showedCall != TRUE) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:@"Incoming Call."];
localNotif.alertAction = NSLocalizedString(@"Accept Call", nil);
localNotif.soundName = @"alarmsound.caf";
// localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
}
showedCall = TRUE;
}
}
});
}
我的客户会听10分钟然后停止。我需要做两件事:监视特定(来电)数据包,并每隔5秒发送一次keepalive。但是我没看到这些东西要配置在哪里。此外,上面的apple链接上的定义指出为了防止丢失其连接,VoIP应用程序通常需要定期唤醒并检查其服务器。为了促进此行为,iOS允许您使用特殊处理程序安装 setKeepAliveTimeout:handler:
UIApplication的方法
根据我的理解,这个超时的事情应该只是做keepalive,然而这没有意义,因为最短的时间是600秒?就像我上面所说的那样,我找不到我要告诉它的地方,以及找到它时要做什么。
My client will listen for 10mins then stop. I need to do 2 things: watch for a specific (incoming call) data packet, and send a keepalive every 5seconds. But I dont see where these things are to be configured at. Furthermore the definition on the apple link above states "To prevent the loss of its connection, a VoIP app typically needs to wake up periodically and check in with its server. To facilitate this behavior, iOS lets you install a special handler using the setKeepAliveTimeout:handler:
method of UIApplication
" From my understanding of that this timeout thing should only be doing the keepalive, however that doesnt make sense because the minimal time is 600seconds? And like I said above I cannot find where I tell it what packet to look for as well as what to do when its found.
推荐答案
所以我相信我已经解决了这个问题但需要进一步测试才能100%肯定。目前我使用此代码进行了测试,暂停时间为21分钟并且有效。
so i believe i have resolved the issue but need further testing to be 100% sure. Currently i ran a test with this code for the length of 21mins of suspended time and it worked
第2步:
[socket performBlock:^{
[socket enableBackgroundingOnSocket];
}];
第3步:
- (void)applicationDidEnterBackground:(UIApplication *)application {
inProgram = FALSE;
showedCall = FALSE;
BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
if (backgroundAccepted)
{
NSLog(@"VOIP backgrounding accepted");
}
}
这似乎让我的客户端保持运行所以从这里我添加一个观察者来等待我的来电数据包并在看到该数据包时发出通知
this seems to keep my client running so from here i add an observer to wait for my incoming call packet and launch a notification when that packet is seen
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"currentDigitalJoin"])
{
switch ([[cClient currentDigitalJoin]intValue])
{
case 1000:
{
if (showedCall != TRUE && inProgram != TRUE) {
NSLog(@"incoming audio call");
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:@"Incoming Call."];
localNotif.alertAction = NSLocalizedString(@"Accept Call", nil);
localNotif.soundName = @"alarmsound.caf";
[app presentLocalNotificationNow:localNotif];
[localNotif release];
showedCall = TRUE;
}
}
break;
}
case 602:
{
if (showedCall != TRUE && inProgram != TRUE) {
NSLog(@"incoming audio call");
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:@"Incoming Call."];
localNotif.alertAction = NSLocalizedString(@"Accept Call", nil);
localNotif.soundName = @"alarmsound.caf";
[app presentLocalNotificationNow:localNotif];
[localNotif release];
showedCall = TRUE;
}
}
break;
}
case 513:
{
showedCall = FALSE;
break;
}
}
}else if([keyPath isEqualToString:@"currentDigitalJoin"])
{
switch ([[cClient currentDigitalJoin]intValue])
{
case 602:
{
showedCall = FALSE;
break;
}
}
}
}
注意:所示的步骤是参考苹果文档中指示的步骤
NOTE: the "step"s indicated are in reference to the steps indicated on the apple documentation
也不要忘记在plist文件中设置require wireless
also dont forget to set the require wireless in the plist file
这篇关于GCDAsyncSocket后台VOIP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!