无需聊天即可发送和接收消息

Smack send and receive message without chat

我想从一个客户端向另一个客户端发送一条简单的消息,而不是打开聊天,因为永远不会有响应,并且所有消息都会触发相同的事件。

在 smack (4.1.7) 文档中我发现可以这样做,但到目前为止我还没有找到方法。

你有什么想法吗? 使用聊天会更好吗(特别是根据性能:运行时和内存)?

对于接收,您可能希望使用带有合适 filter.For 示例的同步节侦听器,如果您希望接收来自 user@example.org 的正文消息,那么您可以

XMPPConnection connection = …;
connection.addSyncStanzaListener(new StanzaListener() {

@Override
void process(Stanza stanza) {
    Message message = (Message) stanza;
    // Received new message with body from user@example.org
}, new AndFilter(MessageWithBodiesFilter.INSTANCE, 
    FromMatchesFilter.create("user@example.org")));

发送消息更简单

Message message = new Message("user@example.org", "Hi, how are you?");
XMPPConnection connection = …;
connection.sendStanza(message);

提示:阅读 Smack 的源代码是了解这些东西的好方法。如果你看source of ChatManager,你会发现我刚才写的。