XMPP我需要通过XMPP聊天在特定的聊天对话中发送输入状态

时间:2018-08-20 07:31:24

标签: ios objective-c xmpp xmppframework

我的要求是两个用户有2个或2个以上不同的对话,但用户相同。因此,当一个人当时可以输入内容时,另一个人在与此相关的所有聊天对话中都处于输入状态。下面是我在Sent方法上的代码

-(void)xmpp_SendTypingNotification:(BOOL)isTyping toFriendID:(NSString*)recieverId{
    XMPPMessage * xMessage = [[XMPPMessage alloc] initWithType:@"chat" to:[XMPPJID jidWithString:strAddDomain(recieverId)]];
    isTyping?[xMessage addComposingChatState]:[xMessage addInactiveChatState];
    [self.xmppStream sendElement:xMessage];
}

或接收代码为

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
    if ([message isErrorMessage]) {
        return;
    }

    if ([message hasChatState]){
        if (!_arrTypingUsersIDs) {
            _arrTypingUsersIDs=[NSMutableArray new];
        }

        NSString *otherUserID=[NSString stringWithFormat:@"%@",[message.fromStr componentsSeparatedByString:@"@"][0]];

        NSInteger typingStatus=[message hasComposingChatState]?1:0;
        [self xmpp_setTypingStatus:typingStatus ofUser:otherUserID];

    }
}

2 个答案:

答案 0 :(得分:1)

您必须使用XEP-0085发送和接收聊天状态(例如正在输入或暂停输入)。

在XMPPFramework中,如果要发送正在键入聊天状态,只需发送一条消息,然后使用message.addaddComposingChatState()向消息中添加撰写状态。

当您在第二个客户端中收到消息时,可以使用message.hasChatStatemessage.message.hasComposingChatStatehasPausedChatState来检查消息是否处于聊天状态

答案 1 :(得分:1)

之所以如此工作,是因为您每次都将此状态发送给其他用户:

HttpResponse

,而不是在特定对话框的上下文中。

如果您进行1-1聊天,那么您的解决方案就是正确的选择。

如果您有群聊(使用XEP-0045),则需要将输入状态发送到群聊室JID:

LoginView

因此只有该特定聊天室中的用户才能收到您的状态

这里是XEP-0045 implementation at XMPPFramework

的链接