我正在使用 XMPPFramework 开发聊天应用程序
加入现有房间后如何接收消息历史记录?
现在我加入这样的房间:
XMPPJID *roomJid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@conference.%@",systemName,xmppServer]];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:[XMPPRoomHybridStorage sharedInstance] jid:roomJid];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:xmppStream];
NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
[history addAttributeWithName:@"maxstanzas" stringValue:@"100"];
[xmppRoom joinRoomUsingNickname:user.deviceUUID history:history];
我还阅读了 documentation 中的示例
根据这个例子,我也尝试通过这种方式加入房间:
XMPPJID *roomJid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@conference.%@",systemName,xmppServer]];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:[XMPPRoomHybridStorage sharedInstance] jid:roomJid];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:xmppStream];
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[presence addAttributeWithName:@"from" stringValue:[NSString stringWithFormat:@"bob@%@",xmppServer]];
[presence addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"%@@conference.%@/%@",systemName,xmppServer,user.deviceUUID]];
NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"http://jabber.org/protocol/muc"];
NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
[history addAttributeWithName:@"maxstanzas" stringValue:@"100"];
[x addChild:history];
[presence addChild:x];
[xmppRoom joinRoomUsingNickname:user.deviceUUID history:presence];
我成功加入房间,但没有收到之前消息的历史记录。
顺便说一句,如果房间中至少有一个用户,即使我加入房间,我也会收到所有之前的消息:
[xmppRoom joinRoomUsingNickname:user.deviceUUID history:nil];
如果所有用户都离开房间,然后一些用户再次加入 - 历史记录为空=(
我做错了什么? 我是否需要在服务器端打开某些设置来保存历史记录(例如日志记录)?
以及有关文档中示例的一些问题:
“from”参数是什么意思?这是否意味着我只要求该房间中用户 bob 的消息历史记录?如果我想接收所有历史记录(来自任何用户的消息)怎么办?
“id”参数是什么意思?我在文档中没有找到任何描述。
最佳答案
当您创建房间并加入后,您需要配置该房间以使其持久,这意味着:
持久房间 如果最后一个居住者退出,房间不会被摧毁;反义词:临时房间。 (您想要这个房间的配置)。
临时房间 如果最后一个居住者退出,房间就会被摧毁;反义词:执着室。
1.因此,您创建并加入一个房间。
XMPPJID *roomJid = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@conference.%@",systemName,xmppServer]];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:[XMPPRoomHybridStorage sharedInstance] jid:roomJid];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom activate:xmppStream];
[xmppRoom joinRoomUsingNickname:user.deviceUUID history:history];
2. 然后,调用委托(delegate)方法 xmppRoomDidJoin:sender;
(仅当一切顺利时),并且您必须配置您的房间
-(void)xmppRoomDidJoin:(XMPPRoom *)sender {
NSLog("I did join.");
[sender fetchConfigurationForm];
}
fetchConfigurationForm
方法发送 IQ 来请求初始房间配置表单。
已发送到 XMPP 服务器的 IQ 示例:
<iq from='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5734253839326617243f363c32242732362532793b3e23" rel="noreferrer noopener nofollow">[email protected]</a>/desktop'
id='create1'
to='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e4878b92818aa4878c8590ca978c858f81979481859681ca888d90" rel="noreferrer noopener nofollow">[email protected]</a>'
type='get'>
<query xmlns='http://jabber.org/protocol/muc#owner'/>
</iq>
3. 当 XMPP 服务器使用房间配置应答时,-xmppRoom:sender didFetchConfigurationForm:configForm;
方法被调用。 在这里您可以更改房间的默认值以使其持久化,房间名称,仅限成员等。
示例:
-(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];
}
关于iOS XMPPFramework - 房间/聊天消息历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28395335/