为了使用RTMFP,我在ActionScript中编写了一些基本函数:
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.ui.Keyboard;
private var serverAddress:String = "rtmfp://cc.rtmfp.net/";
private var serverKey:String = "xxxxx";
private var netConnection:NetConnection;
private var outgoingStream:NetStream;
private var incomingStream:NetStream;
private function initConnection():void {
netConnection = new NetConnection();
netConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);
netConnection.connect(serverAddress + serverKey);
}
private function netConnectionHandler(event:NetStatusEvent):void {
receivedMessages.text += "NC Status: " + event.info.code + "n";
//Some status handling will be here, for now, just print the result out.
switch (event.info.code) {
case 'NetConnection.Connect.Success':
receivedMessages.text += "My ID: " + netConnection.nearID + "n";
break;
}
}
private function sendInit():void {
outgoingStream = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS);
outgoingStream.addEventListener(NetStatusEvent.NET_STATUS, outgoingStreamHandler);
outgoingStream.publish("media");
var sendStreamObject:Object = new Object();
sendStreamObject.onPeerConnect = function(sendStr:NetStream):Boolean {
receivedMessages.text += "Peer Connected ID: " + sendStr.farID + "n";
return true;
}
outgoingStream.client = sendStreamObject;
}
private function receiveInit():void {
receivedMessages.text += "Initializing Receiving Stream: " + incomingID.text + "n";
incomingStream = new NetStream(netConnection, incomingID.text);
incomingStream.addEventListener(NetStatusEvent.NET_STATUS, incomingStreamHandler);
incomingStream.play("media");
incomingStream.client = this;
}
public function receiveMessage(message:String):void {
receivedMessages.text += "Received Message: " + message + "n";
}
private function outgoingStreamHandler(event:NetStatusEvent):void {
receivedMessages.text += "Outgoing Stream: " + event.info.code + "n";
}
private function incomingStreamHandler(event:NetStatusEvent):void {
receivedMessages.text += "Incoming Stream: " + event.info.code + "n";
}
private function sendMessage():void {
outgoingStream.send("receiveMessage", toSendText.text);
}
private function disconnectNetConnection():void {
netConnection.close();
netConnection.removeEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);
netConnection = null;
}
private function disconnectOutgoing():void {
outgoingStream.close();
outgoingStream.removeEventListener(NetStatusEvent.NET_STATUS, outgoingStreamHandler);
outgoingStream = null;
}
private function disconnectIncoming():void {
incomingStream.close();
incomingStream.removeEventListener(NetStatusEvent.NET_STATUS, incomingStreamHandler);
incomingStream = null;
}
-
initConnection
初始化 NC,sendInit
初始化发送流,receiveInit
基于远对等体初始化传入流我复制粘贴到incomingID.text
中的id - 我把每个结果都打印成
receivedMessages.text
- 我没有防火墙,也没有 NAT。
- Adobe 示例应用程序(http://labs.adobe.com/technologies/cirrus/samples/)完美运行。
我遵循的程序:
- 在应用程序 1 上初始化 NC。(发件人)
- 在应用程序 2 上初始化 NC。(接收器)
- 我将远对等 ID 复制到应用程序 2 并运行 receveInit。
- 我在应用程序 1 上初始化发送流 (sendInit)。
注意:我试图逆转最后 2 个过程,无论哪种方式都有效。
在此之后,我执行 sendMessage() 它从 toSendText.text
读取消息。
不起作用。为什么?我的代码有什么问题?
提前感谢您的每一个有用的答案。
因为 cc.rtmfp.net 是连接检查器,而不是P2P引入服务(p2p.rtmfp.net)。 CC 执行一些快速测试,发送结果并断开连接。 它不执行其他功能,也不提供任何其他服务。
我发现了。
为了开发和测试,我使用Mac并在那里测试了应用程序。没用。仍然不起作用。在Windows上,它可以完美地工作。
奇怪。
您必须为 receiveMessage()
添加处理程序。否则,它只是一个你必须调用它的函数。