首先我用的是Openfire版本是:3.10.3 下載地址Openfire ; 安裝就不說了, 安裝完成須要作的設置 1.安裝2個插件css
在openfire 有效的插件裏有 這2個插件都是用來作WEB端用戶添加、更新、查詢openfire帳號和查詢用戶狀態的html
PHP 端 方法就放出來一個 還有都是業務端的 插件都有文檔你們能夠本身去看看 基本業務都是和插件有關的jquery
/** *//這個須要安裝OPenfire User Service插件 * 獲取用戶當前在線狀態 * * @param unknown $username * @return mixed text[空閒、在線、離開、電話中、正忙、unavailable] */ function get_of_user_status($username) { $_CFG = $GLOBALS['_CFG'];//設置的全局變量 $of_ip = $_CFG['chat_server_ip'];//openfire 服務器地址 $of_port = $_CFG['chat_server_port'];//插件端口通常默認9090 $of_url = get_of_url($of_ip, $of_port); $of_domain = $_CFG['chat_server_ip'];//這個是openfire 服務器名稱 $url = $of_url.'/plugins/presence/status?jid='.$username.'@'.$of_domain.'&type=xml'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // 設置能夠讀取返回值 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 運行curl $result = curl_exec ( $ch ); // 關閉 curl_close ( $ch ); $xml = simplexml_load_string($result); //$type = $xml->attributes()->type; $type=$xml->show; if(!empty($type)) { //修改stat switch($type){ case "chat": $type="空閒"; break; case "away": $type="離開"; break; case "away": $type="離開"; break; case "dnd": $type="勿擾"; break; case "xa": $type="忙碌"; break; } //end return (string)$type; }else{ if(!empty($xml->status)) { $status = $xml->status; return (string)$status; } $type="在線"; return (string)$type; } //var_dump($xml); return 'unavailable'; }
// WEB 端測試JS test.js服務器
// XMPP服務器BOSH地址 var BOSH_SERVICE = 'http://服務器地址:7070/http-bind/'; // XMPP鏈接 var connection = null; // 當前狀態是否鏈接 var connected = false; // 當前登陸的JID var jid = ""; // 鏈接狀態改變的事件 function onConnect(status) { console.log(status) if (status == Strophe.Status.CONNFAIL) { alert("鏈接失敗!"); } else if (status == Strophe.Status.AUTHFAIL) { alert("登陸失敗!"); } else if (status == Strophe.Status.DISCONNECTED) { alert("鏈接斷開!"); connected = false; } else if (status == Strophe.Status.CONNECTED) { alert("鏈接成功,能夠開始聊天了!"); connected = true; // 當接收到<message>節,調用onMessage回調函數 connection.addHandler(onMessage, null, 'message', null, null, null); // 首先要發送一個<presence>給服務器(initial presence) connection.send($pres().tree()); } } // 接收到<message> function onMessage(msg) { // 解析出<message>的from、type屬性,以及body子元素 var from = msg.getAttribute('from'); var type = msg.getAttribute('type'); var elems = msg.getElementsByTagName('body'); if (type == "chat" && elems.length > 0) { var body = elems[0]; $("#msg").append(from + ":<br>" + Strophe.getText(body) + "<br>") } return true; } $(document).ready(function() { // 經過BOSH鏈接XMPP服務器 $('#btn-login').click(function() { if(!connected) { connection = new Strophe.Connection(BOSH_SERVICE); connection.connect($("#input-jid").val(), $("#input-pwd").val(), onConnect); jid = $("#input-jid").val(); } }); // 發送消息 $("#btn-send").click(function() { if(connected) { if($("#input-contacts").val() == '') { alert("請輸入聯繫人!"); return; } // 建立一個<message>元素併發送 var msg = $msg({ to: $("#input-contacts").val(), from: jid, type: 'chat' }).c("body", null, $("#input-msg").val()); connection.send(msg.tree()); $("#msg").append(jid + ":<br>" + $("#input-msg").val() + "<br>"); $("#input-msg").val(''); } else { alert("請先登陸!"); } }); });
html併發
<!DOCTYPE html> <html> <head> <script src='http://cdn.bootcss.com/jquery/1.9.1/jquery.min.js'></script> <script src='http://cdn.bootcss.com/strophe.js/1.1.3/strophe.min.js'></script> <!--這裏用CDN能夠直接用--> <script src='test.js'></script> </head> <body> JID:<input type="text" id="input-jid"> <br> 密碼:<input type="password" id="input-pwd"> <br> <button id="btn-login">登陸</button> <div id="msg" style="height: 400px; width: 400px; overflow: scroll;"></div> 聯繫人JID: <input type="text" id="input-contacts"> <br> 消息: <br> <textarea id="input-msg" cols="30" rows="4"></textarea> <br> <button id="btn-send">發送</button> </body> </html>
實現的效果圖 這個HTMLWEB客戶端 app