1
votes

I am using GCDWebserver in my app and setting up GCDWebserver’s listening socket as the VOIP socket. Everything works well until my app goes in background for more than 3 mins. I can see that my GCDWebserver is running after 3 mins but my socket seems to be closing as the listening socket (VOIP socket) does not listen to the incoming requests. However, if I go in foreground at any time, it receives all the client requests sent to it in the time when it was in background. I would really appreciate if anyone could tell me what am I missing here and why is my VOIP socket not receiving the requests in the background when it clearly knows about it (as it handles all those requests as soon as its brought to the foreground.)

Another interesting thing is I am setting setKeepAlive handler when i go to background and when that handler fires after 10 mins, my socket starts receiving all the requests in the background that were sent by the clients when it was in background.

So here are the scenarios: Server receives all the request in foreground Server does not receive requests in background but handles all the earlier requests(sent when it was in background) as soon as it comes to the foreground Server receives all the earlier requests(sent when it was in background) when the setKeepAlive handler hits after every 10 mins.

Following my code for setting up VOIP (Background mode is already set in Info.plist as VOIP)

- (void) createVoipOnSocket:(int) listenSocket {
       CFReadStreamRef readStream = NULL;
       CFWriteStreamRef writeStream = NULL;

       CFSocketRef sn = CFSocketCreateWithNative(NULL, listenSocket, kCFSocketReadCallBack | kCFSocketDataCallBack | kCFSocketConnectCallBack, NULL, NULL);
       CFSocketNativeHandle csock = *(CFSocketNativeHandle *)sn;
       CFStreamCreatePairWithSocket(kCFAllocatorDefault, csock, &readStream, &writeStream);

       self.inputStream = (__bridge_transfer NSInputStream *)readStream;
       self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;

       [self.inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];

       [self.inputStream setDelegate:self];
       [self.outputStream setDelegate:self];

       [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
       [self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

       [self.inputStream open];
       [self.outputStream open];
}

In my AppDelegate I set the handler when app goes in background:

func applicationDidEnterBackground(application: UIApplication) {

    UIApplication.sharedApplication().setKeepAliveTimeout(600, handler: {

        STCNetworkManager.sharedInstance.sendToIPs(["10.0.1.8"], commandDictionary: ["name" : "chirp", "msg" : "Ping from sleeping device"], completionBlock: { (success, result) -> Void in
            if success {
                println("SUCCESS IN SENDING when asleep and result is : \(result)")
            } else {
                println("Could not send while sleeping")
            }
        })

    })        
 }

Is there a way that I can set up my VOIP socket to keep handling requests in the background as soon as client sends it.

1

1 Answers

0
votes

I'm not sure how could be creating your own VOIP socket and then interface it with GCDWebServer somehow, but assuming that works, make sure to set the GCDWebServerOption_AutomaticallySuspendInBackground option to NO (the default iS YES). Otherwise, GCDWebServer will automatically stop itself and close its listening socket when the app is suspended while in background.

See https://github.com/swisspol/GCDWebServer#gcdwebserver--background-mode-for-ios-apps and https://github.com/swisspol/GCDWebServer/blob/master/GCDWebServer/Core/GCDWebServer.h#L181