項目下載地址:https://github.com/liuwenhaoCN/openfire/tree/websocketlinux
老版本是須要安裝websocket插件的。以前版本就不說了。git
<script src="lib/XML/ObjTree.js"></script> <script src="lib/XML/jkl-dumper.js"></script>
自行百度下載新版本。我使用的版本上傳至百度網盤:github
連接: https://pan.baidu.com/s/1ALWJPAbb14yKtsY_CTj1Gg 提取碼: vfc3web
使用方法:json
//json轉xml function json2xml(jsonstring) { var xotree = new XML.ObjTree(); var xml = xotree.writeXML(jsonstring); //使用jkl-dumper.js中的formatXml方法將xml字符串格式化 //var xmlText = formatXml(xml); return xml; } //xml轉json function xml2json(xmlstring) { //將xml字符串轉爲json var xotree = new XML.ObjTree(); var json = xotree.parseXML(xmlstring); //將json對象轉爲格式化的字符串 var dumper = new JKL.Dumper(); var jsonText = dumper.dump(json); return JSON.parse(jsonText); }
在http://192.168.2.11:9090/index.jsp 首頁面能夠看到如下信息:安全
服務器屬性 服務器啓動時間: 20 天, 23 小時, 22 分鐘 -- started 2018-9-5 15:44:47 版本: Openfire 4.2.2 Openfire 主目錄: /var/openfire 服務器名稱: lvs-backup-1 環境 JVM 版本和供應商: 1.8.0_65 Oracle Corporation -- Java HotSpot(TM) 64-Bit Server VM 應用服務器: jetty/9.2.z-SNAPSHOT 主機名: lvs-backup-1 OS/硬件: Linux / amd64 語言環境/時區: zh_CN / 中國標準時間 (8 GMT) 服務器端口 全部的地址 7070 HTTP綁定 端口用於客戶端使用不安全的HTTP方式訪問 全部的地址 7443 端口用於網絡上以SSL加密的通訊,HTTP綁定端口用於客戶端使用安全的HTTP方式訪問
//地址 var host = "ws://192.168.2.11:7070/ws/"; //建立websocket, OpenFire是實現了WebSocket的子協議 websocket = new WebSocket(host, 'xmpp'); //鏈接成功創建的回調方法 websocket.onopen = wsopen; //鏈接發生錯誤的回調方法 websocket.onerror = wserror; //鏈接關閉的回調方法 websocket.onclose = wsclose; //收到消息時執行的方法 websocket.onmessage = wsmessage; 當websocket鏈接成功,就須要發起創建流的請求
<?xml version="1.0" encoding="UTF-8" ?> <open to="lvs-backup-1" from="AA@lvs-backup-1" xmlns="urn:ietf:params:xml:ns:xmpp-framing" xml:lang="zh" version="1.0" />
//域名 var to = "lvs-backup-1"; //JID var from = AA@lvs-backup-1; var xmlns = "urn:ietf:params:xml:ns:xmpp-framing"; var version = "1.0"; var xmllang = "zh"; var resource = "appClient"; //發起創建流的請求 function connwsopen() { var temp = { "open": { "-to": to, "-from": from, "-xmlns": xmlns, "-xml:lang": xmllang, "-version": version } }; //轉化爲xml var steam = json2xml(temp); websocket.send(steam); }
服務器返回贊成創建流服務器
<open to='AA@lvs-backup-1' from='lvs-backup-1' id='1g0xa0mr98' xmlns='urn:ietf:params:xml:ns:xmpp-framing' xml:lang='zh' version='1.0'/>
安全驗證的方式,使用PLAIN方式,這種方式是把用戶名和密碼經過BASE64加密後傳輸。websocket
<stream:features xmlns:stream='http://etherx.jabber.org/streams'><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism><mechanism>SCRAM-SHA-1</mechanism><mechanism>CRAM-MD5</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms></stream:features>
使用PLAIN方式,須要對用戶名和密碼經過BASE64加密網絡
字符串格式是:jid+password,以\0做爲分隔符 var temp = username + "@" + to + "\0" + password; //Base64編碼 var token = window.btoa(temp);
<?xml version="1.0" encoding="UTF-8" ?> <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">MTUyNzUwNzYzNTNAbHZzLWJhY2t1cC0xAGUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNl</auth>
//登陸驗證 function validatelogin() { //字符串格式是:jid+password,以\0做爲分隔符 var temp = username + "@" + to + "\0" + password; //Base64編碼 var token = window.btoa(temp); var message = { "auth": { "-xmlns": "urn:ietf:params:xml:ns:xmpp-sasl", "-mechanism": "PLAIN", "#text": token } }; websocket.send(json2xml(message)); }
登陸成功:session
<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
登陸失敗:
<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
登陸驗證成功後,須要再發起一個新的流,新的流服務端會給予一些新的XML節點權限(、),這樣才能發送一些其餘功能的信息,好比發送消息,獲取聯繫人列表,剛開始創建的第一個流失不能發送這些節點的。
在發起新的流以前,須要獲取到第一個流的id,這個id就是與websocket的會話差很少。
<?xml version="1.0" encoding="UTF-8" ?> <open xmlns="jabber:client" to="lvs-backup-1" version="1.0" from="AA@lvs-backup-1" id="1g0xa0mr98" xml:lang="zh" />
id = "1g0xa0mr98"; 第一個流的id //發起新的流 function newopen() { var temp = { "open": { "-xmlns": "jabber:client", "-to": to, "-version": version, "-from": from, "-id": id, "-xml:lang": xmllang } }; //轉化爲xml var steam = json2xml(temp); websocket.send(steam); }
贊成打開新流
<open to='AA@lvs-backup-1' from='lvs-backup-1' id='1g0xa0mr98' xmlns='urn:ietf:params:xml:ns:xmpp-framing' xml:lang='zh' version='1.0'/>
接下來須要作bind操做
<stream:features xmlns:stream='http://etherx.jabber.org/streams'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/><session xmlns='urn:ietf:params:xml:ns:xmpp-session'><optional/></session><sm xmlns='urn:xmpp:sm:3'/></stream:features>
綁定操做,也須要id的,在綁定的時候,能夠增長客戶端的描述。
appClient:是app的客戶端
<?xml version="1.0" encoding="UTF-8" ?> <iq id="1g0xa0mr98" type="set"> <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"> <resource>appClient</resource> </bind> </iq>
//bind操做 function bind() { var temp = { "iq": { "-id": id, "-type": "set", "bind": { "-xmlns": "urn:ietf:params:xml:ns:xmpp-bind", "resource":resource } } }; //轉化爲xml var steam = json2xml(temp); websocket.send(steam); }
<iq xmlns="jabber:client" type="result" id="1g0xa0mr98" to="lvs-backup-1/8itpfrge4k"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>AA@lvs-backup-1/appClient</jid></bind></iq>
<?xml version="1.0" encoding="UTF-8" ?> <iq xmlns="jabber:client" id="1g0xa0mr98" type="set"> <session xmlns="urn:ietf:params:xml:ns:xmpp-session" /> </iq>
//獲取session function getsession() { var temp = { "iq": { "-xmlns": "jabber:client", "-id": id, "-type": "set", "session": {"-xmlns": "urn:ietf:params:xml:ns:xmpp-session"} } }; //轉化爲xml var steam = json2xml(temp); websocket.send(steam); }
<iq xmlns="jabber:client" type="result" id="1g0xa0mr98" to="AA@lvs-backup-1/appClient"/>
這個時候,在web頁面會話中看到的,帳號仍是離線狀態。對應的消息也收不到。下一步就須要作上線的操做了。
<?xml version="1.0" encoding="UTF-8" ?> <presence id="1g0xa0mr98"> <status>online</status> <priority>1</priority> </presence>
priority:設置優先級的 //上線 function presence() { var temp = { "presence": { "-id": id, "status": "online", "priority": "1" } }; //轉化爲xml var steam = json2xml(temp); websocket.send(steam); }
會把對應的好友發送過來
<presence xmlns="jabber:client" from="BB@lvs-backup-1" to="AA@lvs-backup-1" type="subscribe"/> <presence xmlns="jabber:client" from="CC@lvs-backup-1" to="AA@lvs-backup-1" type="subscribe"/>
這時候再看web中的會話,該帳號就在線了。
body:對應的消息內容 <?xml version="1.0" encoding="UTF-8" ?> <message type="chat" from="AA@lvs-backup-1" to="BB@lvs-backup-1"> <subject>標題</subject> <body>你好</body> </message>
number:給誰發 message:消息內容 //發消息 function send(number,message) { var temp = { "message": { "-type":"chat", "-from": username+"@"+to, "-to": number+"@"+to, "subject":"標題", "body": message } }; //轉化爲xml var steam = json2xml(temp); websocket.send(steam); }
用戶名: 15275076353 密碼: e10adc3949ba59abbe56e057f20f883e 正在鏈接.. 鏈接已創建 發起創建流的請求: <?xml version="1.0" encoding="UTF-8" ?> <open to="lvs-backup-1" from="15275076353@lvs-backup-1" xmlns="urn:ietf:params:xml:ns:xmpp-framing" xml:lang="zh" version="1.0" /> 接收到的 <open to='15275076353@lvs-backup-1' from='lvs-backup-1' id='3r01w7mpew' xmlns='urn:ietf:params:xml:ns:xmpp-framing' xml:lang='zh' version='1.0'/> 接收到的 <stream:features xmlns:stream='http://etherx.jabber.org/streams'><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"> <mechanism>PLAIN</mechanism><mechanism>SCRAM-SHA-1</mechanism><mechanism>CRAM-MD5</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms> </stream:features> 登陸驗證: <?xml version="1.0" encoding="UTF-8" ?> <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">MTUyNzUwNzYzNTNAbHZzLWJhY2t1cC0xAGUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNl</auth> 接收到的 <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/> 登陸成功! 發起新的流: <?xml version="1.0" encoding="UTF-8" ?> <open xmlns="jabber:client" to="lvs-backup-1" version="1.0" from="15275076353@lvs-backup-1" id="3r01w7mpew" xml:lang="zh" /> 接收到的 <open to='15275076353@lvs-backup-1' from='lvs-backup-1' id='3r01w7mpew' xmlns='urn:ietf:params:xml:ns:xmpp-framing' xml:lang='zh' version='1.0'/> 接收到的 <stream:features xmlns:stream='http://etherx.jabber.org/streams'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/> <session xmlns='urn:ietf:params:xml:ns:xmpp-session'><optional/></session><sm xmlns='urn:xmpp:sm:3'/></stream:features> bind操做: <?xml version="1.0" encoding="UTF-8" ?> <iq id="3r01w7mpew" type="set"> <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"> <resource>appClient</resource> </bind> </iq> 接收到的 <iq xmlns="jabber:client" type="result" id="3r01w7mpew" to="lvs-backup-1/3r01w7mpew"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>15275076353@lvs-backup-1/appClient</jid></bind></iq> 獲取session: <?xml version="1.0" encoding="UTF-8" ?> <iq xmlns="jabber:client" id="3r01w7mpew" type="set"> <session xmlns="urn:ietf:params:xml:ns:xmpp-session" /> </iq> 接收到的 <iq xmlns="jabber:client" type="result" id="3r01w7mpew" to="15275076353@lvs-backup-1/appClient"/> 上線: <?xml version="1.0" encoding="UTF-8" ?> <presence id="3r01w7mpew"> <status>online</status> <priority>1</priority> </presence> 接收到的 <presence xmlns="jabber:client" from="11112222333@linux" to="15275076353@lvs-backup-1" type="subscribe"/> 接收到的 <presence xmlns="jabber:client" from="13121372109@linux" to="15275076353@lvs-backup-1" type="subscribe"/>