以前学习过用Scoket 建立聊天,简单的建立聊天是没问题的,但如果要实现多人复杂的聊天,后台服务器代码就比较复杂,对于我这新手来讲就比较难了。后来在网上看到用openfire做服务器,利用强大的Smack API库来实现IM聊天就简单多了。
网上也有很多关于这方面的资料,我就在这里把网上的资料和自己的理解总结一下和大家一起交流学习。
需要用到以下几个工具:
Openfire 最新版本(openfire 3.8.1)
下载地址:
安装配置可参考:http://www.cnblogs.com/hoojo/archive/2012/05/17/2506769.html
Spark 最新版本(Spark 2.6.3)
下载地址:
ASmack 最新版本 (asmack-jse-buddycloud-2010.12.11.jar)
由于在Android平台下开发 所以我们用到的是 aSmack,与Smack没有很大的区别
下载地址1: (现在用的就是这个,修改了一些BUG)
下载地址2:
需要安装的安装好,需要下载的下载好(这里就不多作介绍)。创建项目工程,导入ASmack jar包,OK,下面就一起去学习怎么实现 用户注册、登录、修改密码和注销等一些功能!
首先第一、与服务器建立好连接
关键代码:127.0.0.1为服务器IP,5222为服务器端口号
-
-
-
-
-
-
public boolean conServer() {
-
ConnectionConfiguration config = new ConnectionConfiguration(
-
127.0.0.1, 5222);
-
-
config.setSASLAuthenticationEnabled(false);
-
-
-
-
try {
-
connection = new XMPPConnection(config);
-
-
connection.connect();
-
return true;
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
-
return false;
-
}
第二、实现注册
关键代码:
以下代码注释都很清楚了,如有不懂的地方可以留言,这里就不多做解释了。
-
-
-
-
-
-
-
-
public String regist(String account, String password) {
-
if (connection == null)
-
return "0";
-
Registration reg = new Registration();
-
reg.setType(IQ.Type.SET);
-
reg.setTo(ClientConServer.connection.getServiceName());
-
reg.setUsername(account);
-
reg.setPassword(password);
-
reg.addAttribute("android", "geolo_createUser_android");
-
PacketFilter filter = new AndFilter(new PacketIDFilter(
-
reg.getPacketID()), new PacketTypeFilter(IQ.class));
-
PacketCollector collector = ClientConServer.connection
-
.createPacketCollector(filter);
-
ClientConServer.connection.sendPacket(reg);
-
IQ result = (IQ) collector.nextResult(SmackConfiguration
-
.getPacketReplyTimeout());
-
-
collector.cancel();
-
if (result == null) {
-
Log.e("RegistActivity", "No response from server.");
-
return "0";
-
} else if (result.getType() == IQ.Type.RESULT) {
-
return "1";
-
} else {
-
if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
-
Log.e("RegistActivity", "IQ.Type.ERROR: "
-
+ result.getError().toString());
-
return "2";
-
} else {
-
Log.e("RegistActivity", "IQ.Type.ERROR: "
-
+ result.getError().toString());
-
return "3";
-
}
-
}
-
}
第三、实现登录
关键代码: 代码很简单不做太多解释
-
-
-
-
-
-
-
-
public boolean login(String a, String p) {
-
try {
-
if (connection == null)
-
return false;
-
-
connection.login(a, p);
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return false;
-
}
第四、修改密码
关键代码:同样很简单
-
-
-
-
-
-
public static boolean changePassword(XMPPConnection connection,String pwd)
-
{
-
try {
-
connection.getAccountManager().changePassword(pwd);
-
return true;
-
} catch (Exception e) {
-
return false;
-
}
-
}
第五、更改用户状态
关键代码:好吧,代码有点多,不过很清晰
-
-
-
-
public void setPresence(int code) {
-
if (connection == null)
-
return;
-
Presence presence;
-
switch (code) {
-
case 0:
-
presence = new Presence(Presence.Type.available);
-
connection.sendPacket(presence);
-
Log.v("state", "设置在线");
-
break;
-
case 1:
-
presence = new Presence(Presence.Type.available);
-
presence.setMode(Presence.Mode.chat);
-
connection.sendPacket(presence);
-
Log.v("state", "设置Q我吧");
-
System.out.println(presence.toXML());
-
break;
-
case 2:
-
presence = new Presence(Presence.Type.available);
-
presence.setMode(Presence.Mode.dnd);
-
connection.sendPacket(presence);
-
Log.v("state", "设置忙碌");
-
System.out.println(presence.toXML());
-
break;
-
case 3:
-
presence = new Presence(Presence.Type.available);
-
presence.setMode(Presence.Mode.away);
-
connection.sendPacket(presence);
-
Log.v("state", "设置离开");
-
System.out.println(presence.toXML());
-
break;
-
case 4:
-
Roster roster = connection.getRoster();
-
Collection entries = roster.getEntries();
-
for (RosterEntry entry : entries) {
-
presence = new Presence(Presence.Type.unavailable);
-
presence.setPacketID(Packet.ID_NOT_AVAILABLE);
-
presence.setFrom(connection.getUser());
-
presence.setTo(entry.getUser());
-
connection.sendPacket(presence);
-
System.out.println(presence.toXML());
-
}
-
-
presence = new Presence(Presence.Type.unavailable);
-
presence.setPacketID(Packet.ID_NOT_AVAILABLE);
-
presence.setFrom(connection.getUser());
-
presence.setTo(StringUtils.parseBareAddress(connection.getUser()));
-
connection.sendPacket(presence);
-
Log.v("state", "设置隐身");
-
break;
-
case 5:
-
presence = new Presence(Presence.Type.unavailable);
-
connection.sendPacket(presence);
-
Log.v("state", "设置离线");
-
break;
-
default:
-
break;
-
}
-
}
第六:注销
关键代码:很简单吧!
-
-
-
-
-
-
public static boolean deleteAccount(XMPPConnection connection)
-
{
-
try {
-
connection.getAccountManager().deleteAccount();
-
return true;
-
} catch (Exception e) {
-
return false;
-
}
-
}
好吧,这篇就先介绍到这!
阅读(1898) | 评论(0) | 转发(0) |