亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Flex2基于XMPP的chat程序 (一)

系統 2044 0

先解釋幾個名詞
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 代碼
  1. <? xml ? version = "1.0" ? encoding = "utf-8" ?> ??
  2. < mx:Application ? xmlns:mx = "http://www.adobe.com/2006/mxml" ? layout = "absolute" ??
  3. ???? creationComplete = "initApp()" > ??
  4. ??
  5. ???? < mx:Script > ??
  6. ????????[CDATA[ ??
  7. ????????????import?org.igniterealtime.xiff.events.RoomEvent; ??
  8. ????????????import?mx.controls.Alert; ??
  9. ????????????import?org.igniterealtime.xiff.events.MessageEvent; ??
  10. ????????????import?org.igniterealtime.xiff.core.XMPPConnection; ??
  11. ????????????import?org.igniterealtime.xiff.conference.Room; ??
  12. ????????????import?org.igniterealtime.xiff.events.LoginEvent; ??
  13. ????????????import?org.igniterealtime.xiff.data.*; ??
  14. ???????????? ??
  15. ????????????public?const?SERVER_NAME?:? String ?=? "wangcheng" ; ??
  16. ????????????public?const?CHATROOM?:? String ?=? "chatRoom1" ; ??
  17. ???????????? ??
  18. ????????????private?var?chatRoom?:?Room; ??
  19. ????????????private?var?connection?:?XMPPConnection; ??
  20. ???????????? ??
  21. ????????????private?function?initApp():void?{ ??
  22. ???????????????? connection ?=? new ?XMPPConnection(); ??
  23. ????????????????connection.addEventListener(LoginEvent.LOGIN,?onLogin); ??
  24. ????????????} ??
  25. ???????????? ??
  26. ????????????private?function?doLogin():void?{ ??
  27. ????????????????if?(!connection.isLoggedIn())?{ ??
  28. ???????????????????? connection.username ?=?username.text; ??
  29. ???????????????????? connection.password ?=?password.text; ??
  30. ???????????????????? connection.server ?=? SERVER_NAME ; ??
  31. ???????????????????? connection.port ?=? 5222 ; ??
  32. ????????????????????connection.connect("flash"); ??
  33. ????????????????????if(connection.isLoggedIn()){ ??
  34. ????????????????????????chatContent.htmlText?+=?"Welcome?"?+?username.text?+?" < br /> "; ??
  35. ????????????????????} ??
  36. ????????????????}?else?{ ??
  37. ????????????????????connection.disconnect(); ??
  38. ????????????????} ??
  39. ????????????} ??
  40. ???????????? ??
  41. ????????????private?function?onLogin(event):void?{ ??
  42. ???????????????? inputMsg.enabled ?=? true ; ??
  43. ???????????????? sendBtn.enabled ?=? true ; ??
  44. ??
  45. ???????????????? chatRoom ?=? new ?Room(?connection?); ??
  46. ????????????????chatRoom.setRoomJID(connection.getJID()); ??
  47. ???????????????? chatRoom.roomName ?=? CHATROOM ; ??
  48. ???????????????? chatRoom.nickname ?=? connection .username; ??
  49. ???????????????? chatRoom.conferenceServer ?=? "conference." ?+?SERVER_NAME; ??
  50. ???????????????? ??
  51. ????????????????chatRoom.join(); ??
  52. ????????????????chatRoom.addEventListener(RoomEvent.GROUP_MESSAGE,?groupMessage); ??
  53. ????????????} ??
  54. ???????????? ??
  55. ????????????private?function?groupMessage(event):void?{ ??
  56. ????????????????displayUserMessage(getNickName(event.data.from)?,?event.data.body?); ??
  57. ????????????} ??
  58. ??
  59. ????????????private?function?getNickName(jid?:?String)?:?String?{ ??
  60. ????????????????var? name ?=? jid .split("/")[1]; ??
  61. ????????????????if?( name ?==?null)?{ ??
  62. ???????????????????? name ?=? "Message" ; ??
  63. ????????????????} ??
  64. ????????????????return?name; ??
  65. ????????????} ??
  66. ???????????? ??
  67. ????????????private?function?displayUserMessage(user:String,?message:String)?:?void?{ ??
  68. ??
  69. ????????????????var?fontColor?:? String ?=? "#002bd2" ; ??
  70. ????????????????if?( user ?==?chatRoom.nickname)?{ ??
  71. ???????????????????? fontColor ?=? "#8e2800" ; ??
  72. ????????????????} ??
  73. ??
  74. ????????????????chatContent.htmlText?+=?" < font ? color = '"?+?fontColor?+?"' > < b > "?+?user?+?": </ b > ?"?+?message?+?" </ font > < br ? /> "; ??
  75. ????????????} ??
  76. ???????????? ??
  77. ????????????private?function?sendMsg():void?{ ??
  78. ????????????????if?(inputMsg.text?!=?"")?{ ??
  79. ????????????????????chatRoom.sendMessage(inputMsg.text); ??
  80. ???????????????????? inputMsg.text = "" ; ??
  81. ????????????????} ??
  82. ????????????} ??
  83. ??
  84. ????????]] ??
  85. ???? </ mx:Script > ??
  86. ??
  87. ??
  88. ???? < mx:Label ? x = "10" ? y = "10" ? text = "UserName" /> ??
  89. ???? < mx:TextInput ? id = "username" ? x = "80" ? y = "8" ? width = "92" /> ??
  90. ???? < mx:Label ? x = "180" ? y = "10" ? text = "Password" /> ??
  91. ???? < mx:TextInput ? id = "password" ? x = "244" ? y = "8" ? width = "99" ? displayAsPassword = "true" /> ??
  92. ???? < mx:Button ? x = "351" ? y = "8" ? label = "Login" ? click = "doLogin()" /> ??
  93. ???? < mx:TextArea ? id = "chatContent" ? x = "10" ? y = "36" ? width = "397" ? height = "171" /> ??
  94. ???? < mx:TextInput ? id = "inputMsg" ? enabled = "false" ? x = "12" ? y = "215" ? width = "333" ? enter = "sendMsg()" ? /> ??
  95. ???? < mx:Button ? id = "sendBtn" ? enabled = "false" ? x = "353" ? y = "215" ? label = "Send" ? click = "sendMsg()" ? /> ??
  96. </ mx:Application > ??

