您的位置:首页 > 移动开发 > IOS开发

iOS应用中通过设置VOIP模式实现休眠状态下socket的长连接

2014-03-31 13:15 801 查看
如果你的应用程序需要在设备休眠的时候还能够收到服务器端发送的消息,那我们就可以借助VOIP的模式来实现这一需求。但是如果的应用程序并不是正真的VOIP应用,那当你把你的应用提交到AppStore的时候基本上会被苹果Reject.
但是如果你的应用是企业内部发布的或者你只想了解其中的原理,那该文也许对您会有所帮助。

一、在iOS中如何应用VOIP
VOIP程序需要稳定的网络去连接和它相关的服务,这样它才能接到来电和其他相关的数据。系统允许VOIP程序被挂断并提供组件去监听它们的sockets,而不是在任意时候都处于唤醒状态。设置VOIP应用程序步骤如下:

A、 添加UIBackgroundModes中的VOIP键值



大多数VOIP应用需要设置后台audio 应用去传递音频,因此你应该设置audio 和voip两个键值。

如果只是想通过VOIP来达到socket在休眠状态下维持长连接那只需要象上图一样设置VOIP即可。

B、 为VOIP设置一个应用程序socket
为了使应用程序在后台时保持稳定的连接,你必须tag你的主 通讯socket专门应用于VOIP,tagging这个socket来告诉系统,它必须在你的应用程序中断时接管这个socket。这个切换本身对于你
的应用程序时透明的,当新的数据到达socket的时候,系统会唤醒应用程序,并将socket的控制权返回给应用程序,这样应用程序就可以处理新来的数据。

你只需要tag用于voip服务的socket,这个 socket用来接收来电或者其他相关的数据来保持你的VOIP服务的连接。根据收到的信息,这个socket要决定下一步的动作。比如一个来电,你会想
弹出一个本地的通知来告知用户;对于其他不是那么关键的数据,你可能会想悄悄的处理这些数据并让系统将应用程序重新中断。

在IOS中,sockets是用流或者更高级的结构,设置一个VOIP的socket,你只需要在通常的设置中添加一个特殊的key来标明这个接口是用于连接VOIP服务的,下表列出了流的接口和设置:

设置流接口用于voip接口

1、NSInputStream 和NSOutputStream
对于 Cocoa streams, 使用 setProperty:forKey: 方法添加

NSStreamNetworkServiceType
属性给 stream.

改属性的值设为
NSStreamNetworkServiceTypeVoIP.

2、NSURLRequest
对于 URL loading system, 使用 setNetworkServiceType:
method of your NSMutableURLRequest object to set the network service

type of the request. The service type should be set to
NSURLNetworkServiceTypeVoIP.

3、CFReadStreamRef和CFWriteStreamRef
For Core Foundation streams, use the CFReadStreamSetProperty or
CFWriteStreamSetProperty function to add the kCFStreamNetwork-
ServiceType property to the stream. The value for this property should
be
set to kCFStreamNetworkServiceTypeVoIP.
(注意:当设置socket的时候,你需要在你的主信号通道中设置合适的service type key。当设置声道时,不需要设置这个key)

由于,VOIP应用程序需要一直运行以确保收到来电,所以如果程 序通过一个非零的exit code退出,系统将自动重启这个应用程序(这种退出方式可以发生在内存压力大时终止程序运行)。尽管如此,中断应用程序会release所有的
sockets,包括那个用于连接voip 服务的socket。因此,当程序运行时,它需要一直从头创建socket。

C、 在移出后台之前,调用setKeepAliveTimeout:handler:方法去建立一个定期执行的handler,你的应用程序可以运行这个handler来保持服务的连接
为了防止断连,voip程序需要定期被唤醒去检查它的服务。为了容易实现这个行为,IOS通过使用(UIApplication setKeepAliveTimeout:handler:)方法建立一个特殊的句柄。你可以在
applicationDidEnterBackground方法中建立该句柄。一旦建立,系统至少会在超时之前调用该句柄一次,来唤醒你的应用程序。

这个keep-alive handler在后台执行,必须尽快的返回参数,它有最多30秒的时间来执行所需的任务,如果这段时间内句柄没有返回,那么系统将终止应用程序。

当你建立了handler之后,确定应用程序所需的最大超时。系统保证会在最大超时之前调用handler,但是这个时间是不确定的,所以你的handler必须在你申明的超时之前做好执行程序的准备。

二、通过GCDAsyncSocket来实现休眠状态下的长连接:

1、在创建socket并且成功连接上远程服务器的delegate中执行下述代码

[objc] view
plaincopyprint?





- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)connectedPort{

[socket performBlock:^{

[socket enableBackgroundingOnSocket];

}];

}

2、在ApplicationDidEnterBackground方法中调用setKeepAliveTimeOut以保持长连接

[objc] view
plaincopyprint?





BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];

if (backgroundAccepted)

{

NSLog(@"VOIP backgrounding accepted");

}

参考:
1、XMPP中长连接问答
http://stackoverflow.com/questions/5257580/iphone-xmpp-app-run-background

2、GCDAsyncSocket实现后台长连接的VOIP问答
http://stackoverflow.com/questions/8748874/gcdasyncsocket-background-voip

3、后台运行(内容比较杂,但还是很有参考价值)
/article/7819673.html

4、有一篇国内的关于VOIP以及后台程序的博文(内容还是比较杂--还是英文的,估计只能由博主自己看懂,仅做参考) http://blog.csdn.net/gnicky/article/details/7452418
5、iOS socket编程基础
http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server

6、一篇StackOverflow.com中的问答,其中对iOS中对VOIP的解释比较详细
If you want to let your VOIP application run in background , except those base settings in plist file, you need a TCP socket who's property is set to
VOIP, than the iOS system will take care this socket for you, when your application enter background , every thing was 'sleep' except that tcp socket. and if VOIP server send some data thought that TCP socket, your application will be awake up for 10 secs.
during this time, you can post a local notification.

Only Tcp socket can be set as VOIP Socket. But from i know , mostly VOIP application are based on UDP socket. if you do not want to separate the control
socket from the data socket. you should create another tcp socket which is focus on 'awake' your application , and from my personal experience , it's very hard to keep this 'awake' signal and the real sip control signal synchronize, the application always
miss the sip invite request.

So,the best way is separating the sip control single from the UDP data socket , make it as a tcp socket , this is the best solution , but never use tcp
socket to transfer voice data.

Another dirty way: keep the application awake all the time. As i said , each TCP single the application received thought that 'VOIP' tcp socket , will
keep application awake for 10 seconds, so at the end of this duration(after 9 secs) , you can send a response to the server to ask for another signal , when the next signal arrived, the application will be awake again,after 9 secs , send response again. keep
doing this, your application will awake forever.
http://stackoverflow.com/questions/6601536/ios-voip-socket-will-not-run-in-background

7.CocoaAsyncSocket
https://github.com/robbiehanson/CocoaAsyncSocket
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: