2014-09-11 86 views
-1

我想在操作腳本3.0中創建的應用程序中添加一個簡單的聊天應用程序功能。我做了一些研究,但不明白數據如何流動和應用程序將被觸發。 我也不知道實現聊天應用程序需要什麼類型的資源。 在這方面任何好的最新教程將對我非常有幫助。as3中的簡單聊天應用程序

回答

2

您應該檢查SmartFoxServer。我做了類似的應用程序和他們的解決方案(還有一個免費版本)非常好,還有很多示例和非常好的參與性支持。

您也可以嘗試自己的解決方案,請檢查flash.net.Socket課程。 我還發現一個很好的例子here

+0

這是唯一的方法來實現?我可以使用套接字程序來完成這項任務嗎? – kaif 2014-09-11 11:39:07

+0

有幾種方法可以實現它,這只是一個建議。 SmartFoxServer是一個Java套接字服務器。 – gabriel 2014-09-11 11:45:14

+0

ok gPearl.I有php服務器,所以這就是爲什麼我要實現使用套接字聊天。可以描述我使用套接字實現的數據流。 – kaif 2014-09-11 12:08:52

1
# This code is work as adding user to user list and allow the user's to private chat using #SharedObject. 

Client side code : 

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" height="500" width="700"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
      import flash.events.AsyncErrorEvent; 
      import flash.events.Event; 
      import flash.events.MouseEvent; 
      import flash.events.NetStatusEvent; 
      import flash.events.SyncEvent; 
      import flash.net.NetConnection; 
      import flash.net.SharedObject; 

      import mx.collections.ArrayCollection; 
      import mx.controls.Alert; 
      import mx.events.FlexEvent; 
      import mx.events.ListEvent; 

      public var nc:NetConnection; 
      private var so:SharedObject=null; 
      public var tempUser:String; 
      public var listUsr:String; 
      private var soUser:String="users_so1"; 
      [Bindable] 
      private var a:ArrayCollection; 

      [Bindable] 
      [Embed(source="src/online.png")] 
      private var online:Class; 

      [Bindable] 
      [Embed(source="src/offline.png")] 
      private var offline:Class; 

      private var _alert:Alert; 
      [Embed(source="src/online.png")] 
      private var IconCritical:Class; 

      private var uswerData:ArrayCollection = new ArrayCollection; 

      protected function btnSubmit_clickHandler(event:MouseEvent):void 
      { 
       nc=new NetConnection(); 
       nc.client=this; 
       nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler); 
       nc.connect("rtmp://localhost/test",txtUsername.text); 
       lblCurrentUser.text=txtUsername.text; 
      } 

      protected function netStatusHandler(event:NetStatusEvent):void 
      { 
       if (event.info.code == "NetConnection.Connect.Failed") 
       { 
        trace(""+event.info.code); 
        Alert.show(""+event.info.code); 
       } 

       else if (event.info.code == "NetConnection.Connect.Rejected") 
       { 
        trace(""+event.info.code); 
        Alert.show(""+event.info.code); 
       } 

       else(event.info.code == "NetConnection.Connect.Success") 
       { 
        trace(""+event.info.code); 
        SharedObject.defaultObjectEncoding=flash.net.ObjectEncoding.AMF3; 
        so=SharedObject.getRemote(soUser,nc.uri,false); 
        so.addEventListener(SyncEvent.SYNC,syncEvent); 
        so.connect(nc); 
        so.client=this; 
       } 
      } 

      protected function syncEvent(event:SyncEvent):void{ 
       a=new ArrayCollection(); 
       for (var prop:String in so.data) 
       { 
        var obj:Object = new Object; 
        obj.name = prop; 
        obj.status=so.data[prop]; 
        a.addItem(obj);      
       } 
       lstOnlineUsers.dataProvider=a; 
       lstOnlineUsers.addEventListener(MouseEvent.DOUBLE_CLICK,onDoubleClick); 
      } 

      protected function findIcon(item:Object):String { 
       if(item.status == "hold") { 
        //lstOnlineUsers.setStyle("icon",online); 
        item.icon = online; 
        //item.name=item.name; 
        return item.name; 
       } 
       else { 
        //lstOnlineUsers.setStyle("icon",offline); 
        item.icon = offline; 
        //item.name=item.name; 
        return item.name; 
       } 
      } 

      protected function onDoubleClick(e:MouseEvent):void{ 

       listUsr=""+lstOnlineUsers.selectedItem.name; 
       lblReceiverName.text=listUsr; 
      } 

      private function eSendClick(event:Event):void 
      { 
       sendMessage(); 
      } 

      public function sendMessage():void{ 

       var obj:Object=new Object(); 
       obj.sender=lblCurrentUser.text; 
       obj.msg=txtMessage.text; 
       obj.receiver=lblReceiverName.text; 
       so.send("showMessage",obj); 
      } 

      public function showMessage(msg:Object):void{ 
       if(msg.sender==lblCurrentUser.text){ 
       if(msg.sender==msg.receiver || lblReceiverName.text=="UserNotSelected") 
       { 
        Alert.show("Select A Receiver !"); 
        return; 
       } 
      } 
       if((msg.receiver==lblCurrentUser.text) || (msg.sender==lblCurrentUser.text)){ 
        txtDisplay.text += msg.sender+" : "+msg.msg+ "\n"; 
        txtMessage.text = ""; 

        if(msg.receiver==lblCurrentUser.text) 
         showAlert(msg.sender); 
       } 
      } 

      private function showAlert(sender:String):void{ 

       var myMessage:String = "!!!"; 
       var myTitle:String = "Messagesg Received From "+sender; 
       _alert = Alert.show(myMessage, myTitle, Alert.OK, null, null, IconCritical); 
      } 

      private function onErrorMsg(event:AsyncErrorEvent):void{ 
       trace("========================= onErrorMsg"+"\r"); 
      } 

     ]]> 
    </fx:Script> 


    <!--design--> 
    <s:Panel x="97" y="78" width="239" height="337" 
      title="Welcome User" id="panelUsers"> 
     <s:Label id="lblCurrentUser" x="103" y="-22" width="89" height="16" 
       text=""/> 
     <s:Label id="lblUser" x="30" y="47" width="78" height="30" text="Username" 
       verticalAlign="middle"/> 
     <s:TextInput id="txtUsername" width="86" height="29" 
        horizontalCenter="-44" verticalCenter="-68" /> 
     <!--<s:TextInput id="txtPassword" width="88" height="21" horizontalCenter="-50" verticalCenter="-8" />--> 
     <s:Button id="btnSubmit" x="126" y="76" label="Submit" click="btnSubmit_clickHandler(event)"/> 
     <mx:List id="lstOnlineUsers" x="26" y="117" width="138" height="174" 
       labelFunction="findIcon" doubleClickEnabled="true" /> 
    </s:Panel> 
    <s:Panel x="381" y="92" width="250" height="308" 
      id="panelChat" title="To: "> 
     <s:TextArea x="20" y="9" width="207" height="196" id="txtDisplay" editable="false"/> 
     <s:TextInput x="20" y="215" width="140" id="txtMessage"/> 
     <s:Button x="168" y="216" label="SEND" id="btnSend" click="sendMessage()"/> 
     <s:Label x="31" y="-22" id="lblReceiverName" text="UserNotSelected"/> 

    </s:Panel> 
    <!--design--> 
</s:WindowedApplication> 

------------------------ 
server side code : 
------------------------ 
application.onAppStart = function() 
{ 
    trace("Begin...."); 

    // Get the server shared object 'users_so' 
    application.users_so = SharedObject.get("users_so1", false); 
} 

application.onConnect = function(client, name) 
{ 
    //trace("clients::"+application.clients.length) 
    var temp = application.users_so.getProperty(name); 
    if (temp) 
    { 
     application.rejectConnection(client); 
     return false; 
    } 
    client.name = name; 
    application.users_so.setProperty(name,"hold"); 
    application.acceptConnection(client); 
} 


application.onDisconnect = function(client) 
{ 
    trace("disconnect: " + client.name); 
    application.users_so.setProperty(client.name, null); 
    //users_so1.clear(); 
}