我无法使用 SMACK API 向 openfire 服务器上的 XMPP 客户端发送消息。 我不确定我哪里出错了。 我在 gtalk 上测试了相同的代码,它工作正常。
public class SenderTest
{
public static void main(String args[])
{
ConnectionConfiguration connConfig = new ConnectionConfiguration("localhost", 5222);
connConfig.setSASLAuthenticationEnabled(false);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
connection.connect();
System.out.println("Connected to " + connection.getHost());
} catch (XMPPException ex) {
//ex.printStackTrace();
System.out.println("Failed to connect to " + connection.getHost());
System.exit(1);
}
try {
connection.login("sender", "a");
System.out.println("Logged in as " + connection.getUser());
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
} catch (XMPPException ex) {
//ex.printStackTrace();
System.out.println("Failed to log in as " + connection.getUser());
System.exit(1);
}
ChatManager chatmanager = connection.getChatManager();
Chat newChat = chatmanager.createChat("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2c3b3d3b37283b2c1e3b263f332e323b703d3133" rel="noreferrer noopener nofollow">[email protected]</a>", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
try {
newChat.sendMessage("Howdy!");
System.out.println("Message Sent...");
}
catch (XMPPException e) {
System.out.println("Error Delivering block");
}
}
}
它给我一条“消息已发送...”。 但没有消息到达接收端。
此外,如果“发送者”想要向“接收者”发送消息,那么这是否意味着他们应该添加到彼此的“名册”中
最佳答案
您检查openfire服务器的错误日志。 您可能会收到类似“流 header 中的主机名不正确”的错误。主机:example.com' 我见过这种类型的错误,如果您的服务器名称是 'localhost' ,那么您可以在用户之间发送消息,例如 [email protected] , [email protected] ...等等
但是[email protected]无法发送消息至[email protected] .
关于java - 无法向 openfire 服务器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784320/