创建测试XMPP客户端以进行测试

时间:2012-08-01 10:26:35

标签: xmpp openfire smack xmppframework

我试图用Java创建一个XMPP测试客户端,我想插入我的代码,它将像真正的xmpp客户端一样,但会输出消息,比如说例如日志。我的目标是这样做,因为我想在现实的环境中测试或接近。

理想情况下,我不想模拟或存根,因为我想要运行xmpp客户端。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

你遇到过Psi吗?它是一个非常强大的跨平台XMPP客户端,允许您在其控制台中查看原始XMPP流,并粘贴您自己的XML以直接发送。它可以节省您编写自己的基于Smack的XMPP客户端所需的时间。另请查看我听到的gajim类似内容。

答案 1 :(得分:0)

这将有助于你在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");
    }
}

}