This article from http://blog.csdn.net/hellogv/, reference must indicate the source!
More and more people use agsxmpp. Recently, several netizens have asked how to set and respond to the online status of friends on the agsxmpp client.
There are two important points in agsxmpp to set the online status:
1. presencetype. Available and presencetype. unavailable. XMPP officially does not recommend presencetype. unavailable for stealth. Why? User A sends presencetype to user B (or all users. unavailable, which is equivalent to deprecation. It cannot be identified as "stealth" or "deprecation". Some partial-door tools like to display invisible users ....... Therefore, presencetype. unavailable is not recommended.
2. showtype. XXXXX. You don't need to talk about it. In itself, it is set to multiple online states ....
The following code shows how to tell a specific friend (multiple friends can be used in a loop) and their online status:
/// <Summary> <br/> /// tell the server/friends, my current visual status <br/> /// </Summary> <br/> /// <Param name = "jidto"> null indicates the Notification Server, if the parameter is not null, a friend is specified. </param> <br/> // <Param name = "visible"> 0: visible, 1: invisible, 2: unavailable </param> <br/> Public static void changmystate (string jidto, int visible) <br/>{< br/> presence P = new presence (); <br/> If (jidto! = NULL) <br/> P. to = new jid (jidto); <br/> If (visible = 0) // online, completely visible <br/>{< br/> P. type = presencetype. available; <br/> P. show = showtype. chat; <br/>}< br/> else if (visible = 1) // online, invisible <br/>{< br/> P. type = presencetype. available; <br/> P. show = showtype. away; <br/>}< br/> else if (visible = 2) // offline, invisible <br/>{< br/> P. type = presencetype. unavailable; <br/>}< br/> cls_config.xmppcon.send (p); <br/>}
The following code responds to the online status sent by a friend:
Delegate void onpresencedelegate (Object sender, presence PRES); <br/> /// <summary> <br/> // events triggered when the presence node is received, determine whether a user is triggered online or offline, and the user processes friend requests (reject/accept), processed by T <br/> /// </Summary> <br/> private void xmppcon_onpresence (Object sender, presence PRES) <br/>{< br/> If (uccontactlist. invokerequired) <br/>{< br/> uccontactlist. begininvoke (New onpresencedelegate (xmppcon_onpresence), new object [] {sender, Pres}); <br/> return; <br/>}< br/> // ****************** process friend requests ********** ************** // <br/> If (Pres. type = presencetype. subscribe) // receives a friend request <br/>{< br/> // queries whether the user accepts the friend request <br/>}< br/> else if (Pres. type = presencetype. subscribed) {}< br/> else if (Pres. type = presencetype. unsubscribe) {}< br/> else if (Pres. type = presencetype. unsubscribed) // when a "rejected request" or "failed" is received <br/>{</P> <p >}< br/> else // process a friend's online/offline event Processing <br/>{< br/> If (Pres. from. bare. equals (cls_config.xmppcon.myjid.bare) = false) // if it is not your own information, that is, the friend Information <br/>{< br/> // checks whether the friend does not exist in the friend list (onpresence is better than onrosteritem to add friends ), listitem <br/> If (cls_config.alfriendslist.indexof (Pres. from. bare) =-1) <br/>{< br/> // Add to friend list <br/>}< br/> // determine the current status of the friend, online or offline <br/> If (Pres. type = presencetype. available & Pres. show = showtype. chat) // if a friend is online and in chat status <br/>{}< br/> else if (Pres. type = presencetype. available & Pres. show = showtype. away) // if a friend is online and in the away status <br/>{}< br/> else if (Pres. type = presencetype. unavailable) // if a friend is offline <br/>{}< br/>}< br/>}
These two statements are the key to judgment:
If (Pres. type = presencetype. Available & Pres. Show = showtype. Chat) // if a friend is online and in chat status
{}
Else if (Pres. type = presencetype. Available & Pres. Show = showtype. Away) // if a friend goes online and is in the away status
{}