在最近一个新的项目里面要用openfire很多的自带功能,比如:加好友,加聊天室,发消息,发送文件,获取组消息,更新状态等。虽然用c++开发,但是先用java实现一下。smack可以说把openfire有设计的那些交互协议都封装的很完整,在消息服务器,其实很好实现各种功能,说到底就是协议的定义,openfire自己定义了这几种协议来实现这些功能,smack把这些协议封装成对应的对象去处理,让开发者更容易上手。
1.发消息:
public boolean sendMessage(String toJid,String content){
Message message = new Message();
message.setFrom(connectManager.getJid());
message.setTo(toJid);
message.setBody(content);
message.setType(Message.Type.chat);
message.setSubject(MSG_SUBJECT);//消息主题
BaseInfo baseInfo = new BaseInfo();
baseInfo.setRecvName("接收人");
baseInfo.setSendName("发送人");
message.addExtension(baseInfo);//消息包的扩展
return connectManager.sendPacket(message);
}
扩展包的java:
import org.jivesoftware.smack.packet.PacketExtension;
public class BaseInfo implements PacketExtension{
private String sendName;
private String recvName;
public String getRecvName() {
return recvName;
}
public void setRecvName(String recvName) {
this.recvName = recvName;
}
public String getSendName() {
return sendName;
}
public void setSendName(String sendName) {
this.sendName = sendName;
}
public String getElementName() {
return "base_info";
}
public String getNamespace() {
return "uc:staff_name";
}
<span style="white-space:pre"> </span>//重写xml方法
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
"\">");
if (getSendName() != null) {
buf.append("<send_name>").append(getSendName()).append("</send_name>");
}
if (getRecvName() != null) {
buf.append("<rec_name>").append(getRecvName()).append("</rec_name>");
}
buf.append("</").append(getElementName()).append(">");
return buf.toString();
}
}
2.修改状态
/**
* 修改状态
* @param state
* @return
*/
public boolean sendPresence(String state){
return sendPresence(state, null);
}
/**
* 针对某人修改状态
* @param state
* @param toJid
* @return
*/
public boolean sendPresence(String state,String toJid){
Presence.Type type = Presence.Type.available;
Presence.Mode mode =null;
String status =null;
if("1".equals(state)){
mode = Presence.Mode.chat;
status = "空闲";
}else if("2".equals(state)){
mode = Presence.Mode.xa;
status="离开一段时间";
}else if("3".equals(state)){
mode = Presence.Mode.away;
status="离开";
}else if("4".equals(state)){
mode = Presence.Mode.dnd;
status="正忙";
}else if("5".equals(state)){
type = Presence.Type.unavailable;
status = "隐身";
}else{
return false;
}
Presence presence = new Presence(type);
presence.setTo(toJid);
presence.setMode(mode);
presence.setStatus(status);
return connectManager.sendPacket(presence);
}
3.对Roster中好友组的操作
/**
* 返回所有组信息
* @return List(RosterGroup)
*/
public List<RosterGroup> getGroups() {
List<RosterGroup> groupsList = new ArrayList<RosterGroup>();
Collection<RosterGroup> rosterGroup = roster.getGroups();
Iterator<RosterGroup> i = rosterGroup.iterator();
while (i.hasNext()){
RosterGroup group = i.next();
groupsList.add(group);
log.debug("获取组信息:name:"+group.getName());
}
return groupsList;
}
/**
* 返回相应(groupName)组里的所有用户
*
* @return List(RosterEntry)
*/
public List<RosterEntry> getEntriesByGroup(String groupName) {
List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
RosterGroup rosterGroup = roster.getGroup(groupName);
if(rosterGroup !=null){
Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
Iterator<RosterEntry> i = rosterEntry.iterator();
while (i.hasNext()){
RosterEntry entry = i.next();
EntriesList.add(entry);
log.debug("获取好友信息:name:"+entry.getName()+",status:"+entry.getStatus()+",group:"+entry.getGroups()+",type:"+entry.getType()+",user:"+entry.getUser());
}
}
return EntriesList;
}
/**
* 返回所有用户信息
*
* @return List(RosterEntry)
*/
public List<RosterEntry> getAllEntries() {
List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
Collection<RosterEntry> rosterEntry = roster.getEntries();
Iterator<RosterEntry> i = rosterEntry.iterator();
while (i.hasNext()){
RosterEntry entry = i.next();
EntriesList.add(entry);
log.debug("获取好友信息:name:"+entry.getName()+",status:"+entry.getStatus()+",group:"+entry.getGroups()+",type:"+entry.getType()+",user:"+entry.getUser());
}
return EntriesList;
}
4.对Roster好友的操作
/**
* 添加一个好友无分组
*/
public boolean addUser(String userName,String name)
{
try {
roster.createEntry(userName, name, null);
return true;
} catch (Exception e) {
log.error("添加一个好友无分组异常",e);
return false;
}
}
/**
* 添加一个好友到分组
* @param roster
* @param userName
* @param name
* @return
*/
public boolean addUser(String userName,String name,String groupName)
{
try {
roster.createEntry(userName, name,new String[]{groupName});
return true;
} catch (Exception e) {
log.error("添加一个好友到分组异常",e);
return false;
}
}
/**
* 删除一个好友
* @param roster
* @param userName
* @return
*/
public boolean removeUser(String userName){
try {
RosterEntry entry = roster.getEntry(userName);
roster.removeEntry(entry);
log.debug("删除一个好友:"+entry.getName());
return true;
} catch (Exception e) {
log.error("删除一个好友异常",e);
return false;
}
}
把聊天室的操作以及各种事件监听器放到下一章。