Hi ,
~at first, pardon me for my influent english writing
I get stuck implementing small client chat program T_T
Here’s the thing: userA & userB are already registered in jabber server. userA wants to add userB to be one of his contact list and wants userB to approve userA’s contact invitation. AFAIK, we need to send 2 kind of packets: RosterPacket and Presence. the RosterPacket is not a problem, it works (roster list for each user is updated correcltly). But the presence, i really have no idea . I’m convinced that if we send Presence packet to the server that notifying userA needs userB to approve userA’s invitation, then (when userB log-in) userB will receive invitation of notification from userA which userB needs to approve/disapprove.
In fact, userB doesn’t get any notification of invitation at all. In source code view, the PresenceListener (see code below) doesn’t get any incoming packet.
anyway, these are my source code which implement this feature:
//add to contact(roster) list
RosterPacket.Item rosterItem = new RosterPacket.Item(userAccount,usernameOnly);
rosterItem.addGroupName(“Default Friend”);
rosterItem.setItemType(RosterPacket.ItemType.to);
rosterItem.setItemStatus(RosterPacket.ItemStatus.SUBSCRIPTION_PENDING);
//send the RosterPacket to update roster list in jabber database
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
rosterPacket.addRosterItem(rosterItem);
rosterPacket.toXML();
connection.sendPacket(rosterPacket);
//send Presence packet to targeted user to get their approval.
Presence presence = new Presence(Presence.Type.subscribe);
presence.setTo(userAccount);
String destination = connection.getUser();
presence.setFrom(destination);
presence.toXML();
connection.sendPacket(presence);
/*
- Listen to check whether any Presence packet coming or not
*/
PresenceListener packetPresenceListener = new PresenceListener();
PacketFilter packetFilter = new PacketTypeFilter(Presence.class);
connection.addPacketListener(packetPresenceListener, packetFilter);
/*
- Presence Listener class --> listen to Presence packet only
*/
class PresenceListener implements PacketListener{
public void processPacket(Packet incomingPacket) {
if(incomingPacket instanceof Presence){
Presence thisPresence = (Presence) incomingPacket;
thisPresence.getFrom();
thisPresence.getMode();
thisPresence.getType();
thisPresence.getTo();
System.out.println(thisPresence.getTo()+" has invited you ("+thisPresence.getFrom()+") to authorize his/her request or not");
System.out.println("Presence status: "+thisPresence.getType()+"; Presence mode: "+thisPresence.getMode());
}
}
}
Well, can anyone help me what’s wrong with my source code? or anything that i miss?
help…!!!
thx