CocoaAsyncSocket在后台运行后不起作用 [英] CocoaAsyncSocket doesn't work after being in background

查看:247
本文介绍了CocoaAsyncSocket在后台运行后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AsyncSocket的实例,我一直在iPad上使用它作为服务器,然后在另一个充当客户端的iPad上运行的AsyncSocket套接字。我有所有必要的代码在客户端和服务器之间交换数据 - 那里没有问题。

I have an instance of AsyncSocket which I've been using as a server on an iPad, and then an AsyncSocket socket running on another iPad which is acting as the client. I have all the necessary code to exchange data between the client and server -- there are no issues there.

我遇到的问题是它一切正常,但在我的应用程序的错误测试期间,我注意到一个特别奇怪(和恼人的问题):

The problem I'm experiencing is that it all works fine, but during bug testing of my app I have noticed one particularly strange (and irritating issue):

如果我关闭服务器iPad(此时没有套接字的委托在服务器上被触发),然后客户端断开连接(并进入我不断重试的循环)。令人讨厌的是,即使服务器重新启动,客户端仍然无法连接到它。事实上,即使我从头开始重新启动客户端,它仍然无法连接到服务器。我必须重新启动服务器应用程序,以便客户端能够再次连接。

奇怪的是,此错误仅在服务器实际上是从顶部的按钮关闭(即进入待机状态)。如果我只是使用主页按钮将应用程序发送到后台,则客户端仍然保持与服务器的连接:只有当设备断开连接时,客户端才会收到断开委托消息并断开连接,然后拒绝重新连接。与此同时,服务器完全没有注意到这一点,根本没有任何委托方法。

The odd thing is that that this bug is only triggered when the server is actually switched "off" (i.e. put into standby) from the button at the top. If I just send the app to the background using the home button, then the client still maintains its connection to the server: it's only when the device is disconnected that the client receives the disconnect delegate message and disconnects, then refuses to reconnect. The server, meanwhile, is completely oblivious to this and no delegate methods are fired at all.

概括我的问题:


  • 使用iPad顶部的按钮将设备置于待机状态时,AsyncSocket服务器实例会发生什么变化?

  • 为什么没有触发委托方法,但是任何连接的客户端都断开连接?

  • 当设备再次开启时会发生什么?

  • 为什么客户端无法重新连接?

  • What exactly happens to an AsyncSocket server instance when the device is put into standby using the button at the top of the iPad?
  • Why are no delegate methods fired, yet any connected clients get disconnected?
  • What happens when the device is turned on again?
  • Why are clients unable to reconnect?

推荐答案

我设法找到了这个问题的解决方案,所以我将在这里分享: -

I managed to find a solution to this issue, so I'll share it here:-

在我的服务器类上,我添加了以下内容:

On my server class, I added the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];

然后我添加了以下方法,使套接字再次开始监听:

Then I added the following method which makes the socket start listening again:

- (void)applicationDidBecomeActive {
    NSError *error;
    if (![socket acceptOnPort:kPORT error:&error])
    {
        // error code
    }
}

当服务器进入后台时,仍无法阻止客户端断开连接,所以我只是让它们循环直到服务器再次可用,并且 appEnteredForeground 方法将确保一旦iPad(或我猜想iPhone)再次打开,服务器就会再次开始监听。

There's still no way to stop clients disconnecting when the server goes into the background, so I just make them loop until the server becomes available again, and the appEnteredForeground method will ensure the server starts listening again once the iPad (or iPhone, I guess) is turned on again.

这篇关于CocoaAsyncSocket在后台运行后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