无法通过ASmack发送或接收在线状态

时间:2012-09-03 13:06:45

标签: java android xmpp smack asmack

我正在尝试为Android开发一个XMPP聊天客户端(使用连接到C#/ Unity的Java)。我有Java - > Unity / C#连接完美运行。我还下载了Asmack,可以生成一个库和我的包装类,用于初始化与OpenFire XMPP服务器的连接。但是,我似乎无法获得Presence发送或接收。我可以登录,注册新用户并填充他们的名单,发送消息,但不能发送任何状态。

代码会自动注册之前从未使用过该应用的用户。它还使用预设的朋友列表,并自动填充这些朋友的名单。

代码如下(抱歉所有调试行,使用Unity时不能使用断点):

public class ASmackWrapper
{
    private XMPPConnection connection;
    private String[] friends;
    private static final String eventClass = "ASmackEventListener";
    private static ASmackWrapper wrapper;

    public static ASmackWrapper instance()
    {
        System.out.println("instancecreator of ASmackWrapper 1!");
        if (wrapper == null)
            wrapper = new ASmackWrapper();
        return wrapper;
    }

    public ASmackWrapper()
    {
        System.out.println("constructor of ASmackWrapper");
    }

    public boolean tryToRegister(String user, String pass){
        AccountManager acManager = connection.getAccountManager();
        try {
            Map<String, String> attributes = new HashMap<String,String>();
            attributes.put("email", "MY email");
            acManager.createAccount(user, pass,attributes);
        } catch (XMPPException e) {
            System.out.println("cant autoregister user "+ user +" ... with pass: "+pass+" on server. error:" + e.getLocalizedMessage());
            if (e.getLocalizedMessage().contains("conflict"))
                return false; // Wrong password, since there is already an account with that id!
            return false;
        }
        return true;
    }

    public void setFriends(String[] _friends) {
        friends = _friends;
    }

    public void start(String host, String user, String pass)
    {
        System.out.println("Java: openConenction host:"+host);
        ConnectionConfiguration cc = new ConnectionConfiguration(host,5222);
        //cc.setSendPresence(true);
        this.connection = new XMPPConnection(cc);
        Connection.DEBUG_ENABLED = true;
        try {
            this.connection.connect();
        } catch (XMPPException e) {
            System.out.println("Error connecting to server");
            return;
        }

        if(!this.connection.isConnected()) {
            System.out.println("Java: is not connected");
            onError("Connection failed");
            return;
        }

        boolean loginStatus = login(user, pass);
        if (!loginStatus) {
            onError("Login Failed");
            return;
        }

        RosterListener rl = new RosterListener() {
            public void entriesAdded(Collection<String> addresses) {}
            public void entriesUpdated(Collection<String> addresses) {}
            public void entriesDeleted(Collection<String> addresses) {}
            public void presenceChanged(Presence presence) {
                System.out.println("presence changed!" + presence.getFrom() + " "+presence.getStatus());
                onPresence(presence);
            }
        };
        if (connection.getRoster() != null) {
            connection.getRoster().setSubscriptionMode(Roster.SubscriptionMode.accept_all);

            System.out.println("7");
            connection.getRoster().addRosterListener(rl);
        }

        onAuthenticate("");
        System.out.println("10");

        //Set presence to online!
        Presence presence = new Presence(Presence.Type.available);
        presence.setStatus("Online, Programmatically!");
        presence.setPriority(24);
        presence.setMode(Presence.Mode.available);
        connection.sendPacket(presence);
    }

    private void addFriends() throws Exception {
        if (friends == null) {
            System.out.println("No friends to add");
            return;
        }
        System.out.println("Number of friends to add: "+friends.length);
        for (int i = 0;i<friends.length;i++) {
            System.out.println("Create user in roster: "+friends[i]);
            connection.getRoster().createEntry("fb"+friends[i], "No name_",null);
        }
    }

    private boolean login(String jid, String password) {
        System.out.println("1");
        boolean isLoggedIn=true;
        try { 
            this.connection.login(jid, password);
        } catch (XMPPException e) {
            isLoggedIn=false;
        }

        System.out.println("2");
        if(!isLoggedIn) {
            boolean isRegistred = tryToRegister(jid,password);
            if (isRegistred) {
                connection.disconnect();
                try {
                    connection.connect();
                    connection.login(jid, password); 
                } catch (XMPPException e) {
                    onError("Could not connect and login after registring");
                    return false;
                }
            } else {
                return false;
            }
        } 

        try {
            addFriends();
        } catch (Exception e) {
            onError("Could not add friends to roster");
        }
        ChatManager chatmanager = connection.getChatManager();

        chatmanager.addChatListener(new ChatManagerListener()
        {
            public void chatCreated(final Chat chat, final boolean createdLocally)
            {
                System.out.println("OK Chat created!");
                chat.addMessageListener(new MessageListener()
                {
                    public void processMessage(Chat chat, Message message)
                    {
                        onMessage(chat, message);
                    }
                });
            }
        });

        return true;
    }

    public void sendMessage(String rec, String message) {
        System.out.println("sendMessage(string,string) to host :"+connection.getHost());
        Chat chat = connection.getChatManager().createChat(rec+"@"+connection.getHost(), new MessageListener() {
            public void processMessage(Chat chat, Message message) {
                // Print out any messages we get back to standard out.
                System.out.println("Probably an error, since we got a instant reply on sent message. Received message body: " + message.getBody() + " from:"+message.getFrom() + " to:"+message.getTo());
            }
        });
        try {
            chat.sendMessage(message);
            System.out.println("Message sent");
        } catch (XMPPException e) {
            System.out.println("Error sending message: "+e.toString());
            e.printStackTrace();

        }
    }

    public void logout () {
        System.out.println("Login out...");
        connection.disconnect();
    }

    public void getOnlineFriends() {
        Roster roster = connection.getRoster(); 
        Collection<RosterEntry> entries = roster.getEntries();

        for(RosterEntry rosterEntry: entries) {
            String user = rosterEntry.getUser();
            Presence presence = roster.getPresence(user);
            System.out.println("Presence : "+presence);                                     
            System.out.println("Presence type: "+presence.getType());                
            System.out.println("Presence mode: "+presence.getMode());
        }

        //Set presence to online!
        Presence presence = new Presence(Presence.Type.available);
        presence.setStatus("Online, Programmatically!");
        presence.setPriority(24);
        presence.setMode(Presence.Mode.available);
        connection.sendPacket(presence);
    }

    private void onMessage(Chat chat, Message message) {
        String m = ("Received message: " + (message != null ? message.getBody() : "NULL"));
        System.out.println(m);

        UnityPlayer.UnitySendMessage(eventClass, "Message", m);
    }

    private void onError(String message) {
        UnityPlayer.UnitySendMessage(eventClass, "Error", message);
    }

    private void onAuthenticate(String message) {
        UnityPlayer.UnitySendMessage(eventClass, "Authenticate", message);
    }

    private void onPresence(Presence presence) {
        String user = presence.getFrom();
        if (presence.getType() == Presence.Type.available)
            UnityPlayer.UnitySendMessage(eventClass, "Online", user);
        else
            UnityPlayer.UnitySendMessage(eventClass, "Offline", user);

            System.out.println("Java: Presence changed, from:" +presence.getFrom() + " type:"+presence.getType() + " toString:"+presence.toString());
    }


}

通过设置状态监听器和登录后获取状态,以两种方式检查状态。这个SO页面建议在获得前置之前等待5秒:Unable to get presence of roster by using smack, openfire 我也尝试过这样,通过从按钮调用getOnlineFriends,登录后超过5秒。听众永远不会被召唤。它只是在登录后触发一次,对于名单中的每一个,其中一个存在= null。

编辑:在Asmack上打开调试模式后,我看到以下回复我的状态发送消息:

SENT <presence id="3sG7l-11"><status>Online, Programmatically!</status><priority>24</priority></presence>

RCV <presence id="6s7BX-5" to="787122012@xxx.tripnet.se/Smack" from="10000063946242" type="error">
<error code="404" type="cancel">
<remote-server-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error></presence>

服务器日志:

org.jivesoftware.openfire.nio.ConnectionHandler - Closing connection due to error while processing message:
<iq id="566-4" type="error" from="xxx.tripnet.se/f55aea72" to="xxx.tripnet.se">
<error /><error type="cancel" code="501">
<feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" /></error>
<ping xmlns="urn:xmpp:ping" /></iq> 

java.lang.IllegalArgumentException: IQ must be of type 'set' or 'get'. Original IQ:
<iq id="566-4" type="error" from="xxx.tripnet.se/f55aea72" to="xxx.tripnet.se">
<error/><error type="cancel" code="501">
<feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
<ping xmlns="urn:xmpp:ping"/></iq> 
at org.xmpp.packet.IQ.createResultIQ(IQ.java:384) 

我也尝试将setSendPresence = true传递给connect()中传递的连接配置,但没有区别。

我还试图在OpenFire服务器中手动将订阅模式设置为两个用户的“both”(来自“none”),但没有效果。

2 个答案:

答案 0 :(得分:3)

搞定了!这可能是因为我没有在我的jid列表中使用正确的格式。正确的格式必须是 user@myserver.se ,而不仅仅是用户

答案 1 :(得分:1)

编写一个RosterListener,看它是否有效。

public void rosterOnlineStatus(){
    Roster roster = connection.getRoster();
    Presence status = new Presence(Type.available);
    status.setStatus("Hello This is Phaneendra");
    roster.addRosterListener(new RosterListener() {

        @Override
        public void presenceChanged(Presence presence) {

            System.out.println(presence.getFrom()+ "is "+presence+" "+presence.getStatus());
            presence.getStatus();


        }

        @Override
        public void entriesUpdated(Collection<String> addresses) {}

        @Override
        public void entriesDeleted(Collection<String> addresses) {}

        @Override
        public void entriesAdded(Collection<String> addresses) {}
    });
}

看看添加RosterListener是否有效?