XMPP 无法设置侦听器
我已成功连接到 XMPP 服务器(从 Android XMPP 客户端),并且我可以向用户发送消息,但我无法接收来自同一用户的响应。
我发送这样的消息:
public void send_message(String message, String buddy) throws XMPPException {
buddy += "@localhost";
/* send message to user */
Log.w("Sending mesage " + message + " to user " + buddy, "0");
chat = chatManager.createChat(buddy, messageListener);
chat.sendMessage(message);
}
我将 messageListener 传递给 createChat 函数。 MessageListener 的类是:
class XMPPMessageListener implements MessageListener {
private String from;
private String body;
public void processMessage(Chat chat, Message message) {
this.from = message.getFrom();
this.body = message.getBody();
Log.w("*****Received message" + body + " from " + from, "0*****");
}
}
当向用户发送消息时,我得到以下调试输出:
W/Sending mesage play to user test@localhost( 823): 0
D/SMACK ( 823): 10:43:54 AM SENT (1156346368): <message id="vwaJX-15" to="test@localhost" from="eleano@localhost/Smack" type="chat"><body>test</body><thread>249ke0</thread></message>
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): <presence id="vwaJX-12" to="eleano@localhost/Smack" from="eleano" type="error"><error code="404" type="cancel"><remote-server-no
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): t-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): <presence id="vwaJX-14" to="eleano@localhost/Smack" from="test" type="error"><error code="404" type="cancel"><remote-server-not-
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>
并且消息“test”显示给连接的用户 test(在本例中)。我正在从用户 eleano 发送消息。我们还可以看到屏幕上显示了调试输出“ending message play to user test@localhost”,这表明我的函数确实被成功调用。
收到从 test 到 eleano 的消息后,我只得到此调试输出:
D/SMACK ( 823): 10:44:00 AM RCV (1156346368): <message id="58Fjj-64" to="eleano@localhost/Smack" from="test@localhost/Spark 2.6.3" type="chat"><body>yes</body><thread>0tlK7o<
D/SMACK ( 823): 10:44:00 AM RCV (1156346368): /thread><x xmlns="jabber:x:event"><offline/><composing/></x></message>
但用户 eleano 没有收到消息。 no:
Log.w("*****Received message" + body + " from " + from, "0*****");
我们还可以注意到屏幕上没有显示 ,因此 MessageListener 永远不会被调用。这是为什么?我已经按照文档中的说明正确设置了它。
欢迎任何想法。谢谢。
I've successfully connected to XMPP server (from android XMPP client), and I can send messages to a user, but I can't receive responses from that same user.
I'm sending messages like this:
public void send_message(String message, String buddy) throws XMPPException {
buddy += "@localhost";
/* send message to user */
Log.w("Sending mesage " + message + " to user " + buddy, "0");
chat = chatManager.createChat(buddy, messageListener);
chat.sendMessage(message);
}
I'm passing the messageListener to the createChat function. The MessageListener's class is:
class XMPPMessageListener implements MessageListener {
private String from;
private String body;
public void processMessage(Chat chat, Message message) {
this.from = message.getFrom();
this.body = message.getBody();
Log.w("*****Received message" + body + " from " + from, "0*****");
}
}
When sending a message to a user I'm getting the following debugging output:
W/Sending mesage play to user test@localhost( 823): 0
D/SMACK ( 823): 10:43:54 AM SENT (1156346368): <message id="vwaJX-15" to="test@localhost" from="eleano@localhost/Smack" type="chat"><body>test</body><thread>249ke0</thread></message>
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): <presence id="vwaJX-12" to="eleano@localhost/Smack" from="eleano" type="error"><error code="404" type="cancel"><remote-server-no
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): t-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): <presence id="vwaJX-14" to="eleano@localhost/Smack" from="test" type="error"><error code="404" type="cancel"><remote-server-not-
D/SMACK ( 823): 10:43:54 AM RCV (1156346368): found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></presence>
And the message "test" is displayed to the connected user test (in this case). I'm sending the message from a user eleano. We can also see there's a debug output "ending mesage play to user test@localhost" being displayed on the screen, indicating that my function is indeed called successfully.
Upon receiving the message from test to eleano, I only get this debug output:
D/SMACK ( 823): 10:44:00 AM RCV (1156346368): <message id="58Fjj-64" to="eleano@localhost/Smack" from="test@localhost/Spark 2.6.3" type="chat"><body>yes</body><thread>0tlK7o<
D/SMACK ( 823): 10:44:00 AM RCV (1156346368): /thread><x xmlns="jabber:x:event"><offline/><composing/></x></message>
But the user eleano is not receiving the message. We can also notice there's no:
Log.w("*****Received message" + body + " from " + from, "0*****");
being displayed on the screen, so the MessageListener is never called. Why is that? I've set it up correctly as it says in documentation.
Any ideas are welcome. And thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您指出这一点。您的观察使我在连接上设置侦听器(并侦听 Char 类型的包),而不是在聊天对象本身上设置侦听器。
现在我的代码如下所示。我发送的包裹是这样的:
并接收如下的消息:
它有效。我可以向用户发送消息并很好地接收它们。
Thanks for pointing that out. Your observation led me to setup a listener on connection (and to listen for packages of type Char), rather than setting a listener on the chat object itself.
Now my code looks like the following. I'm sending the packages like this:
And receiving the messages like the following:
And it works. I can send messages to the user and receive them just fine.
您的问题是来自 test 的消息具有不同的线程 id。这相当于属于不同聊天的消息。如果您创建一个 ChatManagerListener,它将在创建新聊天时调用它。我不确定为什么回复消息会有不同的线程 ID。
通常,对话是通过线程 ID 来协调的。这允许两个用户之间进行多个并发对话。话虽这么说,有些客户端根本不使用线程 ID。在这种情况下,Smack 会将传入的聊天消息与具有相同 JID 的现有聊天消息(如果已存在)进行匹配,或者创建一条新消息(如果不存在)。
Your problem is that the message from test has a different thread id. This equates to the message belonging to a different chat. If you create a ChatManagerListener, it will get called with the new Chat as it is created. I am not sure why the reply message would have a different thread id though.
Typically, a conversation is coordinated by the thread id. This allows for multiple concurrent conversations between two users. That being said, some clients don't use a thread id at all. In that case, Smack will match the incoming chat message to an existing one with the same JID, if it exists already, or create a new one if it does not.
也许你可以像这样确保线程 id:
Maybe you could make sure thread id like this: