【问题标题】:Smack send and receive message without chat无需聊天即可发送和接收消息
【发布时间】:2016-05-18 08:15:24
【问题描述】:

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

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

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

【问题讨论】:

    标签: java smack


    【解决方案1】:

    对于接收,您可能希望使用带有合适过滤器的同步节侦听器。例如,如果您想从 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,你会发现我刚刚在上面写的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 2014-07-26
      • 1970-01-01
      相关资源
      最近更新 更多