1

在我的应用程序中,我使用连接到服务器

- (void)connectToServerUsingCFStream:(NSString *) urlStr portNo: (uint) portNo

这个函数被另一个方法调用

- (void)connectToServer:(NSString *)serverName onPort:(int)portNo 
{

    [self connectToServerUsingCFStream:serverName portNo:portNo];

    while (!((iStream.streamStatus == 2) || (oStream.streamStatus == 2))) {

        continue; 
    }

    NSLog(@"Streams connected");

    [self sendLoginRequest];
}

现在我想知道是否可以轻松检查我的连接请求是否超时(可能具有一定的时间值?)。有没有办法在我的while循环中处理这个问题,或者我应该使用不同的东西?

在此先感谢,包茨

4

1 回答 1

1

我不知道您是如何准确实现连接的,但在这里我有一些来自XMPPFramework的连接代码,如代码注释所示:

/**
 * XMPPReconnect handles automatically reconnecting to the xmpp server due to accidental disconnections.
 * That is, a disconnection that is not the result of calling disconnect on the xmpp stream.
 * 
 * Accidental disconnections may happen for a variety of reasons.
 * The most common are general connectivity issues such as disconnection from a WiFi access point.
 * 
 * However, there are several of issues that occasionaly occur.
 * There are some routers on the market that disconnect TCP streams after a period of inactivity.
 * In addition to this, there have been iPhone revisions where the OS networking stack would pull the same crap.
 * These issue have been largely overcome due to the keepalive implementation in XMPPStream.
 * 
 * Regardless of how the disconnect happens, the XMPPReconnect class can help to automatically re-establish
 * the xmpp stream so as to have minimum impact on the user (and hopefully they don't even notice).
 * 
 * Once a stream has been opened and authenticated, this class will detect any accidental disconnections.
 * If one occurs, an attempt will be made to automatically reconnect after a short delay.
 * This delay is configurable via the reconnectDelay property.
 * At the same time the class will begin monitoring the network for reachability changes.
 * When the reachability of the xmpp host has changed, a reconnect may be tried again.
 * In addition to all this, a timer may optionally be used to attempt a reconnect periodically.
 * The timer is started if the initial reconnect fails.
 * This reconnect timer is fully configurable (may be enabled/disabled, and it's timeout may be changed).
 * 
 * In all cases, prior to attempting a reconnect,
 * this class will invoke the shouldAttemptAutoReconnect delegate method.
 * The delegate may use this opportunity to optionally decline the auto reconnect attempt.
 * 
 * Auto reconnect may be disabled at any time via the autoReconnect property.
 * 
 * Note that auto reconnect will only occur for a stream that has been opened and authenticated.
 * So it will do nothing, for example, if there is no internet connectivity when your application
 * first launches, and the xmpp stream is unable to connect to the host.
 * In cases such as this it may be desireable to start monitoring the network for reachability changes.
 * This way when internet connectivity is restored, one can immediately connect the xmpp stream.
 * This is possible via the manualStart method,
 * which will trigger the class into action just as if an accidental disconnect occurred.
**/

我不知道这个XMPPReconect 类是否满足您的需求。

于 2012-07-19T09:02:21.467 回答