我创建了一个Xmpp聊天应用,在其中我实现了一对一和群聊。聊天本身运行得很好。问题出在群聊中。我创建了一个有2-3名成员的群组,同样,聊天工作正常,但当我终止应用程序并重新启动它时,我无法从我创建的任何群组中获得群组消息。当我连接到XMPP服务器并重新加入任何组时,我会收到消息。我的问题是,每次我完全关闭应用程序后,我都必须再次加入群组。
请告诉我,当我从killed状态打开应用程序时,如何获取消息或自动加入群。
发布于 2015-05-21 17:45:33
一旦应用程序启动或从后台退出,您就需要将状态发送到XMPP
服务器。因此XMPP
服务器了解相应group
已准备好处理事件。
编辑:您可以使用以下代码发送在线状态。
- (void)goOnline {
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
NSXMLElement *show = [NSXMLElement elementWithName:@"show"
stringValue:@"dnd"];
NSXMLElement *status = [NSXMLElement elementWithName:@"status" stringValue:@"available"];
NSXMLElement *priority = [NSXMLElement elementWithName:@"priority" stringValue:@"24"];
[presence addChild:show];
[presence addChild:status];
[presence addChild:priority];
[_xmppStream sendElement:presence];
[self createOrJoinRoom];
}
- (void)createOrJoinRoom {
if ([appDelegate.xmppStream isConnected]) {
NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:@"XMPPUserId"];
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[presence addAttributeWithName:@"from" stringValue:[[appDelegate.xmppStream myJID]full]];
[presence addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"%@@%@/%@", @"newone", GroupChatRoomName,myJID]];
NSXMLElement *xelement = [NSXMLElement elementWithName:@"x" xmlns:XMPPMUCNamespace];
[presence addChild:xelement];
[appDelegate.xmppStream sendElement:presence];
}
}
希望这对你有帮助。
发布于 2015-05-29 13:50:49
您必须加入所有以前加入/连接的组。因为在iOS中,如果你杀死了你的应用程序,那么你就离开了你创建的或加入的群组。
所以每次在这段代码中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
你必须再次加入你的小组。
下面是它的演示代码:
XMPPRoomHybridStorage *xmppRoomStorage1 = [XMPPRoomHybridStorage sharedInstance];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:xmppRoomStorage1 jid:RoomName];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:appDelegate.Obj_xmppManager.xmppStream];
NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
[history addAttributeWithName:@"maxstanzas" stringValue:@"1"];
[xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user history:nil];
发布于 2015-05-25 22:36:35
除了在线状态的答案之外,我还会检查更基本的东西-
在一些服务器上,房间默认情况下是临时的-你可以通过连接两个独立的客户端来检查这一点,发送一些消息,只断开其中一个客户端,然后重新连接-如果你确实看到了在重新连接的客户端上发送的消息-这可能是你的问题,你能在创建房间时显示你传递给服务器的参数吗?
https://stackoverflow.com/questions/30370078
复制相似问题