?

參考

http://www.dgrigg.com/post.cfm/09/05/2006/XIFF-Actionscript-3-for-Flex-2

Flex2基于XMPP的chat程序 (一)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 免费人成激情视频在线看 | 久久国产精品国产精品 | 天天草夜夜草 | 久久精品无码一区二区日韩av | 亚洲天天做夜夜做天天欢 | 色妞www精品视频 | 日本xxx高清免费视频 | 91精品国产色综合久久不 | 国产精品视频全国免费观看 | 精品成人在线观看 | 曰本一区二区三区 | 不卡福利视频 | 亚洲精品二三区伊人久久 | 欧美一级毛片图 | 欧美三级在线观看不卡视频 | 成人免费视频网站 | 国产码欧美日韩高清综合一区 | 看一级特黄a大一片 | 四虎永久免费网站免费观看 | 狼人香蕉香蕉在线视频播放 | 亚洲视频在线一区二区 | 亚洲香蕉在线 | 女人18一级毛片免费观看 | 国产小视频91 | 久青草国产手机在线视频 | 女女同免费播放毛片 | 一区二区三区不卡在线 | 欧美精品在线一区 | 欧美激情久久欧美激情 | 欧美国产精品日韩在线 | 欧美性猛交xxx嘿人猛交 | 狠狠色噜噜狠狠狠狠97影音先锋 | 亚洲精品欧洲一区二区三区 | 国产精品自在线拍国产 | 97se亚洲国产综合自在线观看 | 狠狠干人人插 | 精品一区二区久久 | 在线高清一级欧美精品 | 欧美97| 亚洲国产欧美在线不卡中文 | 国产欧美一区视频在线观看 |