2012-03-19 89 views
1

我想連接到使用smack API的openfire服務器,我無法這樣做。無法連接到Openfire服務器

下面是代碼:

public class Tests{ 

public static void main(String[] args) { 

    System.out.println("Starting IM client"); 

    // gtalk requires this or your messages bounce back as errors 
    ConnectionConfiguration connConfig = new ConnectionConfiguration("localhost", 5222); 
    XMPPConnection connection = new XMPPConnection(connConfig); 

    try { 
     connection.connect(); 
     System.out.println("Connected to " + connection.getHost()); 
    } catch (XMPPException ex) { 
     //ex.printStackTrace(); 
     System.out.println("Failed to connect to " + connection.getHost()); 
     System.exit(1); 
    } 
    try { 
     connection.login("[email protected]", "setup1"); 
     System.out.println("Logged in as " + connection.getUser()); 

     Presence presence = new Presence(Presence.Type.available); 
     connection.sendPacket(presence); 

    } catch (XMPPException ex) { 
     //ex.printStackTrace(); 
     System.out.println("Failed to log in as " + connection.getUser()); 
     System.exit(1); 
    } 
    connection.disconnect(); 
} 
} 

以下是輸出:

Starting IM client 
Connected to localhost 
Failed to log in as null 

似乎連接到服務器,但無法登錄

回答

2
connection.login("[email protected]", "setup1"); 

如果您的服務器在本地主機上啓動,則絕對不應該登錄到example.com域。 儘量只:

connection.login("test", "setup1"); 

但請記住,要能夠登錄,您需要有一個有效的用戶名和密碼。這意味着您必須在服務器上創建密碼爲「setup1」的用戶「test」。

+0

k ...這工作。所以如果服務器是在實際域中,那麼我應該使用[email protected]從客戶端設備登錄,對吧? – frewper 2012-03-19 10:16:36

+0

我認爲你總是可以用用戶名登錄,但[email protected]也應該可以工作。 – Maggie 2012-03-19 10:23:38

+0

在XMPP界面上,您總是需要一個域。我不知道這個API是否需要它,但我會虛心地暗示,爲了一致性,無論如何都使用域是個好主意。 – 2012-03-19 10:51:43