用smack+openfire做即時通訊
首發:個人博客
必須說明:smack最新的4.1.1,相對之前版本變化很大,而且資料缺乏,官方文檔也不好,所以還是用老版本3.2.2吧。這篇博文中的代碼是4.1.1版的,但不推薦用它。
用openfire做服務器,用spark做幫助調試的客戶端,用smack(官方文檔在這里,感覺寫得非常不好)做java的庫,來完成即時通訊功能。
1.安裝openfire
在官網下載安裝即可。
2.安裝spark
在官網下載安裝即可。
3.maven引入smack
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-java7</artifactId>
<version>4.1.1</version>
<exclusions>
<exclusion>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
<version>1.1.3.4.O</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-tcp</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-im</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-extensions</artifactId>
<version>4.1.1</version>
</dependency>
如果提示找不到就手動安裝一下吧,在maven倉庫找到jar并手動下載之后,執行類似下面這條語句:
mvn install:install-file -Dfile={Path/to/your/ojdbc.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
4.編寫demo代碼做實驗
關鍵的概念:配置,連接,花名冊,會話管理者,會話
import java.util.Collection;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterListener;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;public class SmackClient {
public static void countPeople(Roster r){
System.out.println("在線人數變為:" + r.getEntryCount());
}
public static void go(){
try{
//對連接的配置
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
builder.setUsernameAndPassword("guoguo", "cc19881031");
builder.setServiceName("127.0.0.1");
builder.setHost("127.0.0.1");
builder.setPort(5222);
//不加這行會報錯,因為沒有證書
builder.setSecurityMode(SecurityMode.disabled);
builder.setDebuggerEnabled(true);
XMPPTCPConnectionConfiguration config = builder.build();
//建立連接并登陸
AbstractXMPPConnection c = new XMPPTCPConnection(config);
c.addPacketSendingListener(new StanzaListener(){
@Override
public void processPacket(Stanza st)
throws NotConnectedException {
System.out.println("in StanzaListener:" + st.getFrom());
}
}, new StanzaFilter(){
@Override
public boolean accept(Stanza st) {
System.out.println("in StanzaFilter:" + st.getFrom());
return false;
}
});
c.connect();
c.login();
final Roster roster = Roster.getInstanceFor(c);
Presence p = roster.getPresence("admin@127.0.0.1");
System.out.println(p.getType());
roster.addRosterListener(new RosterListener() {
public void entriesAdded(Collection<String> arg0) {countPeople(roster);}
public void entriesDeleted(Collection<String> addresses) {countPeople(roster);}
public void entriesUpdated(Collection<String> addresses) {countPeople(roster);}
public void presenceChanged(Presence presence) {countPeople(roster);}
});
// //設置是否在線狀態,和狀態說明
// Presence presence = new Presence(Presence.Type.unavailable);
// presence.setStatus("Gone fishing");
// c.sendStanza(presence);
//會話管理者的建立和配置監聽
ChatManager chatmanager = ChatManager.getInstanceFor(c);
chatmanager.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat cc, boolean bb) {
//當收到來自對方的消息時觸發監聽函數
cc.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat cc, Message mm) {
System.out.println(mm.getBody());
}
});
}
});
//建立會話
Chat chat = chatmanager.createChat("admin@127.0.0.1");
chat.getThreadID();
//發消息
Message msg = new Message();
msg.setBody("hello!");
chat.sendMessage(msg);
while(true);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
go();
}
}