1 Reply Latest reply on Apr 29, 2010 11:52 PM by palkan_tula

    Flash CS4 onPeerConnect does not fire

    mrBaloometer

      Hi
      I tryed to do Flex example for simple chat (p2p) with Stratus at Flash CS4 IDE
      (adapted it of course) but onPeerConnect does not fire.
      Can you take a look at my code ?
      What's wrong with it ?

       

      package {
          import flash.events.*;
          import flash.net.*;
          import flash.display.*;
          import flash.media.*;

       

          public class stratusTest2 extends Sprite {
              private const SERVER_ADDRESS:String="rtmfp://stratus.adobe.com/";
              private const DEVELOPER_KEY:String="a9fb14e5b040800e8327ab51-37f33907d0c1/";

       

              private var nc:NetConnection;
              private var myPeerID:String;
              private var farPeerID:String;
              // streams
              private var sendStream:NetStream;
              private var recvStream:NetStream;
             
              private var VideoDisplay:MyVideoDisplay;
              private var FAR_PeerVideoDisplay:farPeerVideoDisplay;
              private var VidDisplay:Sprite;

       

              public function stratusTest2(){
                  addEventListener("addedToStage",initThis);
              }
              private function initThis(e:Event){
                  VidDisplay=new Sprite();
                  addChild(VidDisplay);
                  connectToStratusBTN.addEventListener("mouseDown",initConnection);
                  initSendStreamBTN.addEventListener("mouseDown",initSendStream);
                  initReceiveStreamBTN.addEventListener("mouseDown",initRecvStream);
                  sendDataBTN.addEventListener("mouseDown",sendDataBTNPressed);
              }
             
              private function initConnection(e:MouseEvent){
                  if (MY_peerIDText.text) {
                      farPeerID=MY_peerIDText.text;
                  }

       

                  nc=new NetConnection();
                  nc.addEventListener(NetStatusEvent.NET_STATUS,ncStatus);
                  nc.connect(SERVER_ADDRESS+DEVELOPER_KEY);
              }

       

              private function ncStatus(event:NetStatusEvent):void {
                  INF.appendText("ncStatus: "+event.info.code+"\n");
                  trace(event.info.code);
                  myPeerID=nc.nearID;
                  MY_peerIDText.text=myPeerID;
              }

       

              private function initSendStream(e:MouseEvent){
                  trace("initSendStream");
                  INF.appendText("initSendStream\n");
                  sendStream=new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
                  sendStream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
                  sendStream.publish("__media");
                 
                  var sendStreamClient:Object=new Object();
                  sendStream.client=sendStreamClient;
                 
                  sendStreamClient.onPeerConnect=function(callerns:NetStream):Boolean{
                      farPeerID = callerns.farID;
                      trace("onPeerConnect "+farPeerID);
                      INF.appendText("onPeerConnect "+farPeerID+"\n");
                      return true;
                  }

       

                  //VideoDisplay=new MyVideoDisplay(VidDisplay);
                  //var camera:Camera=Camera.getCamera();
                  //VideoDisplay.attachCamera(camera);
                  //sendStream.attachCamera(camera);
              }

       

              private function initRecvStream(e:MouseEvent){
                  recvStream=new NetStream(nc,farPeerID);
                  INF.appendText("start receiving from "+farPeerID+"\n");
                  recvStream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
                  recvStream.play("__media");
                  recvStream.client=this;
                  //FAR_PeerVideoDisplay=new farPeerVideoDisplay(VidDisplay);
                  //FAR_PeerVideoDisplay.attachStream(recvStream);
              }

       

              public function receiveSomeData(str:String){
                  txtReceiveData.text=str;
              }

       

              private function sendDataBTNPressed(e:MouseEvent){
                  sendStream.send("receiveSomeData",txtSendData.text);
              }

       

              private function netStatusHandler(event:NetStatusEvent):void {
                  trace("netStatusHandler: "+event.info.code);
                  INF.appendText("netStatusHandler: "+event.info.code+"\n");
                  trace(event.info.code);
              }
          }
      }

       

      i have 3 input texts: MY_peerIDText, txtSendData, txtReceiveData
      When i press connectToStratusBTN i get ID after than i open another
      browser window with the same swf and paste this ID into MY_peerIDText

       

      After connection i press initSendStreamBTN in first swf (it is 1st browser page)
      and i press initReceiveStreamBTN in 2nd swf

       

      After that iwrite down some text in txtSendData TextField (in 1st browser page) and press sendDataBTN

       

      Nothing happens

        • 1. Re: Flash CS4 onPeerConnect does not fire
          palkan_tula Level 1

          this function doesn't work correctly:

           

          private function sendDataBTNPressed(e:MouseEvent){


                      sendStream.send("receiveSomeData",txtSendData.text);

                  }

           

          because you send not String, but link to the txtSendData.text, which is empty in another peer.

           

          So, solution is following (not the one, but for example):

           

          private function sendDataBTNPressed(e:MouseEvent){
                   var sendString:String = "";

                  sendString+=txtSendData.text;

                  sendStream.send("receiveSomeData",sendString);
                  }