Java XMPP客户端登录失败

时间:2015-04-02 18:56:55

标签: java client xmpp ejabberd sasl

我试图让客户端程序正常工作 我在我的Raspberry Pi上工作了ejabberd aleady 我可以用Pidgin成功写消息 所以现在我的问题是我的客户端可以连接到服务器,但身份验证总是失败 继承我的经理班:

package com.javacodegeeks.xmpp;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;

public class XmppManager {

private static final int packetReplyTimeout = 500; // millis

private String server;
private int port;

private ConnectionConfiguration config;
private XMPPConnection connection;

private ChatManager chatManager;
private MessageListener messageListener;

public XmppManager(String server, int port) {
    this.server = server;
    this.port = port;
}

public void init() throws XMPPException {

    System.out.println(String.format("Initializing connection to server %1$s         port %2$d", server, port));

    SmackConfiguration.setPacketReplyTimeout(packetReplyTimeout);

    config = new ConnectionConfiguration(server, port, "localhost");
    config.setSASLAuthenticationEnabled(false);
    config.setSecurityMode(SecurityMode.disabled);

    connection = new XMPPConnection(config);
    connection.connect();

    System.out.println("Connected: " + connection.isConnected());

    chatManager = connection.getChatManager();
    messageListener = new MyMessageListener();

}

public void performLogin(String username, String password) throws XMPPException {
    if (connection!=null && connection.isConnected()) {
        connection.login(username, password);
    }
}

public void setStatus(boolean available, String status) {

    Presence.Type type = available? Type.available: Type.unavailable;
    Presence presence = new Presence(type);

    presence.setStatus(status);
    connection.sendPacket(presence);

}

public void destroy() {
    if (connection!=null && connection.isConnected()) {
        connection.disconnect();
    }
}

public void sendMessage(String message, String buddyJID) throws XMPPException {
    System.out.println(String.format("Sending mesage '%1$s' to user %2$s", message, buddyJID));
    Chat chat = chatManager.createChat(buddyJID, messageListener);
    chat.sendMessage(message);
}

public void createEntry(String user, String name) throws Exception {
    System.out.println(String.format("Creating entry for buddy '%1$s' with name %2$s", user, name));
    Roster roster = connection.getRoster();
    roster.createEntry(user, name, null);
}

class MyMessageListener implements MessageListener {

    @Override
    public void processMessage(Chat chat, Message message) {
        String from = message.getFrom();
        String body = message.getBody();
        System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
    }

}

}

这是我的测试类

package com.javacodegeeks.xmpp;

public class XmppTest {

public static void main(String[] args) throws Exception {

    String username = "nico";
    String password = "nico";

    XmppManager xmppManager = new XmppManager("192.168.0.110", 5222);

    xmppManager.init();
    xmppManager.performLogin(username, password);
    xmppManager.setStatus(true, "Hello everyone");

    String buddyJID = "test";
    String buddyName = "test";
    xmppManager.createEntry(buddyJID, buddyName);

    xmppManager.sendMessage("Hello mate", "test@localhost");

    boolean isRunning = true;

    while (isRunning) {
        Thread.sleep(50);
    }

    xmppManager.destroy();

}

}

它给出了这个输出:

Initializing connection to server 192.168.0.110 port 5222
Connected: true
Exception in thread "main" service-unavailable(503)
at     org.jivesoftware.smack.NonSASLAuthentication.authenticate(NonSASLAuthentication.java:78)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:404)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
at com.javacodegeeks.xmpp.XmppManager.performLogin(XmppManager.java:57)
at com.javacodegeeks.xmpp.XmppTest.main(XmppTest.java:13)

任何想法/解决方案?

0 个答案:

没有答案