1

所以这里是这个问题的简要描述:

我有一个使用 Web 套接字的应用程序 SocketRocket - 客户端,Nodejs + Socket.io - 服务器端。我在启动应用程序时建立了一个套接字连接,一切正常。如果我暂停应用程序(进入后台、锁定屏幕等),我可以在进入前台时重新连接。
但是,如果我失去连接(例如,如果我在路由器上阻止自己(我在 5-10 秒内收到套接字故障错误),然后解除阻止自己) - 我将无法使用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 并且永远不会完成。因此,实际上请求可能正在尝试启动,但从未到达执行点。

如果我在调试器中暂停应用程序,这就是我得到的结果:您可以在此处查看屏幕截图 -> 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]
4

1 回答 1

2

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

于 2012-05-12T05:48:41.603 回答