用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();
}
}