我已经实现了一个使用 Smack API 连接的 Facebook 聊天客户端。目前,主要功能运行良好。我发送、接收消息、接收打字通知...... 但我有一个问题。如果我正在使用我的客户端与某人交谈,我会收到我在 Facebook 聊天中发送给该联系人的消息。但是,如果通过 Facebook 聊天向联系人发送消息,我的客户不会收到该消息。如果联系人回答我,我会收到他的消息,但对话变得不完整,因为它只显示联系人发送的消息,但不显示我在另一个 XMPP 客户端中发送的消息。
由于 Facebook 显示了我的客户发送的消息,我认为也可以这样做。我对 XMPP 还很陌生,我需要一些帮助来弄清楚如何做到这一点。
我的代码现在看起来像这样:
接收消息:
connection.getChatManager().addChatListener(
new ChatManagerListener(){
@Override
void chatCreated(Chat chat, boolean createdLocally) {
if(!createdLocally){
chat.addMessageListener(messageListener)
}
}
}
)
消息监听器只是通过推送将消息传递到我的 Javascript 客户端。
发送消息:
public boolean sendMessage(String jid, String message){
FacebookContact contact = mapJIDContact.get(jid)
try{
if (contact != null && (connection != null) && (connection.isConnected())) {
ChatManager chatManager = connection.getChatManager();
if(contact.chat == null){
contact.chat = chatManager.createChat(jid, messageListener);
}
contact.chat.sendMessage(message);
return true
}
return false
}
catch (XMPPException e){
return false
}
}
FacebookContact 是我创建的一个类。它存储一些联系信息和聊天(如果已打开)。
编辑:
我发现我想要的这个功能是由这个XMPP扩展提供的:http://xmpp.org/extensions/xep-0280.html 。我正在尝试查找 facebook 聊天是否实现了此扩展。
最佳答案
如果您发送 IQ 来启用该功能,服务器会回复
智商:
<iq xmlns='jabber:client'
from='my.facebook.username@chat.facebook.com/Mac-Pro-di-Michele_65563c5f_4D689E59FB8A5'
to='chat.facebook.com'
type='set'
id='enable1'>
<enable xmlns='urn:xmpp:carbons:2'/>
</iq>
回复:
<iq xmlns='jabber:client'
from='chat.facebook.com'
to='my.facebook.username@chat.facebook.com/Mac-Pro-di-Michele_65563c5f_4D689E59FB8A5' type='error'
id='enable1'>
<enable xmlns='urn:xmpp:carbons:2'/>
<error code='501' type='cancel'>
<feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</iq>
关于java - XMPP - Facebook - 我的客户端实现如何接收其他客户端实现发送的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15025615/