先解釋幾個名詞
XMPP
: (eXtensible Messaging and Presence Protocol) XMPP的前身是Jabber,一個開源形式組織產生的網絡即時通信協議。XMPP目前被IETF國際標準組織完成了標準化工作。是目前主流的四種IM(IM:instant messaging,即時消息)協議之一,其他三種分別為:IMPP、PRIM、SIP(SIMPLE)。
XIFF : XMPP Implementation For Flash. (一個ActionScript的XMPP類庫)
Openfire : (原名Wildfire) 基于Java的開源實時協作(RTC)服務器,使用XMPP(Jabber)協議。
Google推出的Google Talk就是基于XMPP的IM軟件。所以我想使用Flex也開發一個基于XMPP的聊天程序。現在已經有了很好的開源服務器Openfire
http://www.igniterealtime.org/projects/openfire/index.jsp
支持中文哦。
另外還有一套XIFF API,專為flash開發XMPP應用,但現在XIFF2.0是用AS2寫的,而Flex2是基于AS3的,幸運的是有人已經寫了一個基于AS3的實現。
http://svn.igniterealtime.org/svn/repos/xiff/branches/xiff_as3_flexlib_beta1/
要開發Flex + Openfire的系統,首先要安裝Openfire服務器,安裝很簡單,具體看安裝手冊 http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/install-guide.html
安裝在本機的Openfire啟動后,可以通過 http://localhost:9090 ?管理
好,現在開始開發Flex客戶端。因為現在XIFF_AS3的文檔幾乎沒有,所以只能通過XIFF2的例子,和Smack API(for java的)來學習。XIFF的核心是XMPPConnection,它基本是圍繞flash.net.XMLSocket寫的。首先創建一個Flex項目并引入 XIFF.swc
登錄服務器比較簡單
var connection : XMPPConnection = new XMPPConnection();
connection.server = 服務器名
connection.port = 服務器端口號
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
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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