0 Replies Latest reply on Dec 23, 2011 2:36 AM by Marauder64r

    Problem with .onPeerConnect

    Marauder64r

      Hi! I have a problem with .onpeerconnect, it's fired only one time. For example, I connect first user, then second, onpeerconnect fired, and I have two users connected, then I connect third user, and onpeerconnect fired only for second user. If I connect fourth user onpeerconnect fired for third, also another problem: when somebody disconnected I have not one but two fired events "NetStream.Connect.Closed". Why does it's all heappen?

       

      Here is my source:

       

      import flash.events.NetStatusEvent;

      import flash.media.Camera;

      import flash.media.Microphone;

      import flash.net.NetConnection;

      import flash.net.NetStream;

       

       

      import mx.collections.ArrayCollection;

      import mx.controls.Alert;

       

       

      public var c:Camera;

      public var outgoingstream:NetStream;

      public var incomingstream:NetStream;

      [Bindable]

      public var myPeerID:String;

       

       

      public var ip:String;

       

       

      [Bindable]

      private var countuser:int;

       

       

      private const CIRRUS_ADDR:String = "rtmfp://p2p.rtmfp.net/";

      private const DEVELOPER_KEY:String = "6dd5260c9c1f5d29328241af-3339d1bd7ff5";

       

       

      public var nc:NetConnection;

       

       

      //public var farPeerID:String;

       

       

      public function cirrusconnect():void {

                nc = new NetConnection;

                nc.maxPeerConnections = 6;

                nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);

                nc.connect(CIRRUS_ADDR+DEVELOPER_KEY);

      }

       

       

      private function netHandler(event: NetStatusEvent):void{

                switch(event.info.code){

                          case "NetConnection.Connect.Success":

                                    myPeerID = nc.nearID

                                    wID.send();

                                    sendstream();

                                    break;

                          case "NetConnection.Connect.Failed":

                                    Alert.show('Connection error');

                                    break;

                          case "NetStream.Connect.Success":

                                    //Alert.show('шлем поток');

                                    break;

                          case "NetStream.Publish.Start":

                                    break;

                          case "NetStream.Connect.Closed":

                          var obj:Object = findObjectByAttribute(users, "stream", event.info.stream);

                                    if(obj == null){

                                    }else{

                                              closestream(event.info.stream);

                                    }

                                    break;

                }

      }

       

       

      private function sendstream():void{ // только для сервера

                outgoingstream = new NetStream (nc, NetStream.DIRECT_CONNECTIONS);

                var camera:Camera = Camera.getCamera();

                camera.setMode(640,480,40);

                camera.setQuality(0,60);

                var mic:Microphone = Microphone.getMicrophone();

                outgoingstream.attachAudio(mic);

                outgoingstream.attachCamera(camera);

       

                outgoingstream.publish('media');

       

                var client: Object = new Object();

                client.onPeerConnect =

                          function(callerNS:NetStream):Boolean {

                                    var sendobj:Object = new Object;

                                    sendobj.name = username;

                                    sendobj.rating = rating;

                                    sendobj.id = myPeerID;

                                    callerNS.send('hello', sendobj);

                                    connectcheck(callerNS.farID);

                                    return true;

                          };

                outgoingstream.client = client;

       

      }

       

       

      private function connectcheck(id:String):void{

                var i:int = usersid.getItemIndex(id);

                if (i == -1){

                          usersid.addItem(id);

                          connectnew(id);

                }

      }

       

       

       

       

      private function connectnew(farPeerID:String):void{

                countuser++

                incomingstream = new NetStream(nc, farPeerID);

                incomingstream.play("media");

                var o:Object = new Object();

                o.hello = function(obj:Object):void {

                          if(obj.id == incomingstream.farID){

                                    users.addItem(

                                              {name:obj.name, rating:obj.rating, stream:incomingstream, id:incomingstream.farID})

                                    users.refresh()

                                    if(seating == false){

                                              playerinit();

                                              seating = true; //Убрать ситинг при уходе из комнаты

                                    }

                          }

                };

                incomingstream.client = o;

      }

       

       

      private function closestream(exitstream:NetStream):void{

                countuser--

                exitstream.close();

                usersid.removeItemAt(usersid.getItemIndex(exitstream.farID));

                var str:Object = findObjectByAttribute(users, "stream", exitstream);

                users.removeItemAt(users.getItemIndex(str));

                usersid.refresh();

                users.refresh();

      }

       

       

      private function findObjectByAttribute(arrayCollection:ArrayCollection,

                                                                                                   attributeName:String, value:NetStream):Object{

                for each (var object:Object in arrayCollection) {

                          if(object[attributeName]==value){

                                    return object;

                          }

                }

                return null;

      }

       

      /////////

      Thanx!