===============================================================
主动发送信息给某个用户
------------------------------------
XMPPConnection.DEBUG_ENABLED
= true;
//设置服务器地址
XMPPConnection conn = new XMPPConnection("127.0.0.1");
conn.connect();
//输入账号和密码登陆
conn.login("test2@127.0.0.1", "123456");
//创建一个和test1的对话,并设置信息监听
Chat mychat =
conn.getManager().create("test1@127.0.0.1",
new MessageListener()
{
@Override
public void processMessage(Chat chat,, Message message)
{
String messageBody =
message.getBody();
System.out.println("收到信息:"+messageBody);
}
});
//发送给test1文本信息
mychat.sendMessage("hello");
//退出登陆
conn.disconnect();
===============================================================
设置自己的登陆状态
------------------------------------
XMPPConnection.DEBUG_ENABLED
= true;
//设置服务器地址
XMPPConnection conn = new XMPPConnection("127.0.0.1");
conn.connect();
//输入账号和密码登陆
conn.login("test2@127.0.0.1", "123456");
//设置登陆后的个人状态信息
Presence p = new
Presence(Presence.Type.available);
p.setStatus("发呆中。。。");
conn.sendPacket(p);
//退出登陆
conn.disconnect();
==========================================================================
被动接收用户发来的信息
------------------------------------
XMPPConnection.DEBUG_ENABLED
= true;
//设置服务器地址
XMPPConnection conn = new XMPPConnection("127.0.0.1");
conn.connect();
//输入账号和密码登陆
conn.login("test1@127.0.0.1", "123456");
//设置信息的监听
conn.getChatManager().addChatListener(new ChatManagerListener()
{
@Override
public void chatCreated(Chat chat, boolean createdLocally)
{
chat.addMessageListener(new MessageListener()
{
@Override
public void processMessage(Chat chat, Message message)
{
String messageBody =
message.getBody();
System.out.println("接收到信息:"+messageBody);
}
});
}
});
//退出登陆
conn.disconnect();
==========================================================================
获取我的好友列表
------------------------------------
//设置服务器地址
XMPPConnection
conn = new XMPPConnection("127.0.0.1");
conn.connect();
//输入账号和密码登陆
conn.login("test1@127.0.0.1", "123456");
Collection<RosterEntry> rosters =
conn.getRoster().getEntries();
System.out.println("我的好友列表:=======================");
for(RosterEntry
rosterEntry : rosters){
System.out.print("name:
"+rosterEntry.getName()+",jid:
"+rosterEntry.getUser());
System.out.println("");
}
System.out.println("我的好友列表:=======================");
conn.disconnect();
==========================================================================
聊天窗口输入状态,使用XEP-0085
协议
------------------------------------
//发送给test1初始文本信息,附带输入状态
Message
mess = new Message();
mess.addExtension(new
ChatStateExtension(ChatState.active));
mychat.sendMessage(mess);
//发送给test1初始文本信息,附带正在输入的状态
Message
mess = new Message();
mess.addExtension(new
ChatStateExtension(ChatState.composing));
mychat.sendMessage(mess);
//发送给test1初始文本信息,附带暂停输入的状态
Message mess = new
Message();
mess.addExtension(new
ChatStateExtension(ChatState.paused));
mychat.sendMessage(mess);
//其他略。。。
//接收的时候
public void processMessage(Chat chat, Message message)
{
String messageBody = message.getBody();
PacketExtension
pe;
pe =
message.getExtension("composing","http://jabber.org/protocol/chatstates");
if(pe
!= null){
System.out.println("对方正在输入......");
}
pe =
message.getExtension("active","http://jabber.org/protocol/chatstates");
if(pe
!= null){
System.out.println("接收到信息:"+messageBody);
}
pe =
message.getExtension("paused","http://jabber.org/protocol/chatstates");
if(pe
!= null){
System.out.println("对方已暂停输入");
}
pe =
message.getExtension("inactive","http://jabber.org/protocol/chatstates");
if(pe
!= null){
System.out.println("对方聊天窗口失去焦点");
}
pe =
message.getExtension("gone","http://jabber.org/protocol/chatstates");
if(pe
!=
null){
System.out.println("对方聊天窗口被关闭");
}
}
==========================================================================
接收邀请,加入多人聊天房间
------------------------------------
MultiUserChat.addInvitationListener(conn,
new InvitationListener() {
@Override
public void
invitationReceived(XMPPConnection conn, String room,
String inviter,
String reason, String password, Message message) {
MultiUserChat
multiUserChat = new MultiUserChat(conn, room);
System.out.println("收到来自
"+inviter+" 的聊天室邀请。邀请附带内容:"+reason);
try
{
multiUserChat.join("test2", password);
} catch (XMPPException e)
{
System.out.println("加入聊天室失败");
e.printStackTrace();
}
System.out.println("成功加入聊天室");
multiUserChat.addMessageListener(new
PacketListener() {
@Override
public void processPacket(Packet
packet) {
Message message =
(Message)packet;
//接收来自聊天室的聊天信息
System.out.println(message.getFrom()+":"+message.getBody());
}
});
}
//发送信息到聊天室
multiUserChat.sendMessage("新手到来,大家关照!");
});
==========================================================================
登陆gtalk
------------------------------------
XMPPConnection
conn = new XMPPConnection(new ConnectionConfiguration("talk.google.com", 5222,
"gmail.com"));
conn.connect();
//输入gtalk的账号密码
conn.login("88888888",
"8888888888");
Collection<RosterEntry> rosters =
conn.getRoster().getEntries();
//获取gtalk上的好友列表
System.out.println("我的好友列表:=======================");
for(RosterEntry rosterEntry : rosters){
System.out.print("name:
"+rosterEntry.getName()+",jid:
"+rosterEntry.getUser());
System.out.println("");
}
System.out.println("我的好友列表:=======================");
conn.disconnect();
==========================================================================
smack api 转载未测试,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/hujihon/p/3721779.html