XMPPFramework “typing..” status for message

非 Y 不嫁゛ 提交于 2019-12-05 04:21:27

问题


I am using robbiehanson/XMPPFramework for my current project. How to get the message typing status using XMPPFramework? There XEP- 184 protocol but those are deprecated right now . Need assistance here for getting composing status in iOS . Regards, Bhat


回答1:


The most commonly used protocol for "contact is typing" notifications is XEP-0085: Chat State Notifications. As described in more detail there, the first message to a contact should contain an "active" state element (next to the <body/> element):

<active xmlns='http://jabber.org/protocol/chatstates'/>

If the contact responds with a chat state, the client can go ahead and use other states, such as "composing":

<composing xmlns='http://jabber.org/protocol/chatstates'/>

or "paused" (the user has entered text, but isn't actively typing):

<paused xmlns='http://jabber.org/protocol/chatstates'/>

or "inactive", and finally "gone" when the user ends the conversation by closing the chat window or similar.




回答2:


First you import:

#import "XMPPMessage+XEP_0085.h"

and then you add the following methods according to your purpose.

composing.....

- (void)sendComposingChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addComposingChatState];
    [xmppStream sendElement:message];
}

Active.....

- (void)sendActiveChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addActiveChatState];
    [xmppStream sendElement:message];
}

Inactive...

- (void)sendInactiveChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
     [xmppMessage addInactiveChatState];
    [xmppStream sendElement:message];
}

Gone...

- (void)sendExitChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addGoneChatState];
   [xmppStream sendElement:xmppMessage];
}

Paused...

- (void)sendPausedChatToUser:(XMPPJID*)jid {
    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type" stringValue:@"chat"];
    [message addAttributeWithName:@"to" stringValue:[jid full]];
    XMPPMessage *xmppMessage = [XMPPMessage messageFromElement:message];
    [xmppMessage addPausedChatState];
    [xmppStream sendElement:message];
}

then You should write following code in appdelgate. (didReceiveMessage) method.

Ex:

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
    if ([message isChatMessageWithBody]){
}
else{
if([message elementForName:@"composing"] != nil){



        } else if ([message elementForName:@"paused"] != nil) {



        } else if ([message elementForName:@"gone"] || [message elementForName:@"inactive"] ) {


        }
}


来源:https://stackoverflow.com/questions/25041025/xmppframework-typing-status-for-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!