我在ios应用程序中使用xmpp framwork来支持即时消息,我需要确保在离线用户上线时会收到离线消息,但我发现在线用户尝试向其发送消息时离线用户,在线用户很快就会离线,消息无法成功发送,有没有人遇到过这个问题?
以下是我的相关代码:
- (无效)setupStream {
xmppStream = [[XMPPStream alloc] init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_current_queue()];
}
- (无效)goOnline {
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
}
- (无效)goOffline {
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[[self xmppStream] sendElement:presence];
}
- (BOOL)connect:(NSString *)用户名密码:(NSString *)pwd {
if (xmppStream == nil) {
[self setupStream];
}
if (![xmppStream isDisconnected]) {
return YES;
}
if (username == nil || pwd == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:[NSString stringWithFormat:@"%@@%@", username,LoversClock_Openfire_ServerName]]];
[xmppStream setHostName:LoversClock_Openfire_DomainName];
[xmppStream setHostPort:LoversClock_Openfire_XMPP_Port];
self.password = pwd;
NSError *error = nil;
if (![xmppStream connect:&error]) {
NSLog(@"cant connect %@", LoversClock_Openfire_DomainName);
return NO;
}
return YES;
}
- (无效)断开{
[self goOffline];
[xmppStream disconnect];
}
- (void)sendMessage:(DDXMLElement *)message {
[_arrayXMPPMessage addObject:message];
if (_isOFOnline) {
[[self xmppStream] sendElement:message];
}
}
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
NSError *error = nil;
[[self xmppStream] authenticateWithPassword:password error:&error];
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
if (reconnectTimer!=nil && [reconnectTimer isValid]) {
[reconnectTimer invalidate];
}
reconnectTimer = nil;
[self goOnline];
_isOFOnline = YES;
[self sendOfflineMsg];
[onlineStateView setBackgroundColor:[UIColor greenColor]];
}
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error {
_isOFOnline = NO;
if (!isOffline && !(reconnectTimer!=nil && [reconnectTimer isValid])) {
NSDictionary* personalInfoDict = [ConfigRestoreHelper sharedInstance].myPersonalInfo;
if (personalInfoDict != nil) {
[self connect:[personalInfoDict objectForKey:KEY_USERNAME] password:[personalInfoDict objectForKey:KEY_PASSWORD]];
reconnectTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(reconnectFired:) userInfo:nil repeats:YES];
}
}
[onlineStateView setBackgroundColor:[UIColor yellowColor]];
}
以下是我的消息示例
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body addAttributeWithName:@"clockid" stringValue:_clockId];
[body addAttributeWithName:@"bedclocktype" stringValue:SafeGetStringValue([speechDict objectForKey:@"bedclocktype"])];
[body addAttributeWithName:@"bedclockpath" stringValue:SafeGetStringValue([speechDict objectForKey:@"bedclockpath"])];
NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:XMPP_MESSAGE_WAKETAUP];
[message addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"%@@%@", toUser,LoversClock_Openfire_ServerName]];
[message addAttributeWithName:@"fromuser" stringValue:fromUser];
[message addAttributeWithName:@"destuserid" stringValue:[[ConfigRestoreHelper sharedInstance].taPersonalInfo objectForKey:KEY_USERID]];
[message addChild:body];
[(AppDelegate*)[UIApplication sharedApplication].delegate sendMessage:message];