【问题标题】:How to send variables to serverside (main.asc) from the client swf using Flex, AS3 and Flash Builder如何使用 Flex、AS3 和 Flash Builder 从客户端 swf 向服务器端 (main.asc) 发送变量
【发布时间】:2012-08-08 23:51:36
【问题描述】:

我搜索了 Google、FMS Guru 和大量 Adob​​e 开发人员教程。我有点困惑如何从客户端将变量作为参数发送到共享对象或客户端对象中,以便我可以从 main.asc 文件中获取和处理服务器端的变量。

例如,如何使用 AS3 从创建的 SWF 将用户名、用户 ID、性别、用户类型和生日变量发送到 main.asc 文件?

来自 chat.mxml

private var xmlstring:String = "http://www.blah.com/xml.xml";


            private var userType:String;
            private var userCountText:String;

            protected function getXML():void {
                XML.ignoreWhitespace = true;
                var myLoader:URLLoader=new URLLoader();
                myLoader.load(new URLRequest(ownerstring));
                myLoader.addEventListener(Event.COMPLETE, processXML);
            }

            protected function processXML(e:Event):void {
                var myXML:XML = XML(e.target.data)
                for (var i:int = 0; i<myXML.*.length(); i++){
                    xinstance = myXML.owner[0];
                    xuserid = myXML.owner[1];
                    xusername = myXML.owner[2];
                    xphoto = myXML.owner[3];
                    xroomowner = myXML.owner[4];
                }
                //xinstance = myXML.broadcastowner.owner.(@title == "instance");
                //xuserid = myXML.broadcastowner.owner.(@title == "userid");
                //xusername = myXML.broadcastowner.owner.(@title == "username");
                //xphoto = myXML.broadcastowner.owner.(@title == "photo");
                //xroomowner = myXML.broadcastowner.owner.(@title == "roomowner");

                go();
            }

            private function initConnection(event:FlexEvent):void{
                getXML();
            }

            private function go():void {
                var fmsstring:String = "rtmp://blah.com/appname/" + xinstance;

                nc = new NetConnection();
                nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
                nc.connect(fmsstring);
                nc.client = this;
            }           

            protected function onNetStatus(event:NetStatusEvent):void{
                trace(event.info.code);

                switch(event.info.code){

                    case "NetConnection.Connect.Success":
                        publishCamera(); 
                        displayPublishingVideo();
                        chat_broadcastLive();

                        so = SharedObject.getRemote("message", nc.uri, false);

                        so.username = xusername;
                        so.userid = xuserid;
                        so.userType = xroomowner;

                        so.addEventListener(SyncEvent.SYNC, soOnSync);
                        so.client = this;
                        so.connect(nc);

                        //so.setProperty("userinfo",{username:xusername, userid:xuserid, userType:xroomowner});

                        sendBtn.addEventListener(MouseEvent.CLICK, onClickSendBtn);
                        break;

                    case "NetConnection.Connect.Closed" :
                        nc.call("chat.sendMessage", myResponder, xusername + " left the room");
                        break;

                }
            }

Main.asc

application.onAppstart = function(){
this.totalUserCount = 0; 
}

application.onConnect = function(client, username, userid, gender, userType, birthday )
{

//userType = so.data.userinfo["userType"];

client.username = username;
client.userid = userid;
client.gender = gender;
client.userType = userType;
client.birthdaye = birthday;

if(userType="viewer"){
this.totalUserCount++;
}

client.chat = chat;

application.acceptConnection(client);

}

application.onDisconnect = function(client){
if(userType="viewer"){
this.totalUserCount--;
}
}

trace("usercount is:" + this.totalUserCount);

使用上面的 main.asc 代码我得到“usercount is undefined”,所以我一定是做错了什么。

【问题讨论】:

  • 我用另一个问题修改了我的回答。

标签: actionscript-3 flash apache-flex flash-builder flash-media-server


【解决方案1】:

至少您的一个问题是您将值“viewer”分配给您的 userType var 而不是评估它。例如

if(userType="viewer")

应该是

if(userType == "viewer")

此外,您的跟踪语句很可能在您应用 onStart() 之前运行,因此此时您的变量确实是未定义的。

在您的客户端代码中,您需要在连接字符串之后的网络连接上的 connect() 函数中传递参数,所以如果它会是这样的:

nc.connect(fmsstring, username, userid, gender, userType, birthday);

【讨论】:

  • 我修复了“==”问题。我知道我可以使用 application.clients.length;在 main.asc 中获取所有连接的客户端,但可能希望通过另一个变量进行过滤。如何使用我拥有的现有代码从 mxml 中的 AS3 代码发送所需的变量?
  • 谢谢。前几天晚上我试过了,但没有成功,因为在这个例子中我没有正确加载 XML:stackoverflow.com/questions/11855918/…
猜你喜欢
  • 2017-01-12
  • 2022-07-15
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-06-24
  • 2019-04-23
  • 2020-08-01
  • 1970-01-01
相关资源
最近更新 更多