如何在smack API中处理添加请求

时间:2009-08-26 12:20:12

标签: smack

我使用Smack API编写我的Google Talk客户端。现在我需要处理添加请求。 我将SubscriptionMode设置为手动&现在我必须注册一个存在数据包的监听器,但我不知道如何! 任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:9)

我还没有尝试过,但我想下面应该可行。如果使用手动模式,则应注册PacketListener以侦听类型为Presence.Type.subscribe的Presence数据包。

首先设置名单:

    Roster roster = connection.getRoster();
    roster.setSubscriptionMode(Roster.SubscriptionMode.manual);

然后将数据包列表器添加到上述连接中,例如:

connection.addPacketListener(new SubscriptionListener(), new PacketFilter(){

                public boolean accept(Packet packet) {
                    if(packet instanceof Presence)
                        if(((Presence)packet).getType().equals(Presence.Type.subscribe))
                            return true;
                    return false;
                }});

上述代码只对所有请求返回true,但您可以自定义它,即根据用户GUI输入将其设置为true或false。

答案 1 :(得分:5)

public static void admitFriendsRequest() {
         connection.getRoster().setSubscriptionMode(
         Roster.SubscriptionMode.manual);
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet paramPacket) {
                System.out.println("\n\n");
                if (paramPacket instanceof Presence) {
                    Presence presence = (Presence) paramPacket;
                    String email = presence.getFrom();
                    System.out.println("chat invite status changed by user: : "
                            + email + " calling listner");
                    System.out.println("presence: " + presence.getFrom()
                            + "; type: " + presence.getType() + "; to: "
                            + presence.getTo() + "; " + presence.toXML());
                    Roster roster = connection.getRoster();
                    for (RosterEntry rosterEntry : roster.getEntries()) {
                        System.out.println("jid: " + rosterEntry.getUser()
                                + "; type: " + rosterEntry.getType()
                                + "; status: " + rosterEntry.getStatus());
                    }
                    System.out.println("\n\n\n");
                    if (presence.getType().equals(Presence.Type.subscribe)) {
                        Presence newp = new Presence(Presence.Type.subscribed);
                        newp.setMode(Presence.Mode.available);
                        newp.setPriority(24);
                        newp.setTo(presence.getFrom());
                        connection.sendPacket(newp);
                        Presence subscription = new Presence(
                                Presence.Type.subscribe);
                        subscription.setTo(presence.getFrom());
                        connection.sendPacket(subscription);

                    } else if (presence.getType().equals(
                            Presence.Type.unsubscribe)) {
                        Presence newp = new Presence(Presence.Type.unsubscribed);
                        newp.setMode(Presence.Mode.available);
                        newp.setPriority(24);
                        newp.setTo(presence.getFrom());
                        connection.sendPacket(newp);
                    }
                }

            }
        }, new PacketFilter() {
            public boolean accept(Packet packet) {
                if (packet instanceof Presence) {
                    Presence presence = (Presence) packet;
                    if (presence.getType().equals(Presence.Type.subscribed)
                            || presence.getType().equals(
                                    Presence.Type.subscribe)
                            || presence.getType().equals(
                                    Presence.Type.unsubscribed)
                            || presence.getType().equals(
                                    Presence.Type.unsubscribe)) {
                        return true;
                    }
                }
                return false;
            }
        });

        connection.getRoster().addRosterListener(new RosterListener() {
            public void presenceChanged(Presence presence) {
                System.out.println(presence.getFrom() + "presenceChanged");

            }

            public void entriesUpdated(Collection<String> presence) {
                System.out.println("entriesUpdated");

            }

            public void entriesDeleted(Collection<String> presence) {
                System.out.println("entriesDeleted");

            }

            public void entriesAdded(Collection<String> presence) {
                System.out.println("entriesAdded");
            }
        });
    }