CocoaAsyncSocket在后台运行后不起作用

时间:2022-05-02 02:29:07

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.

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

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):

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

If I turn off the server iPad (at which point none of the socket's delegates are fired on the server), then the client gets disconnected (and enters into a loop that I made where it continually retries). What's annoying is that even when the server comes back up, the client still can't connect to it. In fact, even if I start the client up again from scratch, it still can't connect to the server. I have to restart the server app in order for the client to be able to connect again.

如果我关闭服务器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.

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

To generalise my question:

概括我的问题:

  • What exactly happens to an AsyncSocket server instance when the device is put into standby using the button at the top of the iPad?
  • 当设备使用iPad顶部的按钮进入待机状态时,AsyncSocket服务器实例会发生什么?

  • 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?
  • 为什么客户无法重新连接?

1 个解决方案

#1


1  

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
    }
}

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.

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

#1


1  

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
    }
}

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.

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