通过 P2P NetStream 轻松进行双向通信
我一直在使用 Adobe Stratus 服务研究 Flash 10 中的 P2P 支持。我已经成功地能够将数据从一个用户发送到另一个用户,我的问题是我还没有弄清楚如何以某种简单的方式将数据发送回(或者作为对第一次调用的某种响应)。
我目前正在做什么;
首先与Stratus服务建立连接
nc = new NetConnection(); nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatusHandler); nc.connect(APPLICATION_URL + DEVELOPER_KEY);
在“服务器”端我这样做:
sendStream = new NetStream(nc, NetStream.DIRECT_CONNECTIONS); sendStream.addEventListener(NetStatusEvent.NET_STATUS, sendStreamHandler); sendStream.publish("文件");
在“客户端”端:
//remoteFileID.text是用户从服务器手动复制的(即nc.nearID)。 recvStream = new NetStream(nc,remoteFileID.text); recvStream.client = this; recvStream.addEventListener(NetStatusEvent.NET_STATUS,recvStreamHandler); recvStream.play("文件");
然后我在客户端调用远程函数:
<前><代码>... sendStream.send("aRemoteFunction",parameterData); ...现在是我的问题;我想从客户端到服务器执行相同的操作,以通知一切顺利或出现故障。据我了解,我必须设置一个从客户端到服务器的新 NetStream(即在客户端上发布并在服务器上播放)。但要实现此目的,服务器需要知道客户端上的 nc.nearID。
是否可以在不强制用户手动将其从客户端复制到服务器的情况下获取该 ID?或者,是否有更简单的方法让客户端与我丢失的服务器进行对话?
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;
First set up a connection with Stratus service
nc = new NetConnection(); nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatusHandler); nc.connect(APPLICATION_URL + DEVELOPER_KEY);
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");
Then I call a remote function on the client:
... sendStream.send("aRemoteFunction", parameterData); ...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 Twitter 联系 Tom Krcha ( http://twitter.com/tomkrcha/status/12882755421 ) :他告诉我查看 NetStream 的 onPeerConnected(...) 属性,它起作用了。
订阅者 NetStream 上的 farID 解决了我的问题;
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;
为什么不通过
sendStream.send
调用远程方法将nearID发送到服务器?编辑:抱歉,不太明白。我建议,您在服务器上监听客户端连接。 NetGroup.Neighbor.Connect 应该是您所需要的:
当邻居连接到此节点时发送。 info.neighbor:String 属性是邻居的组地址。 info.peerID:String 属性是邻居的对等 ID。
编辑:如果您在客户端连接时收到任何事件,则可以检索 所有客户端,并使用其
farNonce<服务器上的 /code>,它是客户端上连接的流的
nearNonce
。问候
后退2dos
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 thenearNonce
of the connected stream on the client.greetz
back2dos