XMPP的Client
可以和tigase server .openfire 等主流XMPP服务器连接,
需要用到smack 包
/**
* TestXMPPClient
* @author Charles
* @email chs.garea@gmail.com
*
* */
package com.gareatech.testxmpp;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Registration;
/**
* @param arg[0] XMPP Server
* @param arg[1] Thread Number
* @param arg[2] Debug Mode 1,
* TestXMPPClient 192.168.10.106 10 1
* */
public class TestXMPPClient {
public static void main(String[] arg) throws InterruptedException {
boolean Debug = false;
String XMPPserver = "192.168.10.106";
int number = 0;
switch (arg.length)
{
case 2:
XMPPserver = arg[0];
number = Integer.parseInt(arg[1]);
break;
case 3:
XMPPserver = arg[0];
number = Integer.parseInt(arg[1]);
if(arg[2].equals("1"))
Debug = true;
break;
default:
System.out.println("Wrong Paramenters");
return;
}
System.out.println("server:"+XMPPserver+"\nnumber:"+number+"\nDebug:"+Debug);
for( int i = 0; i<number; i++)
{
new MitiSay(i+"",XMPPserver,Debug).start();
Thread.sleep(1000);
}
}
}
/**
* 线程
*
* */
class MitiSay extends Thread {
private String XMPPServer;
private boolean DebugFlag ;
public MitiSay(String threadName) {
super(threadName);
this.XMPPServer = "192.168.10.106";
this.DebugFlag = false;
}
public MitiSay(String threadName,String XMPPserver) {
super(threadName);
this.XMPPServer = XMPPserver;
this.DebugFlag = false;
}
public MitiSay(String threadName,String XMPPserver,boolean DebugFlag) {
super(threadName);
this.XMPPServer = XMPPserver;
this.DebugFlag = DebugFlag;
}
public void run() {
System.out.println("线程"+getName() + " 开始!");
XMPPConnection.DEBUG_ENABLED = DebugFlag;
ConnectionConfiguration connectionConfig = new ConnectionConfiguration("192.168.10.106", 5222, "pc-201012031249");
connectionConfig.setSecurityMode(SecurityMode.required);
connectionConfig.setSASLAuthenticationEnabled(false);
connectionConfig.setCompressionEnabled(false);
//connectionConfig.setSelfSignedCertificateEnabled(true);
try {
/** Connection **/
XMPPConnection connection = new XMPPConnection(connectionConfig);
PacketFilter packetFilter = new AndFilter();
PacketListener packetListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
// TODO Auto-generated method stub
System.out.println("Thread"+getName()+"Get Message:"+packet.toXML());
if (packet instanceof IQ) {
IQ response = (IQ) packet;
if (response.getType() == IQ.Type.ERROR) {
if (!response.getError().toString().contains("409")) {
}
} else if (response.getType() == IQ.Type.RESULT) {
}
}
}
};
connection.addPacketListener(packetListener, packetFilter);
connection.connect();
System.out.println("connect! Next-> Register!");
/** Registration **/
final String newUsername = newRandomUUID();
final String newPassword = newRandomUUID();
Registration registration = new Registration();
registration.setType(IQ.Type.SET);
registration.setTo(XMPPServer);
Map paramenter = new HashMap();
paramenter.put("username", newUsername);
paramenter.put("password", newPassword);
paramenter.put("name", "new");
paramenter.put("email", "mail@mail.com");
registration.setAttributes(paramenter);
connection.sendPacket(registration);
System.out.println("registration! Next -> Login");
/** login **/
//connection.login("admin", "tigase");
connection.login(newUsername, newPassword);
System.out.println("Logged!!"+newUsername+"\n"+newPassword);
while (true)
{
Thread.sleep(500000000);
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(getName() + " 线程运行结束!");
}
/**
*
* newRandomUUID: 获取一随机数
*/
private static String newRandomUUID() {
String uuidRaw = UUID.randomUUID().toString();
return uuidRaw.replaceAll("-", "");
}
}