2011-02-14 80 views
23

我閱讀了一些例子並測試了它們,但是他們都需要先與某人開始聊天以接收傳入消息......我想要檢索此傳入消息,而無需先與jid交談,任何人都可以舉個例子嗎?如何使用Smack接收傳入的XMPP消息?

+0

你是什麼服務器? – 2016-04-26 13:24:49

回答

29

您需要註冊一個ChatListener通知新的聊天記錄,那麼你可以添加一個消息監聽器,他們像正常:

connection.getChatManager().addChatListener(new ChatManagerListenerImpl()); 

.... 

private class ChatManagerListenerImpl implements ChatManagerListener { 

    /** {@inheritDoc} */ 
    @Override 
    public void chatCreated(final Chat chat, final boolean createdLocally) { 
     chat.addMessageListener(...); 
    } 

} 
+1

@cris Smith hi! thx爲你的回覆:)請給一個完整的例子?我真的很麻煩:S – Winter 2011-02-14 17:00:35

+0

非常有用! addChatListener在登錄之前可以調用的信息。 – h3xStream 2011-04-22 04:04:01

+1

我使用了相同的方法,但離線消息沒有按順序收到。我該如何解決這個問題? – Max 2013-07-24 16:33:55

15

我只是想添加複製&粘貼示例:

// connect to server 
    XMPPConnection connection = new XMPPConnection("jabber.org"); 
    connection.connect(); 
    connection.login("user", "password"); // TODO: change user and pass 

    // register listeners 
    ChatManager chatmanager = connection.getChatManager(); 
    connection.getChatManager().addChatListener(new ChatManagerListener() 
    { 
    public void chatCreated(final Chat chat, final boolean createdLocally) 
    { 
     chat.addMessageListener(new MessageListener() 
     { 
     public void processMessage(Chat chat, Message message) 
     { 
      System.out.println("Received message: " 
      + (message != null ? message.getBody() : "NULL")); 
     } 
     }); 
    } 
    }); 

    // idle for 20 seconds 
    final long start = System.nanoTime(); 
    while ((System.nanoTime() - start)/1000000 < 20000) // do for 20 seconds 
    { 
    Thread.sleep(500); 
    } 
    connection.disconnect(); 

本示例連接到jabber.org並在控制檯上顯示每條收到的消息。

9

請找到下面的代碼。
請smack.jar & smackx.jar添加到您的構建路徑

import org.jivesoftware.smack.ConnectionConfiguration; 
import org.jivesoftware.smack.PacketCollector; 
import org.jivesoftware.smack.XMPPConnection; 
import org.jivesoftware.smack.filter.AndFilter; 
import org.jivesoftware.smack.filter.PacketFilter; 
import org.jivesoftware.smack.filter.PacketTypeFilter; 
import org.jivesoftware.smack.packet.Message; 
import org.jivesoftware.smack.packet.Packet; 

public class GoogleTalkDemo extends Thread{ 
    private XMPPConnection xmppConnection; 

    public void connect(String server, int port, String s) throws Exception { 
     xmppConnection = new XMPPConnection(new ConnectionConfiguration(server, port,s)); 
     xmppConnection.connect(); 
    } 

    public void disconnect(){ 
     if(xmppConnection != null){ 
      xmppConnection.disconnect(); 
      interrupt(); 
     } 
    } 

    public void login(String username, String password) throws Exception{ 
     connect("talk.google.com", 5222, "gmail.com"); 
     xmppConnection.login(username, password); 
    } 

    public void run(){ 
     try { 
      login("[email protected]", "your password"); 
      System.out.println("Login successful"); 
      listeningForMessages(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String args[]) throws Exception { 
     GoogleTalkDemo gtd = new GoogleTalkDemo(); 
     gtd.run(); 
    } 

    public void listeningForMessages() { 
     PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class)); 
     PacketCollector collector = xmppConnection.createPacketCollector(filter); 
     while (true) { 
      Packet packet = collector.nextResult(); 
      if (packet instanceof Message) { 
       Message message = (Message) packet; 
       if (message != null && message.getBody() != null) 
        System.out.println("Received message from " 
          + packet.getFrom() + " : " 
          + (message != null ? message.getBody() : "NULL")); 
      } 
     } 
    } 

} 
3
private MultiUserChat muc; /* Initialize muc */ 

private void listeningForMessages() 
    { 
     muc.addMessageListener(new PacketListener() { 
      public void processPacket(Packet packet) 
      { 
       final Message message = (Message) packet; 

        // Do your action with the message    
      } 
     }); 
    }