My App Requirement :由于某些原因,我应该维护套接字连接以在服务器推送时触发本地通知,而不使用推送通知(APN) . 所以我使用iPhone的VOIP后台功能来维护套接字连接 .
1. I have configured a stream for VOIP in order to persist socket connection to run in background, so what Timeout value should I set? Will the socket connection terminates once the timeout expires? How do I make my application to listen to the socket all the time.?
客户端流配置如下,
NSString *urlStr = @"http://192.168.0.108";
NSURL *website = [NSURL URLWithString:urlStr];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
NSInputStream *inputStream = (NSInputStream *)readStream;
NSOutputStream *outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
[outputStream setDelegate:self];
[outputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
2. Should I reconnect the stream in the handler applicationDidEnterBackground:
[[UIApplication sharedApplication] setKeepAliveTimeout:86400 handler:^(void)
{
if (inputStream)
[inputStream close];
if (outputStream)
[outputStream close];
urlStr = @"http://192.168.0.108";
website = [NSURL URLWithString:urlStr];
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
[outputStream setDelegate:self];
[outputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType] ;
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}];
3. Say my server restarts and the app is in background, how do I ensure the connection? 如果iPhone中的Wi-Fi连接或我终止服务器应用程序的连接将被关闭,那么我应该采取什么措施让我的应用按预期工作?
2 回答
您还需要确保已在pList文件中设置
当您的应用程序在后台时,套接字将由iOS管理 . 只要插槽中有数据,您的应用程序就会收到CPU时间 . 所以在runLoop我正在检查ht
在我的情况下,信令协议在一个单独的线程中工作,所以我正在旋转runLoop我自己
并在需要时停止它:
对于runLoop中的套接字,我在将它们安排到runLoop之前设置了处理函数:
这些函数将在您的套接字为您提供一些信息时调用,即使您的应用程序在后台运行:
为了使服务器知道客户端仍处于活动状态,我们每隔10分钟将ping命令发送到服务器,因此KeepAlive处理程序设置为600.您可以使用其他值来节省电池,但会检测到客户端上的断开连接会更糟糕和服务器端 . 并且会增加断开连接和重新连接之间的时间 .
SchedulePing(0)将执行如下:
而StartLongBGTask是一个
这需要确保在发送ping并等待来自服务器的ping回复之前不会挂起应用程序 . 此外,如果套接字已经断开连接,则可能需要重新连接,这将花费一些时间并且需要proccess在后台运行 .
但是,当您不再需要它们时,请务必正确释放后台任务 . 当超过bg超时时,系统将杀死其他明智的应用程序 .
根据文档
实施VoIP应用有几个要求:
在UIBackgroundModes键中包含voip值可让系统知道它应该允许应用程序在后台运行以管理其网络套接字 . 此键还允许您的应用播放背景音频(尽管仍然鼓励包含UIBackgroundModes键的音频值) . 具有此密钥的应用程序也会在系统启动后立即在后台重新启动,以确保VoIP服务始终可用 . 有关UIBackgroundModes键的详细信息,请参阅信息属性列表键参考 .