如何使用 Smack 接收传入的 XMPP 消息?

发布于 2024-10-17 08:22:18 字数 80 浏览 1 评论 0原文

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

I read some examples and tested them but all of them need to start a chat with someone first to receive Incoming Messages... I want to retrieve this Incoming Messages without need to talk first to the jid anyone can give an example ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

大海や 2024-10-24 08:22:18

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

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

....

private class ChatManagerListenerImpl implements ChatManagerListener {

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

}

You need to register a ChatListener to be notified of new chats, then you can add a message listener to them like normal:

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

....

private class ChatManagerListenerImpl implements ChatManagerListener {

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

}
心意如水 2024-10-24 08:22:18

我只是想添加一个副本&粘贴示例:

  // 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 并在控制台上显示每条收到的消息。

i just wanted to add a copy & paste sample:

  // 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();

This sample connects to jabber.org and displays every received message on the console.

孤云独去闲 2024-10-24 08:22:18

请找到以下代码。
请添加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"));
            }
        }
    }

}

Please find the following code.
Please add smack.jar & smackx.jar to your build path

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"));
            }
        }
    }

}
沐歌 2024-10-24 08:22:18
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              
            }
        });
    }
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              
            }
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文