smack api 轉載未測試

來源:互聯網
上載者:User

標籤:ext   c   http   int   get   com   

===============================================================
主動發送資訊給某個使用者
------------------------------------
XMPPConnection.DEBUG_ENABLED = true;
//設定伺服器位址
XMPPConnection conn = new XMPPConnection("127.0.0.1");
conn.connect();
//輸入帳號和密碼登陸
conn.login("[email protected]", "123456");
//建立一個和test1的對話,並設定資訊監聽
Chat mychat = conn.getManager().create("[email protected]",
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("[email protected]", "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("[email protected]", "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("[email protected]", "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();
==========================================================================

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.