iPhone 进入睡眠状态后,如何恢复 TCP Socket 连接
..我的应用程序仍在运行!
有这个问题 关于它进入睡眠+醒来时会发生什么。 我知道它在调用:(
- (void)AppDidBecomeActive:(NSNotification *)notification {
if(jsocket){
[jsocket release];
}
jsocket = [[johnsocket alloc] init];
if(![jsocket connectViaSocket:(NSString*)ipaddress port:@"3660"]){
//port 3660 is the XML port!!! good thing to know... 3663 is the binary protocol port. yay!
NSLog(@"error connecting from rooms page");
}
}
注意:我正在使用 Asyncsocket.m,它是帮助管理套接字等的伟大类..这就是 jsocket 的一个实例。这是一个 tcp 客户端应用程序。)
我实际上在这个函数中有一个警报弹出窗口,以确保它被调用..你看,iphone/ itouch 在插入 USB 时不会进入睡眠状态,这使得调试变得更加困难。
所以我的问题是,如何才能让它不崩溃?
..and my app is still running!
There is this question about what happens when it goes to sleep + wakes up. I know it's calling:
- (void)AppDidBecomeActive:(NSNotification *)notification {
if(jsocket){
[jsocket release];
}
jsocket = [[johnsocket alloc] init];
if(![jsocket connectViaSocket:(NSString*)ipaddress port:@"3660"]){
//port 3660 is the XML port!!! good thing to know... 3663 is the binary protocol port. yay!
NSLog(@"error connecting from rooms page");
}
}
(note: I'm using Asyncsocket.m, which is a great class that help manage sockets and such.. that's what jsocket is an instance of. This is a tcp client application.)
I actually had an alert popup in this function to make sure it was being called.. you see, the iphone/itouch don't go to sleep while plugged into USB, making this a more difficult situation to debug.
So my question is, how can I make it not crash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您对服务器有非常完全的控制,否则您无法在任意睡眠时间后简单地“唤醒”套接字。 远程服务器可能会在超时后关闭连接 - 无论如何,这是您应该假设的。
您将必须重新启动连接并从头开始将您的状态与服务器同步。
Unless you have very full control over the server, there is nothing you can do to simply "wake up" a socket after an arbirtary time asleep. the remote server will probably have closed the connection after a timeout - that is what you should assume anyway.
You will have to re-initiate the connection and sync your state with the server from scratch.