iphone - NSURLConnection 在我得到 Error Domain=NSPOSIXErrorDomain Code=57 后不会响应

标签 iphone objective-c ios websocket socket.io

下面是问题的简要描述:

我有一个应用程序使用网络套接字 SocketRocket - 客户端,Nodejs + Socket.io - 服务器端。 我在启动应用程序时建立套接字连接,一切正常。如果我暂停应用程序(进入后台、锁定屏幕等),我可以在进入前台时重新连接。
但是,如果我失去连接(例如,如果我在路由器上阻止自己(我在 5-10 秒内收到套接字故障错误),然后解除阻止自己)-我将无法使用 连接到任何 url NSURLConnection。 (我为什么要为此烦恼?好吧,人们往往在移动设备上经常放松,所以我必须处理这个错误)

如果我以前有一个有效的连接,则在连接恢复/进入前台时调用此代码。

 -(void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary*)params withNamespace:(NSString *)endpoint
{    

  if (!_isConnected && !_isConnecting) 
  {
    _isConnecting = YES;

    _host = host;
    _port = port;
    _endpoint = [endpoint copy];

    NSMutableString *query = [[NSMutableString alloc] initWithString:@""];
    [params enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
        [query appendFormat:@"&%@=%@",key,value];
    }];

    NSString *s = [NSString stringWithFormat:HANDSHAKE_URL, _host, _port, rand(), query];
    [self log:[NSString stringWithFormat:@"Connecting to socket with URL: %@",s]];        

    _timeout = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(disconnect) userInfo:nil repeats:NO];

    [[LRResty client] get:s withBlock:^(LRRestyResponse *response) {
        [_timeout invalidate], _timeout = nil;                                
        if ( response.status == 200 )
        {
            NSString *responseString = [response asString];

            [self log:[NSString stringWithFormat:@"requestFinished() %@", responseString]];
            NSArray *data = [responseString componentsSeparatedByString:@":"];

            _sid = [data objectAtIndex:0];
            [self log:[NSString stringWithFormat:@"sid: %@", _sid]];

            _heartbeatTimeout = [[data objectAtIndex:1] floatValue] + 7.0;
            [self log:[NSString stringWithFormat:@"heartbeatTimeout: %f", _heartbeatTimeout]];

            NSString *t = [data objectAtIndex:3];
            NSArray *transports = [t componentsSeparatedByString:@","];
            [self log:[NSString stringWithFormat:@"transports: %@", transports]];

            [self openSocket];                 
        }
        else
        {
            [_delegate socketIOHandshakeFailed:self];
        }
    }];                       
  }                 
}

这就是我在正常情况下得到的结果:

Connecting to socket with URL: https://example.com:9001/socket.io/1/?t=282442 
requestFinished() ==> 843717395328441553:60:60:websocket
debug: Finished request GET https://example.com:9001/socket.io/1/?t=282442 <LRRestyRequest>

如果我在连接失败后尝试重新连接:

Connecting to socket with URL: https://example.com:9001/socket.io/1/?t=282475249

我尝试运行一个同步请求,但它会阻塞我的 UI 并且永远不会完成。所以,实际上 request 可能正在尝试开始,但永远不会到达执行点。

这是我在调试器中暂停应用程序时得到的结果: 您可以在此处查看屏幕截图 -> http://avvs.co/static/app_paused.jpg
检查线程 7 -

#0 0x9855fc22 in mach_msg_trap ()
#7 0x026852d3 in -[NSRunLoop(NSRunLoop) run] ()
#8 0x00019b1b in -[LRURLRequestOperation start]
#9 0x00016670 in -[LRRestyRequest start] 

更新
这是该线程的完整操作列表,如果您能指出这里的错误,我们将非常高兴

#0  0x9855fc22 in mach_msg_trap ()
#1  0x9855f1f6 in mach_msg ()
#2  0x0073c10a in __CFRunLoopServiceMachPort ()
#3  0x0069f5d5 in __CFRunLoopRun ()
#4  0x0069ed84 in CFRunLoopRunSpecific ()
#5  0x0069ec9b in CFRunLoopRunInMode ()
#6  0x0268540f in -[NSRunLoop(NSRunLoop) runMode:beforeDate:] ()
#7  0x026852d3 in -[NSRunLoop(NSRunLoop) run] ()
#8  0x00019b0b in -[LRURLRequestOperation start]
#9  0x00016660 in -[LRRestyRequest start]

最佳答案

我终于找到了解决方案。实际问题不在 NSURLConnection 中,而在套接字实现中。当失去连接时,应用程序正在关闭输入和输出流,但它们并没有从运行循环中移除,也没有被调度。通过修复它,我能够恢复连接。

关于iphone - NSURLConnection 在我得到 Error Domain=NSPOSIXErrorDomain Code=57 后不会响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10552371/

相关文章:

iphone - Xcode:启用分页的 UIScrollView 中的 Pdf

ios - 我需要帮助“使用文本字段自动完成 google places( objective-c )

ios - 我可以使用 Swift 获取所有现有的通知中心消息吗?

ios - 在 iOS 9 中点击 UITextView 中的 NSLinkAttributeName 链接不起作用

ios - NS操作反向依赖

ios - UITextView 不保持 subview 可见

ios - 委托(delegate)方法发送自己的对象或不发送它

iphone - iPhone 设备屏幕关闭模式下可以录制视频吗?

iphone - 无法将 UILabel 文本阴影设置为灰色以外的任何颜色

iphone - iOS : firing multiple segues from a prototype cell in storyboards