我无法使用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("receiver@example.com", 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");
}
}
}
它给了我一条'消息已发送...'。 但没有消息到达接收端。
此外,如果'发送者'想要向“接收者”发送消息,那么这是否意味着他们应该被添加到彼此的“名单”
答案 0 :(得分:2)
您检查openfire服务器的错误日志。 您可能会在流标头中收到错误,如'错误的主机名。主持人:example.com' 我看到过这种类型的错误,如果您的服务器名称是“localhost”,那么您可以在user1 @ localhost.com,user2 @ localhost.com等用户之间发送消息
但是user1@localhost.com无法向xyz@example.com发送消息。
答案 1 :(得分:1)
您正在登录 localhost ,但是您要向
AFAIK,聊天并不要求他们在彼此的名单上,尽管这是更典型的案例。