actionscript-3flashp2pnetconnection

NetStream how to set maximal number of peers that can be connected to the publisher?


Let say I have already 4 peers connected to the publisher and I want to drop new connections (for game development stuff, like players).

I tried this:

stream.client.onPeerConnect = function( s:NetStream ):Boolean
{
 return stream.peerStreams.length + 1 <= 4;
}

It doesn't transmit data but tells 'NetStream.Connect.Success' to the subscriber (expecting 'NetStream.Connect.Failed' or 'NetStream.Connect.Closed').

Also tried this: (at NetStatusEvent.NET_STATUS listener of NetConnection class)

switch(evt.info.code)
{
 case 'NetStream.Connect,Success':
  if(stream.peerStreams.length > 4)
  {
   evt.info.stream.close(); break;
  }
  // setup a new stream for receive data from evt.info.stream.farID (or Peer ID) //
  ................

It closes that connection but before that happens publisher can send some data (audio, video, user) which is I don't like.

Any ideas?


Solution

  • The NetConnection class has a property called maxPeerConnections which you should be able to set to 4 (default is 8). This will make it so the 5th incoming connection doesn't even receive a NetStream.Connect.Success which is what I believe you want

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/NetConnection.html#maxPeerConnections