为什么我的状态为“订阅”?

时间:2013-08-19 08:34:16

标签: java xmpp smack

名为tester的用户在其名单中添加了另一位用户sanikasanika的订阅模式设为 Roster.SubscriptionMode.accept_all 即可。但是当tester检查sanika的状态时,他会subscribe。为什么会这样?

在函数main中,sanika设置其状态,在函数connectTester测试器中,尝试获取sanika的状态。

     public static void main(String[] args) {
         try {
             Connection connection = new XMPPConnection("localhost");
             connection.connect();
             connection.login("sanika", "tester");

             Roster r = connection.getRoster();

             r.setSubscriptionMode(Roster.SubscriptionMode.accept_all);

             Presence p = new Presence(Presence.Type.available);
             p.setStatus("Having Lunch :)");

             connection.sendPacket(p);

             connectTester();

             Thread.sleep(30000);

         }   catch(Exception exc) {
             exc.printStackTrace();
             }   
    }

    public static void connectTester() {
        try {
            Connection connection = new XMPPConnection("localhost");
            connection.connect();
            connection.login("tester", "tester");

            Roster r = connection.getRoster();
            RosterEntry re = r.getEntry("sanika@sanika.com");
            System.out.println(re.getStatus().toString()); 
            // PRINTS SUBSCRIBE

        }catch(Exception exc) {}

我在哪里犯了错误?

注意: testersanika添加为:

        String group[] = {"Friend List"};
        r.createGroup("Friend List");
        r.createEntry("sanika@sanika.com", "sanika", group);

1 个答案:

答案 0 :(得分:0)

我将两个用户添加到彼此的名单中,如下所示

//subscribe to eachother
connection.login("tester", ...); 
Presence subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo("sanika@sanika.com");
connection.sendPacket(subscribe);
connection.disconnect();
...                
connection.connect();
connection.login("sanika", ...); 
subscribe = new Presence(Presence.Type.subscribe);
subscribe.setTo("tester@localhost");
connection.sendPacket(subscribe);
connection.disconnect();

另请注意,您似乎同时使用sanika@sanika.comsanika@localhostlogin中的第一个main - 语句