无法通过 Smack API 使用 openfire 服务器发送/接收消息

Posted

技术标签:

【中文标题】无法通过 Smack API 使用 openfire 服务器发送/接收消息【英文标题】:Can't send/resecive messages with openfire server across Smack API 【发布时间】:2011-08-24 20:09:02 【问题描述】:

我有自己配置的 OpenFire 服务器。我创建了两个用户 user1user2,我正在尝试通过 Smack API 在这些用户之间发送/接收消息。

我在 Smack API 中打开了调试模式,可以看到我的虚拟应用程序发送和接收消息,但 MessageListener 不起作用,它从不调用 processMessage(Chat chat, Message message) 方法,我也看不到 Miranda 中的消息user2的IM客户端。

代码:

public class Main 
    public static void main(String[] args) throws XMPPException, InterruptedException 
        sendJabberMessage();

        while (true) 
            Thread.sleep(50);
        
    

    public static void sendJabberMessage() throws XMPPException 
    XMPPConnection.DEBUG_ENABLED = true;
    ConnectionConfiguration config = new ConnectionConfiguration("ejab.net", 5222, "ejab.net");
    SASLAuthentication.supportSASLMechanism("PLAIN", 0);

    config.setCompressionEnabled(true);
    config.setSASLAuthenticationEnabled(true);

    XMPPConnection connection = new XMPPConnection(config);
    connection.connect();
    connection.login("user1", "password");
    Chat chat = connection.getChatManager().createChat("user2@ejab.net.ru", new MessageListener() 
        public void processMessage(Chat chat, Message message) 
            System.out.println("Received message: " + message);
        
    );

这是我的调试日志:

发送日志

<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
<auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">b3BlcmF0b3IyQGVqYWIubmV0AG9wZXJhdG9yMkBlamFiLm5ldAA4ZGRYUGdQYTU0MlQ=</auth>
<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
<iq id="4KApN-0" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>Smack</resource></bind></iq>
<iq id="4KApN-1" type="set"><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq>
<compress xmlns='http://jabber.org/protocol/compress'>
<method>zlib</method></compress>
<stream:stream to="ejab.net" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
<iq id="4KApN-2" type="get"><query xmlns="jabber:iq:roster"></query></iq>
<presence id="4KApN-3"></presence>
<message id="4KApN-4" to="user1@ejab.net.ru" from="user2@ejab.net/Smack" type="chat"><body>Hello world</body><thread>T0T5u0</thread></message>
<iq id="mir_148" to="user1@ejab.net/Miranda" type="result"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="client" name="Smack" type="pc"/><feature var="http://jabber.org/protocol/xhtml-im"/><feature var="http://jabber.org/protocol/muc"/><feature var="http://jabber.org/protocol/commands"/></query></iq>

收到的日志

<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>JIVE-SHAREDSECRET</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms></stream:features>
<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>JIVE-SHAREDSECRET</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><auth xmlns="http://jabber.org/features/iq-auth"/><register xmlns="http://jabber.org/features/iq-register"/></stream:features>
<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>
<iq type="result" id="4KApN-0" to="ejab.net/4942adbf"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>user2@ejab.net/Smack</jid></bind></iq>
<iq type="result" id="4KApN-1" to="user2@ejab.net/Smack"><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq>
<compressed xmlns='http://jabber.org/protocol/compress'/>
<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="ejab.net" id="4942adbf" xml:lang="en" version="1.0"><stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>
<iq type="result" id="4KApN-2" to="user2@ejab.net/Smack"><query xmlns="jabber:iq:roster"><item jid="user1@ejab.net" name="user1" subscription="both"/></query></iq>
<presence from="user1@ejab.net/Miranda" to="user2@ejab.net/Smack"><priority>0</priority><c xmlns="http://jabber.org/protocol/caps" node="http://miranda-im.org/caps" ver="0.9.28.0" ext="pmuc-v1 mood activity mir_notes"/><x xmlns="vcard-temp:x:update"><photo/></x><status>Yep, I'm here.</status></presence>
<iq type="get" to="user2@ejab.net/Smack" id="mir_147" from="user1@ejab.net/Miranda"><query xmlns="jabber:iq:version"/></iq>
<iq type="get" to="user2@ejab.net/Smack" id="mir_148" from="user1@ejab.net/Miranda"><query xmlns="http://jabber.org/protocol/disco#info"/></iq>
<message type="chat" to="user2@ejab.net/Smack" id="mir_151" from="user1@ejab.net/Miranda"><body>TEST</body></message>

有人能帮我说说为什么我的客户端没有在控制台上打印出入会消息,以及为什么 user2 没有收到消息吗?日志不包含任何错误。

【问题讨论】:

补充:客户端连接服务器成功,米兰达客户端显示user1在线 got the same issue, but solved after following this, received my messages within very first try 【参考方案1】:

在接收消息之前,两个用户实际上都需要彼此打开聊天。您应该在 user1 和 user2 之间打开一个聊天,注册一个 MessageListener 来侦听 user2 发送的消息(您创建的那个实际上侦听 user2 发送的消息,并且您的消息日志说您从 user2 向 user1 发送了一条消息),然后反过来做同样的事情。最后发送一条消息,您就可以处理它了。

另外,只是一个评论,请在你的标题中更具体,因为 api 可以很好地与 openfire 配合使用

【讨论】:

【参考方案2】:

您所说的内容、代码中的内容以及调试日志中显示的内容让我有些困惑。

您的代码(和您的帖子)表明您从 Smack 客户端以 user1 身份登录,但您的日志显示您以 user2 身份发送并从 user1 接收。我假设这只是您报告中的一个简单错误。

真正的问题似乎是您正在向

发送消息
user1@ejab.net.ru

并收到来自

的消息
user1@ejab.net/Miranda

这是 2 个不同的用户。您似乎发送给不同的用户,而不是您从中接收。因此,Miranda 客户端没有收到消息的原因是因为 Smack 客户端没有将消息发送到正确的 JID,当然这也是您在侦听器中没有收到任何内容的原因。

另一方面,要接收传入的消息,您可以创建一个ChatManagerListener,然后您将从 Miranda 客户端收到传入的聊天。

【讨论】:

以上是关于无法通过 Smack API 使用 openfire 服务器发送/接收消息的主要内容,如果未能解决你的问题,请参考以下文章

无法通过使用 smack、openfire 获得名册

使用 smack api 连接到 openfire 服务器

是否可以使用 Smack API 在 Openfire Server 上监听/监视用户向另一个用户发送的消息?

如何在 Openfire 中使用 smack

无法在 JAVA 中使用 SMACK api 显示收到的消息

XMPP 注销(Smack API)