views:

219

answers:

0

When my application connecting with the netconnection and with shared object application connecting fine but while connecting shared object they are not firing the server event like application.onAppStart = function() {} , application.onConnect = function(newClient, userName){}

follwing are my code /// main.as Client.prototype.getServerTime = function(msg){ var now = new Date(); return now.toString(); }

application.onAppStart = function() { trace("Begin sharing text");

// Get the server shared object 'users_so' application.users_so = SharedObject.get("ChatUsers");

// Initialize the history of the text share application.history = "";

// Initialize the unique user ID application.nextId = 0; }

application.onConnect = function(newClient, userName) { // Make this new client's name the user's name newClient.name = userName;

// Create a unique ID for this user while incrementing the // application.nextID. newClient.id = "u" + application.nextId++;

// Update the 'users_so' shared object with the user's name application.users_so.setProperty(newClient.name, userName);

// Accept the client's connection application.acceptConnection(newClient);

// Call the client function 'setHistory,' and pass // the initial history newClient.call("setHistory", null, application.history);

// The client will call this function to get the server // to accept the message, add the user's name to it, and // send it back out to all connected clients. newClient.msgFromClient = function(msg) { msg = userName + ": " + msg + "\n"; application.history += msg; application.users_so.send("msgFromSrvr", msg); } }

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

//main.mxml

//flexFCS.mxml

; import flash.events.; import flash.utils.; import mx.controls.; import mx.core.UIComponent; import chat_test.BWClient;

         // Current release of FMS only understands AMF0 so tell Flex to 
// use AMF0 for all NetConnection, NetStream, and SharedObject objects.
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
//NetStream.defaultObjectEncoding     = flash.net.ObjectEncoding.AMF0;
SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;

// echoResponder is used when nc.call("echo", echoResponder ...) is called.
private var echoResponder:Responder = new Responder(echoResult, echoStatus);

// SharedObject and NetConnection vars
private var nc:NetConnection;
public var ro:SharedObject;

         private function init():void 
         { 
 writeln("Initializing application... in player: " + flash.system.Capabilities.version + "\n");
 // create new connection to FMS and add listeners
          nc = new NetConnection();             
 nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
 nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);


         } 

         /** 
       * connect is called whenever the connectButton is pressed
       * and decides what to do based on the current label of the button.
       * NOTE: the rtmp address is in this function. Change it if you need to.
       */
         private function connect():void
         {
       if(userName.text == ""){
        Alert.show("Please enter a user name.");
        return;
       }
       switch(connectButton.label){
        case "Connect":
         connectButton.label = "Wait";
         connectButton.enabled = false;
         //nc.client = this;
         nc.client = new BWClient();
         nc.connect("rtmp://localhost/TextChat/", userName.text);

         //nc.call("onBWDone",null);
         //nc.connect("rtmp://kurosawa.wpcareyonline.com/chat_test", userName.text);
         //nc.connect("rtmp://localhost/chat_test", userName.text);
         //nc.client = this;
        break;
        case "Disconnect":
         connectButton.label = "Connect";
         connectButton.enabled = true;
         nc.close();
        break;
       }
         }
         /* public function onBWDone():void { 


         }  */
           public function onBWCheck (res:Object):void
        {

        }  

         private function netSecurityError(event:SecurityErrorEvent):void {
          writeln("netSecurityError: " + event);
      }

         /** 
 * This method could be named anything - even onStatus. I've named it
 * netStatus as in Dave Simmons example. In the docs they use the
 * name netStatusHandler. Instead of receiving an information object
 * it is passed an event that contains the information object.
 */
private function netStatus(event:NetStatusEvent):void 
{
 // Write out information about connection events:
          writeln("netStatus: " + event);
          var info:Object = event.info;
          for(var p:String in info) {
              writeln(p + " : " + info[p]);
          }
          writeln("");

 switch (info.code) 
 {
  case "NetConnection.Connect.Success" :
   connectButton.label = "Disconnect";
            connectButton.enabled = true;
            sendButton.enabled = true; 

            writeln("Connecting non-persistent Remote SharedObject...\n");
   //ro = SharedObject.getRemote("ChatUsers", nc.uri);
   ro = SharedObject.getRemote("ChatUsers", nc.uri,false);
   //ro = SharedObject.getLocal("ChatUsers");
   if(ro){

    ro.addEventListener(SyncEvent.SYNC, OnSync);
    //ro.addEventListener(Event.ADDED,onBWCheck);
    ro.client = this;
    ro.connect(nc);
    nc.call ("checkBandwidth",null);
    //nc.call ("onBWCheck",null);
     // refers to the scope of application and public funtions
   }
    getServerTime(); // get local time
     break;
  case "NetConnection.Connect.Closed" :
   connectButton.label = "Connect";
            connectButton.enabled = true;
            sendButton.enabled = false;  
     break;
  case "NetConnection.Connect.Failed" :
     break;
  case "NetConnection.Connect.Rejected" :
     break;
  default :
     //statements
     break;
 }
}

private function OnSync(event:SyncEvent):void 
{
 // Show the ChangeList:
 var info:Object;
 var currentIndex:Number;
 var currentNode:Object;
 var changeList:Array = event.changeList;
 var temp:Array = new Array();

 writeln("---- Shared Object Data -----");

 for(var p:String in ro.data){ 
  writeln("OnSync> RO: " + p + ": " + ro.data[p]);
  temp.push(ro.data[p]);
 }
 this.usersList.dataProvider = temp; //update list of users

 for (var i:Number=0; i < changeList.length; i++) 
 {
  info =  changeList[i];
  writeln("---- test  ----->"+info.toString());
  for (var k:String in info){
   writeln("OnSync> changeList[" + i + "]." + k + ": " + info[k]);
  }
 }
}

/** echoResult is called when the echoResponder gets a result from the nc.call("echoMessage"..) */
      private function echoResult(msg:String):void{
       writeln("echoResult: " + msg + "\n");
       this.serverTime.text = msg;
      }

      /** echoResult is called when the echoResponder gets a error after a nc.call("echoMessage"..) */
      private function echoStatus(event:Event):void{
       writeln("echoStatus: " + event);
      }

/** sendMessage is called when the sendButton is pressed to test ns.send */
private function sendMessage():void{
 // call our remote function and send the message to all connected clients
 nc.call("msgFromClient", null, sendMessageInput.text);
 sendMessageInput.text = "";
}

/** get server tme  */
 private function getServerTime():void{
 nc.call("getServerTime", echoResponder,  sendMessageInput.text);
} 

/** showMessage is the function called by the inStream. See the netStatus function */
public function showMessage(msg:String):void{
 writeln("showMessage: " + msg + "\n");
}

public function setUserID(msg:Number):void{
 writeln("showMessage: " + msg + "\n");
}

public function setHistory(msg:String):void{
 writeln("showHistory: " + msg + "\n");
}

public function msgFromSrvr(msg:String):void{
 writeMessage(msg);
} 

/** 
 * writeln writes text into the traceArea TextArea instead of using trace. 
 * Note to get scrolling to the bottom of the TextArea to work validateNow()
 * must be called before scrolling.
 */  
public function writeln(msg:String):void{
 traceArea.text += msg + "\n";
 traceArea.validateNow();
 traceArea.verticalScrollPosition = traceArea.maxVerticalScrollPosition;
}

/** 
 * writeMessage writes text into the main chat text area
 */  
public function writeMessage(msg:String):void{
 messageArea.text += msg + "\n";
 messageArea.validateNow();
 messageArea.verticalScrollPosition = messageArea.maxVerticalScrollPosition;
}

  ]]> 
 </mx:Script> 

Please let me know where the propblem exist in my code...

Thanks