我试图在 Java 中创建一个 XMPP 测试客户端,我想插入我的代码,它的行为就像真正的 xmpp 客户端,但会输出消息,例如到日志中。我的目标是这样做,因为我想在现实的环境中或接近的环境中进行测试。
理想情况下,我不想模拟或存根它,因为我希望运行 xmpp 客户端。
有任何想法吗?
我试图在 Java 中创建一个 XMPP 测试客户端,我想插入我的代码,它的行为就像真正的 xmpp 客户端,但会输出消息,例如到日志中。我的目标是这样做,因为我想在现实的环境中或接近的环境中进行测试。
理想情况下,我不想模拟或存根它,因为我希望运行 xmpp 客户端。
有任何想法吗?
这将帮助您在 xmpp 上向接收者发送消息。请记住,在运行此之前,您必须配置 openfire
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
public class SenderTest
{
public static void main(String args[])
{
//ConnectionConfiguration connConfig = new ConnectionConfiguration("localhost", 5222);
//connConfig.setSASLAuthenticationEnabled(false);
ConnectionConfiguration connConfig = new ConnectionConfiguration("localhost", 5222);
//ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
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@example.com", "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@gmail.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");
}
}
}