先解釋幾個名詞
XMPP
: (eXtensible Messaging and Presence Protocol) XMPP的前身是Jabber,一個開源形式組織產(chǎn)生的網(wǎng)絡(luò)即時通信協(xié)議。XMPP目前被IETF國際標(biāo)準(zhǔn)組織完成了標(biāo)準(zhǔn)化工作。是目前主流的四種IM(IM:instant messaging,即時消息)協(xié)議之一,其他三種分別為:IMPP、PRIM、SIP(SIMPLE)。
XIFF : XMPP Implementation For Flash. (一個ActionScript的XMPP類庫)
Openfire : (原名Wildfire) 基于Java的開源實時協(xié)作(RTC)服務(wù)器,使用XMPP(Jabber)協(xié)議。
Google推出的Google Talk就是基于XMPP的IM軟件。所以我想使用Flex也開發(fā)一個基于XMPP的聊天程序。現(xiàn)在已經(jīng)有了很好的開源服務(wù)器Openfire
http://www.igniterealtime.org/projects/openfire/index.jsp
支持中文哦。
另外還有一套XIFF API,專為flash開發(fā)XMPP應(yīng)用,但現(xiàn)在XIFF2.0是用AS2寫的,而Flex2是基于AS3的,幸運的是有人已經(jīng)寫了一個基于AS3的實現(xiàn)。
http://svn.igniterealtime.org/svn/repos/xiff/branches/xiff_as3_flexlib_beta1/
要開發(fā)Flex + Openfire的系統(tǒng),首先要安裝Openfire服務(wù)器,安裝很簡單,具體看安裝手冊 http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/install-guide.html
安裝在本機(jī)的Openfire啟動后,可以通過 http://localhost:9090 ?管理
好,現(xiàn)在開始開發(fā)Flex客戶端。因為現(xiàn)在XIFF_AS3的文檔幾乎沒有,所以只能通過XIFF2的例子,和Smack API(for java的)來學(xué)習(xí)。XIFF的核心是XMPPConnection,它基本是圍繞flash.net.XMLSocket寫的。首先創(chuàng)建一個Flex項目并引入 XIFF.swc
登錄服務(wù)器比較簡單
var connection : XMPPConnection = new XMPPConnection();
connection.server = 服務(wù)器名
connection.port = 服務(wù)器端口號
connection.username = 用戶名
connection.password = 密碼
connection.connect("flash");
下面是一個簡單的小例子
- <? xml ? version = "1.0" ? encoding = "utf-8" ?> ??
- < mx:Application ? xmlns:mx = "http://www.adobe.com/2006/mxml" ? layout = "absolute" ??
- ???? creationComplete = "initApp()" > ??
- ??
- ???? < mx:Script > ??
- ????????[CDATA[ ??
- ????????????import?org.igniterealtime.xiff.events.RoomEvent; ??
- ????????????import?mx.controls.Alert; ??
- ????????????import?org.igniterealtime.xiff.events.MessageEvent; ??
- ????????????import?org.igniterealtime.xiff.core.XMPPConnection; ??
- ????????????import?org.igniterealtime.xiff.conference.Room; ??
- ????????????import?org.igniterealtime.xiff.events.LoginEvent; ??
- ????????????import?org.igniterealtime.xiff.data.*; ??
- ???????????? ??
- ????????????public?const?SERVER_NAME?:? String ?=? "wangcheng" ; ??
- ????????????public?const?CHATROOM?:? String ?=? "chatRoom1" ; ??
- ???????????? ??
- ????????????private?var?chatRoom?:?Room; ??
- ????????????private?var?connection?:?XMPPConnection; ??
- ???????????? ??
- ????????????private?function?initApp():void?{ ??
- ???????????????? connection ?=? new ?XMPPConnection(); ??
- ????????????????connection.addEventListener(LoginEvent.LOGIN,?onLogin); ??
- ????????????} ??
- ???????????? ??
- ????????????private?function?doLogin():void?{ ??
- ????????????????if?(!connection.isLoggedIn())?{ ??
- ???????????????????? connection.username ?=?username.text; ??
- ???????????????????? connection.password ?=?password.text; ??
- ???????????????????? connection.server ?=? SERVER_NAME ; ??
- ???????????????????? connection.port ?=? 5222 ; ??
- ????????????????????connection.connect("flash"); ??
- ????????????????????if(connection.isLoggedIn()){ ??
- ????????????????????????chatContent.htmlText?+=?"Welcome?"?+?username.text?+?" < br /> "; ??
- ????????????????????} ??
- ????????????????}?else?{ ??
- ????????????????????connection.disconnect(); ??
- ????????????????} ??
- ????????????} ??
- ???????????? ??
- ????????????private?function?onLogin(event):void?{ ??
- ???????????????? inputMsg.enabled ?=? true ; ??
- ???????????????? sendBtn.enabled ?=? true ; ??
- ??
- ???????????????? chatRoom ?=? new ?Room(?connection?); ??
- ????????????????chatRoom.setRoomJID(connection.getJID()); ??
- ???????????????? chatRoom.roomName ?=? CHATROOM ; ??
- ???????????????? chatRoom.nickname ?=? connection .username; ??
- ???????????????? chatRoom.conferenceServer ?=? "conference." ?+?SERVER_NAME; ??
- ???????????????? ??
- ????????????????chatRoom.join(); ??
- ????????????????chatRoom.addEventListener(RoomEvent.GROUP_MESSAGE,?groupMessage); ??
- ????????????} ??
- ???????????? ??
- ????????????private?function?groupMessage(event):void?{ ??
- ????????????????displayUserMessage(getNickName(event.data.from)?,?event.data.body?); ??
- ????????????} ??
- ??
- ????????????private?function?getNickName(jid?:?String)?:?String?{ ??
- ????????????????var? name ?=? jid .split("/")[1]; ??
- ????????????????if?( name ?==?null)?{ ??
- ???????????????????? name ?=? "Message" ; ??
- ????????????????} ??
- ????????????????return?name; ??
- ????????????} ??
- ???????????? ??
- ????????????private?function?displayUserMessage(user:String,?message:String)?:?void?{ ??
- ??
- ????????????????var?fontColor?:? String ?=? "#002bd2" ; ??
- ????????????????if?( user ?==?chatRoom.nickname)?{ ??
- ???????????????????? fontColor ?=? "#8e2800" ; ??
- ????????????????} ??
- ??
- ????????????????chatContent.htmlText?+=?" < font ? color = '"?+?fontColor?+?"' > < b > "?+?user?+?": </ b > ?"?+?message?+?" </ font > < br ? /> "; ??
- ????????????} ??
- ???????????? ??
- ????????????private?function?sendMsg():void?{ ??
- ????????????????if?(inputMsg.text?!=?"")?{ ??
- ????????????????????chatRoom.sendMessage(inputMsg.text); ??
- ???????????????????? inputMsg.text = "" ; ??
- ????????????????} ??
- ????????????} ??
- ??
- ????????]] ??
- ???? </ mx:Script > ??
- ??
- ??
- ???? < mx:Label ? x = "10" ? y = "10" ? text = "UserName" /> ??
- ???? < mx:TextInput ? id = "username" ? x = "80" ? y = "8" ? width = "92" /> ??
- ???? < mx:Label ? x = "180" ? y = "10" ? text = "Password" /> ??
- ???? < mx:TextInput ? id = "password" ? x = "244" ? y = "8" ? width = "99" ? displayAsPassword = "true" /> ??
- ???? < mx:Button ? x = "351" ? y = "8" ? label = "Login" ? click = "doLogin()" /> ??
- ???? < mx:TextArea ? id = "chatContent" ? x = "10" ? y = "36" ? width = "397" ? height = "171" /> ??
- ???? < mx:TextInput ? id = "inputMsg" ? enabled = "false" ? x = "12" ? y = "215" ? width = "333" ? enter = "sendMsg()" ? /> ??
- ???? < mx:Button ? id = "sendBtn" ? enabled = "false" ? x = "353" ? y = "215" ? label = "Send" ? click = "sendMsg()" ? /> ??
- </ mx:Application > ??
?
參考
http://www.dgrigg.com/post.cfm/09/05/2006/XIFF-Actionscript-3-for-Flex-2
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
