Openfire即时通讯服务搭建与Smack访问.docx
- 文档编号:30403991
- 上传时间:2023-08-14
- 格式:DOCX
- 页数:16
- 大小:165.50KB
Openfire即时通讯服务搭建与Smack访问.docx
《Openfire即时通讯服务搭建与Smack访问.docx》由会员分享,可在线阅读,更多相关《Openfire即时通讯服务搭建与Smack访问.docx(16页珍藏版)》请在冰豆网上搜索。
Openfire即时通讯服务搭建与Smack访问
Openfire即时通讯服务搭建与Smack访问
Openfire是开源的、基于可拓展通讯和表示协议(XMPP)、采用Java编程语言开发的实时协作服务器。
Openfire安装和使用都非常简单,并利用Web进行管理。
单台服务器可支持上万并发用户。
首先到openfire官网上去下载Openfire的服务器包与Smack的客户端调用包。
Openfire下载后,如果是win的版本,那么它是一个exe的安装包,运行下一步下一步即可,其间会需要配置数据库(Openfire当然是需要数据库支持的了,比如要存帐号等等的信息)。
编写我们的程序访问Operfire
[java]viewplaincopy
importjava.util.Collection;
importjava.util.Iterator;
import.SocketFactory;
importorg.jivesoftware.smack.AccountManager;
importorg.jivesoftware.smack.Chat;
importorg.jivesoftware.smack.ChatManager;
importorg.jivesoftware.smack.ChatManagerListener;
importorg.jivesoftware.smack.Connection;
importorg.jivesoftware.smack.ConnectionConfiguration;
importorg.jivesoftware.smack.MessageListener;
importorg.jivesoftware.smack.Roster;
importorg.jivesoftware.smack.RosterEntry;
importorg.jivesoftware.smack.XMPPConnection;
importorg.jivesoftware.smack.XMPPException;
importorg.jivesoftware.smack.packet.Message;
importorg.jivesoftware.smack.packet.Presence;
importorg.jivesoftware.smack.packet.Session;
importorg.jivesoftware.smack.packet.Message.Type;
publicclassSmackUtil{
privateConnectionconnection;
privateConnectionConfigurationconfig;
/**openfire服务器address*/
privatefinalstaticStringserver="10.100.53.65";
privatefinalvoidprint(Objecto){
if(o!
=null){
System.out.println(o);
}
}
privatefinalvoidprint(Objecto,Object...args){
if(o!
=null&&args!
=null&&args.length>0){
Strings=o.toString();
for(inti=0;i Stringitem=args[i]==null? "": args[i].toString(); if(s.contains("{"+i+"}")){ s=s.replace("{"+i+"}",item); }else{ s+=""+item; } } System.out.println(s); } } /** *初始Smack对openfire服务器链接的基本配置 */ publicvoidinit(){ try{ //connection=newXMPPConnection(server); //connection.connect(); /**5222是openfire服务器默认的通信端口,你可以登录http: //10.100.53.65: 9090/到管理员控制台查看客户端到服务器端口*/ config=newConnectionConfiguration(server,5222); /**是否启用压缩*/ config.setCompressionEnabled(true); /**是否启用安全验证*/ config.setSASLAuthenticationEnabled(true); /**是否启用调试*/ config.setDebuggerEnabled(false); config.setReconnectionAllowed(true); config.setRosterLoadedAtLogin(true); /**创建connection链接*/ connection=newXMPPConnection(config); /**建立连接*/ connection.connect(); System.out.println("connectionsuccess"); }catch(XMPPExceptione){ e.printStackTrace(); } print(connection); print(connection.getConnectionID()); } publicvoiddestory(){ if(connection! =null){ connection.disconnect(); connection=null; } } /** *ConnectionConfiguration的基本配置相关信息 */ publicvoidseeConfig(){ print("PKCS11Library: "+config.getPKCS11Library()); print("ServiceName: {0}",config.getServiceName()); //ssl证书密码 print("TruststorePassword: {0}",config.getTruststorePassword()); print("TruststorePath: {0}",config.getTruststorePath()); print("TruststoreType: {0}",config.getTruststoreType()); SocketFactorysocketFactory=config.getSocketFactory(); print("SocketFactory: {0}",socketFactory); } /** *Connection基本方法信息 */ publicvoidseeConnection(){ /**用户管理*/ AccountManageraccountManager=connection.getAccountManager(); for(Stringattr: accountManager.getAccountAttributes()){ print("AccountAttribute: {0}",attr); } print("AccountInstructions: {0}",accountManager.getAccountInstructions()); /**是否链接*/ print("isConnected: ",connection.isConnected()); print("isAnonymous: ",connection.isAnonymous()); /**是否有权限*/ print("isAuthenticated: ",connection.isAuthenticated()); print("isSecureConnection: ",connection.isSecureConnection()); /**是否使用压缩*/ print("isUsingCompression: ",connection.isUsingCompression()); } /** *创建用户 *@paramusername *@parampassword */ publicvoidcreateUser(Stringusername,Stringpassword){ AccountManageraccountManager=connection.getAccountManager(); try{ /**创建一个用户;你可以在管理员控制台页面http: //10.100.53.65: 9090/user-summary.jsp查看用户/组的相关信息,来查看是否成功创建用户*/ accountManager.createAccount(username,password); /**修改密码*/ accountManager.changePassword("abc"); }catch(XMPPExceptione){ e.printStackTrace(); } } /** *修改密码 *@paramusername *@parampassword */ publicvoidchangePassword(Stringpassword){ AccountManageraccountManager=connection.getAccountManager(); try{ /**修改密码*/ accountManager.changePassword("123456"); }catch(XMPPExceptione){ e.printStackTrace(); } } /** *用户登陆 *@paramusername *@parampassword */ publicvoidlogin(Stringusername,Stringpassword){ try{ /**用户登陆,用户名、密码*/ connection.login(username,password); }catch(XMPPExceptione){ e.printStackTrace(); } /**获取当前登陆用户*/ print("User: ",connection.getUser()); /**所有用户组*/ Rosterroster=connection.getRoster(); /*try{ roster.createEntry("test55","test55",null);//添加好友,不添加成为好友也同样可以收发信息 }catch(XMPPExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }*/ /**好友用户组,你可以用Spark添加用户好友,这样这里就可以查询到相关的数据*/ Collection Iterator while(iter.hasNext()){ RosterEntryentry=iter.next(); print("Groups: {0},Name: {1},Status: {2},Type: {3},User: {4}",entry.getGroups(),entry.getName(),entry.getStatus(),entry.getType(),entry); } print("-------------------------------"); /**未处理、验证好友,添加过的好友,没有得到对方同意*/ Collection iter=unfiledEntries.iterator(); while(iter.hasNext()){ RosterEntryentry=iter.next(); print("Groups: {0},Name: {1},Status: {2},Type: {3},User: {4}",entry.getGroups(),entry.getName(),entry.getStatus(),entry.getType(),entry); } } /** *添加消息监听器 */ publicvoidaddMsgListener(){ //监听接收到的消息 connection.getChatManager().addChatListener(newChatManagerListener(){//可以监听到所有用户发来的消息 @Override publicvoidchatCreated(Chatchat,booleancreatedLocally){ chat.addMessageListener(ewMessageListener(){ publicvoidprocessMessage(Chatchat,Messagemessage){ //Stringfrom=message.getFrom(); //Stringbody=message.getBody(); System.out.println("body: "+message.getBody()); } }); } }); } /** *Packet消息聊天 *@paramfrom *@paramto *@parammsg */ publicvoidsendPacketMsg(Stringfrom,Stringto,Stringmsg){ //try{ //connection.login(from,from); //}catch(XMPPExceptione){ //e.printStackTrace(); //} /**更改用户状态,available=true表示在线,false表示离线,status状态签名;当你登陆后,在Spark客户端软件中就可以看到你登陆的状态*/ Presencepresence=newPresence(Presence.Type.available); presence.setStatus("Q我吧"); connection.sendPacket(presence); Sessionsession=newSession(); //Stringsessid=session.nextID(); connection.sendPacket(session); Messagemessage=newMessage(to+"@"+server,Type.chat); //Messagemessage=newMessage(sessid,Type.chat); message.setBody(msg); connection.sendPacket(message); } /** *Chat消息聊天 *@paramfrom *@paramto *@parammsg */ publicvoidsendChatMsg(Stringfrom,Stringto,Stringmsg){ /**设置状态*/ //try{ //connection.login(from,from); //}catch(XMPPExceptione){ //e.printStackTrace(); //} /**设置状态*/ Presencepresence=newPresence(Presence.Type.available); presence.setStatus("Q我吧"); connection.sendPacket(presence); /**获取当前登陆用户的聊天管理器*/ ChatManagerchatManager=connection.getChatManager(); /**为指定用户创建一个chat,监听对方发过来的消息*/ Chatchat=chatManager.createChat(to+"@"+server,newMessageListener(){//仅监听该用户发来的消息 @Override publicvoidprocessMessage(Chatchat,Messagemessage){ System.out.println("body: "+message.getBody()); } }); try{ /**发送消息*/ chat.sendMessage(msg); /**用message对象发送消息*/ //Messagemessage=newMessage(); //message.setBody("essage"); //message.setProperty("color","red"); //chat.sendMessage(message); }catch(XMPPExceptione){ e.printStackTrace(); } } } 创建两个通讯的测试帐号 [java]viewplaincopy publicstaticvoidmain(String[]args){ SmackUtilsmackUtil=newSmackUtil(); smackUtil.init(); smackUtil.seeConfig(); smackUtil.seeConnection(); smackUtil.createUser("test1","test1"); } [java]viewplaincopy publicstaticvoidmain(String[]args){ SmackUtilsmackUtil=newSmackUtil(); smackUtil.init(); smackUtil.seeConfig(); smackUtil.seeConnection(); smackUtil.createUser("test2","test2"); } 利用刚创建好的帐号来登陆并发送消息 test1登陆并发送消息: [java]viewplaincopy publicstaticvoidmain(String[]args){ SmackUtilsmackUtil=newSmackUtil(); smackUtil.init(); smackUtil.seeConfig(); smackUtil.seeConnection(); smackUtil.login("test1","test1"); smackUtil.addMsgListener(); smackUtil.sendChatMsg("test1","test2","1111111111111111111111111111111111111"); smackUtil.sendPacketMsg("test1","test2","1111111111111111111111111111111111111"); try{ Thread.sleep(1000*1000); }catch(InterruptedExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } test2登陆并发送消息: [java]viewplaincopy publicstaticvoidmain(String[]args){ SmackUtilsmackUtil=newSmackUtil(); smackUtil.init(); smackUtil.seeConfig(); smackUtil.seeConnection(); smackUtil.login("test2","test2"); smackUtil.addMsgListener(); smackUtil.sendChatMsg("test2","test1","22222222222222222222
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Openfire 即时通讯 服务 搭建 Smack 访问