1 Reply Latest reply on Jun 24, 2011 11:33 PM by Michael Thornburgh

    Trouble connecting instantly to peer who connects, and also using multiple receive streams

    BryBam Level 1

      I have a situation where users connect to my application, and they can choose to connect to others for private chat.

       

      What i'm doing is upon the application starting, they connect to the netConnection, then I get their nearID sav it with their userid on a mysql server, then I run setupSendStream() (using directconnections) so its sendstream is online and ready to receive connections. I also set the max peer streams to like 50 because I dont see any reason why more than 50 private chats could be going on at once.

       

      Then when a user presses someones username and sends a "request" I'm calling setupReceiveStream(thefarID) on that button press and then it should connect to the sendStream that the far side was broadcasting...

       

      the side that "gets" the request then sees a netstream.play.start event (rather than a connect event which I found weird, i figured it would tell me it connected, then it would say it starting playing the stream but whatever)

       

      on the netstream.play.start event the sender sees doesnt really help because, event.info.target.farID is blank...so I cant have it connect to the connector, I might be able to have it connect to the last person in the peerStreams array, but I'd rather get the peerID from the event so im sure its the right one

       

       

      anyway, My original plan was to listen on the receiving side for a connect event...so on the side that was broadcasting the sendStream I setup something like this just to listen to events for the sendStream. I have something just like this for the receiveStream and when i made the sendstream and the receivestream I added them to each of their own switches for event listenerers.

       

      protected function sendStreamHandler(event:NetStatusEvent):void

      {

      switch (event.info.code)

      {

      case "NetStream.Connect.Success" :

      trace(event.info.code+"sendStream"+"PeerStreamsLength="+sendStream.peerStreams.length);

      break;

       

      case "NetStream.Play.Start" :

      trace(event.info.code+"sendStream"+"PeerStreamsLength="+sendStream.peerStreams.length);

      break;

      }

      }

       

       

      BUT what im noticing is when the other send does their setupReceiveStream, the far side that is the one that has its sendstream being connected to see the NetStream.Connect.Success BUT its happening on the event listenerer for the receivestream which is really odd because the sendstream was cennected to by the other sides receivestream.... But then I see the NetStream.Play.Start trigger right afterwards for the sendStream which seems right. im just not sure why the send stream isnt the one getting the connect event.

       

      The reason its important that i listen for the connect is because I wanted to figure out whoconnected to my sendstream, and then setup that sides receivestream with their netConID and then do a send telling them I got their connection and im ready for them to send their "private chat request" im doing this so I can include some vars about the user so they can see who wants to chat with them, so even though the connection is being established its still not technically approved by the user. and then once the receiving side gets the request and hits approve or deny it then would present something like a private chat screen that would let them talk/videochat/ect or if deny was hit then I would just close the connection.

       

      I was planning to allow users to send messages to specific peers connected to their sendstream using something like this:

       

       

      var THEStream:NetStream = null;

       

      for each (var stream:NetStream in sendStream.peerStreams)

      {

           if (stream["farID"] == farID)

           {

      THEStream = stream;

      break;

           }

      }

       

      THEStream.send("myfunction",someVar);

       

       

       

      I was also planing on putting something like the above code into the netStream.connect event to listen so I could auto send a "Igotyourrequest" function over to the sending side, but it seems like the peerStreams array doesn't always update before the connect event and it sometimes gives me null errors

       

       

      If you made it this for, thanks! Now, i'm not sure how im going to handle setting up multiple receivestreams, the sendstream is easy, it will allow multiple people to connect, but If i understand this correctly ill have to make multiple new receiveStream objects each time someone wants to send a private chat which could get sort of complex.

       

      the whole reason im using direct connections is because the messages are guarenteed.

       

      I was thinking it might just be easier to have users auto setup a group with something like their userID and then when people send requests, then they just connect to the group, and then they can POST information to each other and I can handle all the confirming ect myself and handle if they can do anything beyond just send private chat requests...

       

      but i think if i understand correctly posts aren't guarenteed, and what if i user sends private message requests to like 30 people at once, that would be they are in 30 groups waiting for a respons o a timeout, where i could have them leave the group.

       

       

       

       

       

      Basically thanks for reading this, i'm sort of stuck on this project, and really interested in seeing how I can make it work with p2p but its becomming a headache and I'm not sure how I can get this to work the way I need to. Any ideas how what direction I should be going with it, or if someone could maybe suggest how I could do this better anything would be great.

       

      Also, just remember the main reason for the directconnections idea, how i wanted to have both sides sent up sending/receiving streams to each other upon connection is because i wanted to be able to handle the confirming/denying myself, and send user information vars over in the cofirmation dialog. But im not sure if it will let me setup multiple receivestreams in the long run. because it would be idea to have people send multiple chat reuests, while receiving multiple chat requests, ect.

       

      Thanks!

       

       

      EDIT:

       

      Sorry if it all seems like a jumpled mess, I'll try to narrow this down to the specific issue thats bothering me.

       

      ok, so peerA and peerB both start up and setup their sendStreams, peerB sends a "chat request" to peerA and im doing this by calling setupReceiveStream

      which is real simple,

       

      public function setupReceiveStream(theirID:String):void

      {

      receiveStream = new NetStream(netConnection, theirID);

      receiveStream.addEventListener(NetStatusEvent.NET_STATUS, receiveStreamHandler);

      receiveStream.client = this;

      receiveStream.play("mychannerl");

      trace("SETUP RECEIVE STREAM");

       

      }

       

      the ONLY event that peerA sees is "NetStream.play.Start"...

       

       

      how come it doesnt get a connect event? I want to have peerA auto connect to peerB once peerB strats playing peerA's stream... but you cant get the peers netConnection ID from the play.start event for some reason it just says farID is blank when you look at the vars with a breakpoint...

       

      I thought about maybe setting up a onPeerConnect listener object on the send stream that just connects to the subscribers PeerID but it doesn't seem to just do it Once it keeps setting up the connection over and over and over

       

       

      EDIT AGAIN:

      This is really weird...I've noticed the NetStream.Connect.Success event ONLY dispatches on the swtich that has the netConnection event listenerer...whats with that? I thought that would be a netstream event not a netConnection event...

        • 1. Re: Trouble connecting instantly to peer who connects, and also using multiple receive streams
          Michael Thornburgh Adobe Employee

          NetStream.Connect.Success is sent to the NetConnection event listener to indicate a peer has connected at all, and before that peer is bound to any publishing NetStream (and in fact you might not have a publishing NetStream yet with the stream name that peer issued a "play" for).  this event's info object has a "stream" property that is the per-peer NetStream from which you can get the farID and the nonces.

           

          if you have a DIRECT_CONNECTIONS publishing NetStream with the stream name for which the subscriber issued a "play", that NetStream's client object's onPeerConnect() function will be called with the peer NetStream as its parameter.  to allow the subscribe/play, you must return true from this function. if you don't return true, the subscribe/play will not be allowed.  NetStream.Play.Start should be sent to the master publishing NetStream after you return true from the onPeerConnect function.

           

          you can attach an event listener to the per-peer NetStream (good places to do that might be in the NetStream.Connect.Success event handler or the onPeerConnect function).  i haven't tried this, but i wouldn't be surprised if the NetStream.Play.Start is sent to the per-peer NetStream's event listener once it's attached to a publisher, and that event bubbles up to the master publishing NetStream if the per-peer NetStream doesn't have an event listener set.

           

          the VideoPhoneLabs example used to use some extra NetStreams for signaling, to figure out a "call" was being made, and then set up media NetStreams for sending and receiving video/audio.  the new version of VideoPhoneLabs (i believe) changed the signaling model to use Cirrus's short message relay service (NetConnection.call("relay", null, destPeerID, ...args...) -> NetConnection.client.onRelay(senderPeerID, ...args...))