24

我阅读了一些示例并对其进行了测试,但是所有示例都需要先与某人开始聊天才能接收传入消息...我想检索此传入消息而无需先与 jid 交谈任何人都可以举个例子吗?

4

4 回答 4

30

您需要注册一个 ChatListener 以收到新聊天的通知,然后您可以像往常一样向它们添加消息侦听器:

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

....

private class ChatManagerListenerImpl implements ChatManagerListener {

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

}
于 2011-02-14T16:56:10.823 回答
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 并在控制台上显示收到的每条消息。

于 2011-02-19T20:59:54.480 回答
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("youtID@sample.com", "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"));
            }
        }
    }

}
于 2012-09-10T08:53:47.653 回答
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              
            }
        });
    }
于 2013-06-20T09:48:45.027 回答