gpt4 book ai didi

ios - 无法在多用户聊天中发送或接收消息 - XMPPFramework

转载 作者:行者123 更新时间:2023-11-29 12:05:34 27 4
gpt4 key购买 nike

我在 Openfire 服务器上创建了一个组和两个用户。我能够使用 XMPPFramework 对两个用户进行身份验证,但无法在他们之间交换消息。我可以看到两个用户都加入了 Openfire 服务器上的群组。

使用以下服务器详细信息:

#define kHostName @"Some Server URL"
#define kServerName @"Some Server Name"
#define kRoomAddress @"testchatroom@conference.Some Server Name"

使用以下代码进行流设置和创建房间:

- (void)setupStream {

NSString *jabberID = [UserDefaults stringForKey:kUserID];
NSString *myPassword = [UserDefaults stringForKey:kUserPassword];

[DDLog addLogger:[DDTTYLogger sharedInstance]];

self.xmppStream = [[XMPPStream alloc] init];
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
self.xmppStream.hostName = kHostName;
self.xmppStream.hostPort = OptionPort;

NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];
self.xmppStream.myJID = [XMPPJID jidWithString:userName];
password = myPassword;

NSError *error = nil;
if (OptionOldSchoolSSL)
[self.xmppStream oldSchoolSecureConnectWithTimeout:timeOut error:&error];
else
[self.xmppStream connectWithTimeout:timeOut error:&error];
}

#pragma mark -
#pragma mark XMPP delegates

- (void)xmppStreamDidConnect:(XMPPStream *)sender {

isOpen = YES;

Class authClass = nil;
if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodPlain])
authClass = [XMPPPlainAuthentication class];
else if ([OptionAuthenticationMethod isEqual:XMPPAuthenticationMethodDigestMD5])
authClass = [XMPPDigestMD5Authentication class];
else {
NSLog(@"Unrecognized auhthentication method '%@', falling back on Plain",
OptionAuthenticationMethod);
authClass = [XMPPPlainAuthentication class];
}
id<XMPPSASLAuthentication> auth = [[authClass alloc] initWithStream:sender
password:password];
NSError *error = nil;
if (![sender authenticate:auth error:&error])
NSLog(@"Error authenticating: %@", error);
else
NSLog(@"Authenticated !!");
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
[self createRoom];
}

- (void)createRoom {

self.roomStorage = [[XMPPRoomMemoryStorage alloc] init];
XMPPJID *roomJID = [XMPPJID jidWithString:kRoomAddress];
self.xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:self.roomStorage
jid:roomJID
dispatchQueue:dispatch_get_main_queue()];

[self.xmppRoom activate:self.xmppStream];
[self.xmppRoom addDelegate:self
delegateQueue:dispatch_get_main_queue()];

[self.xmppRoom joinRoomUsingNickname:self.xmppStream.myJID.user
history:nil
password:nil];
}

#pragma mark XMPPRoom delegates

- (void)xmppRoomDidCreate:(XMPPRoom *)sender {
NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)xmppRoomDidJoin:(XMPPRoom *)sender {
NSLog(@"%s", __PRETTY_FUNCTION__);

NSString *jabberID = [UserDefaults stringForKey:kUserID];
NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];
XMPPJID *jid = [XMPPJID jidWithString:userName];

[sender inviteUser:jid withMessage:nil];
}

- (void)xmppRoom:(XMPPRoom *)sender didFetchConfigurationForm:(NSXMLElement *)configForm
{
NSXMLElement *newConfig = [configForm copy];
NSArray *fields = [newConfig elementsForName:@"field"];

for (NSXMLElement *field in fields)
{
NSString *var = [field attributeStringValueForName:@"var"];
// Make Room Persistent
if ([var isEqualToString:@"muc#roomconfig_persistentroom"]) {
[field removeChildAtIndex:0];
[field addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];
}
}
[sender configureRoomUsingOptions:newConfig];
}

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];

NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];

if (msg != nil && from != nil) {
[m setObject:msg forKey:@"msg"];
[m setObject:from forKey:@"sender"];
[_messageDelegate newMessageReceived:m];
}
}

使用以下代码发送消息:

if ([messageStr length] > 0) {
NSString *jabberID = [UserDefaults stringForKey:kUserID];
NSString *userName = [NSString stringWithFormat:@"%@@%@", jabberID, kServerName];

XMPPMessage * message = [[XMPPMessage alloc] init];
[message addAttributeWithName:@"body" stringValue:messageStr];
[message addAttributeWithName:@"sender" stringValue:userName];

[DELEGATE.xmppClient.xmppRoom sendMessage:message];

self.txtMessageField.text = @"";

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:[messageStr substituteEmoticons] forKey:@"msg"];
[m setObject:@"you" forKey:@"sender"];
[m setObject:[NSString getCurrentTime] forKey:@"time"];

[self.messages addObject:m];
[self.messagesTableView reloadData];
}

最佳答案

构建 XMPPMessage 的方式不正确。 xmpp 消息的主体是元素而不是属性。

试试这个:

XMPPMessage *xmppMessage = [[XMPPMessage alloc] initWithType:@"groupchat" to:[XMPPJID jidWithString:@"user@server.com"]];
[xmppMessage addBody:@"Hi there"];

如果你想添加额外的参数:

XMPPElement *extraElement = [XMPPElement elementWithName:@"sender" stringValue:@"you"];
[xmppMessage addChild:extraElement];

然后发送:

[xmppRoom sendMessage:message];

我建议您实现didFailToSendMessage 委托(delegate),并在发送消息后检查您是否收到错误:

- (void)xmppStream:(XMPPStream *)sender didFailToSendMessage:(XMPPMessage *)message error:(NSError *)error {}

关于ios - 无法在多用户聊天中发送或接收消息 - XMPPFramework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com