我想从一个客户端向另一个不打开聊天的客户端发送一条简单的消息,因为永远不会有响应,并且所有消息都会触发同一事件。
在 smack (4.1.7) 文档中,我发现可以这样做,但到目前为止我还没有找到如何做到这一点的方法。
您有什么想法吗? 使用聊天会更好吗(特别是根据性能:运行时和内存)?
最佳答案
为了接收,您可能需要使用带有合适过滤器的同步节监听器。例如,如果您想从 [email protected] 接收带有正文的消息。 ,那么你可以
XMPPConnection connection = …;
connection.addSyncStanzaListener(new StanzaListener() {
@Override
void process(Stanza stanza) {
Message message = (Message) stanza;
// Received new message with body from <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86f3f5e3f4c6e3fee7ebf6eae3a8e9f4e1" rel="noreferrer noopener nofollow">[email protected]</a>
}, new AndFilter(MessageWithBodiesFilter.INSTANCE,
FromMatchesFilter.create("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9eebedfbecdefbe6fff3eef2fbb0f1ecf9" rel="noreferrer noopener nofollow">[email protected]</a>")));
发送消息更加容易
Message message = new Message("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ce9eff9eedcf9e4fdf1ecf0f9b2f3eefb" rel="noreferrer noopener nofollow">[email protected]</a>", "Hi, how are you?");
XMPPConnection connection = …;
connection.sendStanza(message);
提示:阅读 Smack 的源代码是了解此类内容的好方法。如果你看source of ChatManager ,你会发现我上面刚刚写的内容。
关于java - Smack 无需聊天即可发送和接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293883/