问题描述:

I’m using Openfire XMPP server for a android chat application. I can connect and login to the server using my app.

Now I want to know how to send friend request and accept friend request in this chat application.

I can fetch all the user from server but I want to know the process of starting a chat.

Thanks in advance.

网友答案:

The processes are a bit more complex than it looks to add/accept friend requests you need to do some juggling with presence and and roster iq stanzas.

I'll try to give you an indication about what stanzas are sent/received using contacts operations below.

1. Add contact
    To add a contact you need to send a `subscribe` stanza. 
    <presence xmlns="jabber:client" to="[email protected]/123" id="9VO8j-1" type="subscribe" from="[email protected]/1234" />
    1.1. Request accepted
        When adding a contact and the other app "accepts the request" you receive two stanzas:
            1. <presence xmlns="jabber:client" from="[email protected]/123" id="9VO8j-1" type="subscribed" to="[email protected]/1234" />
            2. <presence xmlns="jabber:client" from="[email protected]/123" id="9VO8j-2" type="subscribe" to="[email protected]/1234" />
            3. <presence xmlns="jabber:client" from="[email protected]/1234" id="9VO8j-3" type="subscribed" to="[email protected]/123" />

    1.2. Request Denied
        When the request is denied you receive an "UNSUBSCRIBED" stanza.
            Example: <presence xmlns="jabber:client" from="[email protected]/123" id="9VO8j-1" type="unsubscribed" to="[email protected]/1234" />

2. Accept contact request
    1. <presence xmlns="jabber:client" from="[email protected]/1234" id="9VO8j-1" type="subscribed" to="[email protected]/123" />
    2. <presence xmlns="jabber:client" from="[email protected]/1234" id="9VO8j-2" type="subscribe" to="[email protected]/123" />

3. Deny contact request
    <presence xmlns="jabber:client" from="[email protected]/1234" id="9VO8j-1" type="unsubscribed" to="[email protected]/123" />

4. Remove contact
    When you "Delete" a contact you should be in fact be "deleting" it locally in your app and from the roster. 
    This is done by sending two stanzas:
        1. <presence xmlns="jabber:client" from="[email protected]/1234" id="9VO8j-3" type="unsubscribe" to="[email protected]/123"/>
        2. <iq from='[email protected]/1234' id='ah382g67' to='[email protected]/123' type='set'>
             <query xmlns='jabber:iq:roster' ver='ver34'>
               <item jid='[email protected]/123' subscription='remove'/>
             </query>
            </iq>

Note: The use-cases described above do not include all the iq:roster stanzas associated to the presence stanzas. These are sent automatically by the server whenever a contact changes the subscription and ask types.

For a better understanding of how the these workflows work in details I suggest you read the latest RFC (implemented by your server). You can find it on the office site.

See section 3. Managing the roster.

相关阅读:
Top