2012-08-08 65 views
0

我已经通过Google,FMS Guru和大量Adobe开发人员教程进行了搜索。我有点困惑,因为如何从客户端发送变量作为sharedobject或客户端对象的参数,所以我可以从main.asc文件中抓取服务器端的变量&。如何使用Flex,AS3和Flash Builder将变量从客户端swf发送到serverside(main.asc)

例如,如何使用创建的SWF中的AS3将用户名,用户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未定义”,所以我必须做一些错误的。

+0

我修改我的回答与另一个问题。 – BadFeelingAboutThis 2012-08-09 00:17:07

回答

1

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

if(userType="viewer") 

应该

if(userType == "viewer") 

此外,您的跟踪语句可能是因为您的应用程序调用onStart()之前运行,所以确实你变量在这一点上不确定的。

在您的客户端代码,你需要传递的参数在连接字符串后的净连接上connect()函数,所以在情况下,它会是这样:

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

我修复了“==”问题。我知道我可以使用application.clients.length;在main.asc中获取所有连接的客户端,但可能要过滤另一个变量。如何使用现有代码从mxml中的AS3代码发送所需的变量? – Patriotec 2012-08-09 01:03:32

+0

更新了答案给你看。 – BadFeelingAboutThis 2012-08-09 02:03:56

+0

谢谢。我有一天晚上尝试了这个,但它没有工作,因为我没有在这个例子中正确加载XML:http://stackoverflow.com/questions/11855918/as3-concatenate-rtmp-connection-string-from-两个XML-间函数 – Patriotec 2012-08-09 12:34:01

相关问题