【问题标题】:iOS XMPPFramework - Room / chat messages historyiOS XMPPFramework - 房间/聊天消息历史
【发布时间】:2015-04-08 08:10:16
【问题描述】:

我正在使用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”参数?我在文档中没有找到任何描述。

【问题讨论】:

    标签: ios xmpp xmppframework


    【解决方案1】:

    创建房间并加入后,您需要将该房间配置为持久,这意味着:

    永久房间 如果最后一个住户离开,房间不会被破坏;反义词:临时房间。 (你想要这个房间的配置)。

    临时房间 如果最后一个住户离开,房间就会被摧毁;反义词:永久房间。

    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.然后,委托方法xmppRoomDidJoin:sender;被调用(只有在一切顺利的情况下),你必须配置你的房间

    -(void)xmppRoomDidJoin:(XMPPRoom *)sender {
       NSLog("I did join.");
       [sender fetchConfigurationForm];
    }
    

    fetchConfigurationForm 方法发送 IQ 以请求初始房间配置表。

    已发送到 XMPP 服务器的 IQ 示例:

    <iq from='crone1@shakespeare.lit/desktop'
        id='create1'
        to='coven@chat.shakespeare.lit'
        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];
    }
    

    【讨论】:

    • 谢谢回复,我今天试试
    【解决方案2】:

    感谢@Moral 的解释。 但就我而言,解决方案非常简单。

    ejabberd.yml的聊天服务器中刚刚在模块muc配置中添加了默认选项:

    mod_muc: ## host: "conference.HOST" 
    db_type: odbc 
    access: muc
    access_create: muc_create
    access_persistent: muc_create
    access_admin: muc_admin
    min_message_interval: 1.0
    min_presence_interval: 5.0
    default_room_options:
    logging: true
    persistent: true
    

    并以这种方式在应用程序中加入房间:

    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];
    

    就是这样!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 2015-10-25
      • 2013-02-16
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多