public String SendPresence(String from, String to, String type,String Status) {String domain = JiveGlobals.getProperty("xmpp.domain");StringBuffer result = new StringBuffer();result.append("<?xml version='1.0' encoding='UTF-8'?>");result.append("<SendPresence>");result.append("<Presence>");result.append("<From>" + from + "</From>");result.append("<To>" + to + "</To>");result.append("<msg>" + type + "</msg>");result.append("<Domain>" + domain + "</Domain>");result.append("</Presence>");result.append("<result>");XMPPServer server = XMPPServer.getInstance();SessionManager sessionManager = server.getSessionManager();PresenceRouter presenceRouter=server.getPresenceRouter();Presence presence = new Presence();presence.setFrom(from);presence.setTo(to+"@"+domain);//空闲 在线presence.setStatus(Status);if("0".equals(type)){//聊天框上面的状态presence.setShow(Presence.Show.away);}else{presence.setShow(Presence.Show.chat);}result.append("1");try {presenceRouter.route(presence);for (ClientSession cs : sessionManager.getSessions()) {cs.process(new NovonOpenfireStatusIQ(1, presence.getStatus(),cs.getAddress(), presence.getFrom()));}} catch (Exception e) {Log.error("WebService SendPresence:" + e);result.append("0");}result.append("</result>");result.append("</SendPresence>");return result.toString();}
其中 NovonOpenfireStatusIQ 定义为 如下
import jit.base.dom.common.ElementName;
import jit.base.dom.common.NameSpace;
import jit.base.dom.status.StatusNode;
import jit.base.dom.status.StatusRoot;import org.dom4j.Element;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;public class NovonOpenfireStatusIQ extends IQ {public NovonOpenfireStatusIQ(int available, String status, JID to, JID from) {this.setTo(to);this.setFrom(from);init(available, status);}void init(int available, String status) {Element childElement;childElement = setChildElement(ElementName.PRESENCECHANGED, NameSpace.STATUS_CHANGED);StatusRoot sroot = new StatusRoot();StatusNode statusNode = new StatusNode(getFrom().toString(), available, status);sroot.add(statusNode);childElement.add(sroot.getXml());}
}