首先在服务的控制台,设置允许匿名登陆,设置界面如下:
客户端匿名登陆代码:
package com.bis.smack.client;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
public class AnonymouslyLogin {
private static XMPPConnection connection = new XMPPConnection("wang-think");
static{
try {
connection.connect();
connection.loginAnonymously();//匿名登陆
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
Chat chat = connection.getChatManager().createChat("sys@wang-think",
new MessageListener() {
public void processMessage(Chat arg0, Message message) {
System.out.println("Received message: "+ message.getBody());
}
});
Message msg = new Message();
chat.sendMessage("Howdy5!");
while(true);
}
}