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

发布于 2024-12-01 09:27:55 字数 8894 浏览 1 评论 0原文

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

我在 Smack API 中打开调试模式,可以看到我的虚拟应用程序发送和接收消息,但 MessageListener 不起作用,它从不调用 processMessage(Chat chat, Message message) code> 方法,我也无法在 user2 的 Miranda 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("[email protected]", 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="[email protected]" from="[email protected]/Smack" type="chat"><body>Hello world</body><thread>T0T5u0</thread></message>
<iq id="mir_148" to="[email protected]/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>[email protected]/Smack</jid></bind></iq>
<iq type="result" id="4KApN-1" to="[email protected]/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="[email protected]/Smack"><query xmlns="jabber:iq:roster"><item jid="[email protected]" name="user1" subscription="both"/></query></iq>
<presence from="[email protected]/Miranda" to="[email protected]/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="[email protected]/Smack" id="mir_147" from="[email protected]/Miranda"><query xmlns="jabber:iq:version"/></iq>
<iq type="get" to="[email protected]/Smack" id="mir_148" from="[email protected]/Miranda"><query xmlns="http://jabber.org/protocol/disco#info"/></iq>
<message type="chat" to="[email protected]/Smack" id="mir_151" from="[email protected]/Miranda"><body>TEST</body></message>

有人可以帮助我并说明为什么我的客户端不在控制台上打印传入消息,以及为什么 user2 没有收到消息;日志不包含任何错误。

I have my own configured OpenFire server. I have created two users user1 and user2 and I'm trying to send/receive messages between these users across the Smack API.

I switched on debug mode in Smack API and can see that my dummy application sends and receives messages, but MessageListener doesn't work, it never invokes processMessage(Chat chat, Message message) method, also I can't see messages in the Miranda IM client of the user2.

Code:

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("[email protected]", new MessageListener() {
        public void processMessage(Chat chat, Message message) {
            System.out.println("Received message: " + message);
        }
    });
}

Here is my debug logs:

Sent log

<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="[email protected]" from="[email protected]/Smack" type="chat"><body>Hello world</body><thread>T0T5u0</thread></message>
<iq id="mir_148" to="[email protected]/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>

Received log

<?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>[email protected]/Smack</jid></bind></iq>
<iq type="result" id="4KApN-1" to="[email protected]/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="[email protected]/Smack"><query xmlns="jabber:iq:roster"><item jid="[email protected]" name="user1" subscription="both"/></query></iq>
<presence from="[email protected]/Miranda" to="[email protected]/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="[email protected]/Smack" id="mir_147" from="[email protected]/Miranda"><query xmlns="jabber:iq:version"/></iq>
<iq type="get" to="[email protected]/Smack" id="mir_148" from="[email protected]/Miranda"><query xmlns="http://jabber.org/protocol/disco#info"/></iq>
<message type="chat" to="[email protected]/Smack" id="mir_151" from="[email protected]/Miranda"><body>TEST</body></message>

Can somebody help me and say why my client doesn't prints incomimming messages on console, and why user2 doesn't receive messages; Logs doesn't contain any errors.

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

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

发布评论

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

评论(2

九八野马 2024-12-08 09:27:55

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

另外,只是一条评论,请在您的标题中更具体,因为 api 与 openfire 配合得很好

Both users actually need to open a chat to each other before receiving messages. You should open a chat between user1 and user2, register a MessageListener that will listen for messages send by user2 (the one you created actually listens to messages send by user2 and your message log is saying that you send a message from user2 to user1), then do the same the other way around. Finally send a message and you'll be able to process it.

Also, just a comment, please be more specific in your titles as the api works well with openfire

鸠魁 2024-12-08 09:27:55

我对你所说的、代码中的内容以及调试日志中显示的内容感到有些困惑。

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

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

[email protected]

发送消息并从他们接收消息

[email protected]/Miranda

这两个不同的用户 。您发送的消息似乎与接收消息的用户不同。因此 Miranda 客户端没有收到消息的原因是 Smack 客户端没有将其发送到正确的 JID,当然这也是你在监听器中没有收到任何消息的原因。

另一方面,要接收传入消息,您可以创建 ChatManagerListener 然后您将从米兰达的客户。

I am somewhat confused by what you have said, what is in your code and what you are showing in your debug log.

Your code (and your post) say that you are logging in as user1 from the Smack client, but your log shows that you are sending as user2 and receiving from user1. I am assuming that is just a simple error in your reporting.

The real problem appears to be that you are sending a message to

[email protected]

and receiving a message from

[email protected]/Miranda

These are 2 different users. You appear to be sending to a different user than you are recieving from. Thus the reason the Miranda client does not recieve the message is because the Smack client did not send it to the right JID, and of course is the same reason why you did not receive anything in your listener.

On the flip side, to receive the incoming message, you can create a ChatManagerListener and then you will get your incoming chat from the Miranda client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文