as3中的简单聊天应用程序

时间:2014-09-11 10:09:57

标签: actionscript-3 sockets chat flash-media-server

我想在我的应用程序中添加一个简单的聊天应用程序功能,该功能是在动作脚本3.0中创建的。我做了一些研究,但不明白数据将如何流动,应用程序将被触发。 我也不知道实现聊天应用程序需要什么类型的资源。 在这方面任何好的最新教程对我都非常有帮助。

2 个答案:

答案 0 :(得分:2)

您应该检查SmartFoxServer。我做了类似的应用程序和他们的解决方案(还有一个免费版本)非常好,还有很多例子和非常好的参与支持。

您也可以尝试自己的解决方案,查看flash.net.Socket课程。 我还找到了一个很好的例子here

答案 1 :(得分: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();
}