views:

383

answers:

2

I've been looking into the P2P support in Flash 10, using Adobe Stratus service. I have successfully been able to send data from one user to another, put my problem is that I haven't figured out how to send data back in some easy way (or as some kind of response to the first call).

What I'm currently doing;

  1. First set up a connection with Stratus service

    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatusHandler);
    nc.connect(APPLICATION_URL + DEVELOPER_KEY);
    
  2. On the "server" side I do:

    sendStream = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
    sendStream.addEventListener(NetStatusEvent.NET_STATUS, sendStreamHandler);
    sendStream.publish("file");
    

    And on the "client" side:

    // remoteFileID.text is manually copied by the user from the server (which is nc.nearID).
    recvStream = new NetStream(nc, remoteFileID.text); 
    recvStream.client = this;
    recvStream.addEventListener(NetStatusEvent.NET_STATUS, recvStreamHandler);
    recvStream.play("file");
    
  3. Then I call a remote function on the client:

    ...
    sendStream.send("aRemoteFunction", parameterData);
    ...
    
  4. Now my problem; I want to do the same from the client to the server, to notify that everything went well, or something failed. From what I understand, I will have to setup a new NetStream from the client to the server (i.e publish on the client and play on the server). But to accomplish this, the server need to know the nc.nearID on the client.

    Is it possible to get that ID without forcing the user to manually copy it from the client to server? Or, is there an easier way for the client to talk back to the server that I am missing?

A: 

why don't you send the nearID to the server by invoking a remote method through sendStream.send?

edit: sorry, didn't quite get that. I suggest, you listen for client connections on the server. NetGroup.Neighbor.Connect should be what you need:
Sent when a neighbor connects to this node. The info.neighbor:String property is the group address of the neighbor. The info.peerID:String property is the peer ID of the neighbor.

edit: if you're getting any events when clients connect, then you can retrieve all clients, and use their farNonce on the server, which is the nearNonce of the connected stream on the client.

greetz
back2dos

back2dos
The NetStream sendStream is only available to the server and is set to *publish*, so I am only able to send data from the server to client on this stream (and thus only the nearID of the server, which the client already knows since it used it to connect to the server).
andsve
@andsve: answer updated.
back2dos
I don't get any NetGroup.Neighbor.Connect (should I?), I get NetConnection.Connect.Success (from Stratus), and then a couple of NetStream.Publish.* NetStream.Play.* events, but none of them include a peerID from what I can understand. :(
andsve
@andsve: yet another update :)
back2dos
Thanks again for your answer, but could not get it to return the right ID. The farNonce did return an ID, but not the right one for the client. I did however get a reply from Tom Krcha´s twitter that did work ( http://twitter.com/tomkrcha/status/12882755421 ), will be posting an answer here soon. Thanks again for your help!
andsve
A: 

Contacted Tom Krcha via twitter ( http://twitter.com/tomkrcha/status/12882755421 ): he told me to look into the onPeerConnected(...) property of NetStream and it worked.

The farID on the subscriber NetStream solved my question;

var c:Object = new Object;
c.onPeerConnect = function(subscriber:NetStream):Boolean {
    trace("far id: " + subscriber.farID); // This farID will be the nearID of the connecting "client".

    recvStream = new NetStream(nc, subscriber.farID);
    recvStream.client = this;
    recvStream.addEventListener(NetStatusEvent.NET_STATUS, netRecvStreamHandler);
    recvStream.play("response");

    return true;
};

sendStream = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
sendStream.client = c;
sendStream.addEventListener(NetStatusEvent.NET_STATUS, netSendStreamHandler);
sendStream.publish("file");
andsve