我正在iOS和Android中使用ejabberd创建一个聊天应用程序。该应用程序还具有脱机推送通知。为此,我需要在每次登录时连接到相同的资源。在android中,我可以这样做
XMPPTCPConnectionConfiguration.Builder confBuilder = XMPPTCPConnectionConfiguration.builder()
.setServiceName(serviceName)
.setUsernameAndPassword(jidParts[0], password)
.setConnectTimeout(3000)
// .setDebuggerEnabled(true)
.setResource("xxxxx")
.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
但在IOS,我不能setResource
因为我不知道如何在iOS上设置它。登录代码如下
- (BOOL)connect:(NSString *)myJID withPassword:(NSString *)myPassword auth:(AuthMethod)auth hostname:(NSString *)hostname port:(int)port
{
if (![xmppStream isDisconnected]) {
[self disconnect];
}
if (myJID == nil || myPassword == nil) {
return NO;
}
NSLog(@"Connect using JID %@", myJID);
[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];
username = myJID;
password = myPassword;
authMethod = auth;
xmppStream.hostName = (hostname ? hostname : [username componentsSeparatedByString:@"@"][1]);
if(port){
xmppStream.hostPort = port;
}
NSError *error = nil;
if (port == 5223) {
self.xmppReconnect.usesOldSchoolSecureConnect = YES;
if (![xmppStream oldSchoolSecureConnectWithTimeout:30 error:&error])
{
DDLogError(@"Error connecting: %@", error);
if (self.delegate){
[self.delegate onLoginError:error];
}
return NO;
}
} else {
if (![xmppStream connectWithTimeout:30 error:&error])
{
DDLogError(@"Error connecting: %@", error);
if (self.delegate){
[self.delegate onLoginError:error];
}
return NO;
}
}
return YES;
}
如何在上面的代码中广告资源?
您可以通过将XMPPJID的init方法更改为来设置资源
[xmppStream setMyJID:[XMPPJID jidWithString:myJID resource:resourceId]];
这是XMPPJID中的重载方法