CocoaAsyncSocket 在后台后不起作用
我有一个 AsyncSocket 实例,在 iPad 上用作服务器,然后在另一台 iPad 上运行一个 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):
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.
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?
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法找到了这个问题的解决方案,所以我将在这里分享:-
在我的服务器类上,我添加了以下内容:
然后我添加了以下方法,使套接字再次开始监听:
仍然无法停止客户端当服务器进入后台时断开连接,所以我只是让它们循环,直到服务器再次可用,并且
appEnteredForeground
方法将确保服务器在 iPad(或 iPhone,我猜)后再次开始监听再次打开。I managed to find a solution to this issue, so I'll share it here:-
On my server class, I added the following:
Then I added the following method which makes the socket start listening again:
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.