Reconnect to XMPP when user receives an error or disconnects from XMPP

I have implemented a chat app using XMPP

framework. But I am getting the error:

xmpp did receive error:-
Printing description of error:
<stream:error xmlns:stream="http://etherx.jabber.org/streams"><conflict xmlns="urn:ietf:params:xml:ns:xmpp-streams"></conflict><text xmlns="urn:ietf:params:xml:ns:xmpp-streams" lang="">Replaced by new connection</text></stream>

      

You need to be guided by what might be causing this error and how to reconnect using XMPP

.

Thank.

+3


source to share


2 answers


Use the XMPPReconnect .. class

@property (nonatomic, readonly) XMPPReconnect *xmppReconnect;

self.xmppReconnect = [[XMPPReconnect alloc] init];
[self.xmppReconnect activate:self.xmppStream];
[self.xmppReconnect addDelegate:self delegateQueue:dispatch_get_main_queue()];

      



And implement the "XMPPReconnect" delegation methods

- (void)xmppReconnect:(XMPPReconnect *)sender didDetectAccidentalDisconnect:(SCNetworkReachabilityFlags)connectionFlags
{
    NSLog(@"didDetectAccidentalDisconnect:%u",connectionFlags);
}
- (BOOL)xmppReconnect:(XMPPReconnect *)sender shouldAttemptAutoReconnect:(SCNetworkReachabilityFlags)reachabilityFlags
{
    NSLog(@"shouldAttemptAutoReconnect:%u",reachabilityFlags);
    return YES;
}

      

+4


source


I believe that XMPPReconnect stops working when the client receives this error from the server. Check out XMPPReconnect.m. In the delegate functions, you will see that when you receive this error, the shouldReconnect variable is set to NO, which means there will be no further reconnection attempt.

The explanation is that "usually" this error is received when another client connects using the same resource, so XMPPReconnect is disconnected to avoid an endless loop of reconnections and disconnections between two different clients and the server.



However, I noticed that you sometimes get this error even if only one client is connected. Therefore, if you are sure that no two different clients will ever try to connect to the server at the same time, I would suggest commenting out the setting for the line to be associated with NO, which allows XMPPFramework to treat this as a normal disconnect.

0


source







All Articles