basic sample running in LAN, but not in WAN
Hi ,
Started working on RTMFP after reading that it is suitable for chats through UDP protocol,
but lately found that it is working fine in LAN, but not able to connect peers who are located away ,
pl find code below and let me know where i am going wrong:
Source file
**************************************************************
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="309" height="558" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script source="imports/imports.as" />
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private const SERVER_ADDRESS:String = "rtmfp://p2p.rtmfp.net/";
private const DEVELOPER_KEY:String = "";
private var nc:NetConnection;
private var myPeerID:String;
private var farPeerID:String;
public var microphone:Microphone, camera:Camera,video:Video,remoteVideo:Video;
// streams
private var sendStream:NetStream;
private var recvStream:NetStream;
private function initConnection():void{
Security.allowDomain("*");
ExternalInterface.marshallExceptions = true;
if(txtFingerprint.text){
farPeerID = txtFingerprint.text;
}
// Alert.show( "ExternalInterface :: " +ExternalInterface.available );
// ExternalInterface.addCallback("initRecvStream" , initRecvStream); // 4
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,ncStatus);
nc.connect(SERVER_ADDRESS+DEVELOPER_KEY);
// remoteVideo = new Video(320 , 240);
// hisWebCam.addChild(remoteVideo);
prepareWebcamAndMicro(); // 3
}
private function ncStatus(event:NetStatusEvent):void{
//Alert.show(event.info.code);
myPeerID = nc.nearID;
txtFingerprint.text = myPeerID;
switch (event.info.code){
case "NetConnection.Connect.Success":
// Alert.show("NetConnection.Connect.Success");
//myUser.idStratus = netConnection.nearID;
//ExternalInterface.call("connected" , myUser.username , myUser.idStratus );
initSendStream(); // 2.1
/***********************/
if(ExternalInterface.available){ // 1
try {
ExternalInterface.addCallback("test", test);
}
catch (error:SecurityError)
{
Alert.show("A SecurityError occurred: " + error.message + "\n");
throw error;
}
catch (error:Error)
{
Alert.show("An Error occurred: " + error.message + "\n");
throw error;
}
}//1
else{
Alert.show("External interface is not available for this container.");
}
/***********************/
break;
case "NetConnection.Connect.Closed":
// myUser.connected = false;
break;
case "NetStream.Connect.Success":
//initRecvStream();
// we get this when other party connects to our control stream our outgoing stream
// debug("Connection from: " + event.info.stream.farID );
/* myUser.connectedWithSomeOne = true; */
break;
case "NetConnection.Connect.Failed":
// debug("Unable to connect\n");
break;
case "NetStream.Connect.Closed":
resetStreams();
// myUser.connectedWithSomeOne = false;
// ExternalInterface.call("endChat");
break;
}
/* var timer:Timer = new Timer(50);
timer.start() */;
}
private function initSendStream():void{
// Alert.show("initSendStream");
var sendStreamClient:Object = new Object();
sendStreamClient.onPeerConnect = function(callerns:NetStream):Boolean{
//Alert.show("farPeerID @onPeerConnect @@ initSendStream ==> " + farPeerID);
farPeerID = callerns.farID;
//Alert.show("onPeerConnect "+farPeerID);
return true;
}
sendStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
sendStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
sendStream.client = sendStreamClient;
if (microphone){
sendStream.attachAudio(Microphone.getEnhancedMicrophone());
}
if (camera) {
sendStream.attachCamera(camera);
}
var timer:Timer = new Timer(50);
timer.start();
sendStream.publish("media");
}
public function initRecvStream():void{
// Alert.show("< == initRecvStream == > " );
recvStream = new NetStream(nc,farPeerID);
recvStream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
recvStream.play("media");
recvStream.client = this;
remoteVideo.attachNetStream(recvStream);
}
private function netStatusHandler(event:NetStatusEvent):void{
//Alert.show(event.info.code);
}
private function prepareWebcamAndMicro():void {
if(Microphone.isSupported){
microphone = Microphone.getEnhancedMicrophone();
}
if( Camera.isSupported ){
camera = Camera.getCamera();
}
// Alert.show(""+camera.muted );
if (camera == null) {
Alert.show("Webcam not found");
return;
} else {
if (camera.muted) {
Security.showSettings(SecurityPanel.PRIVACY);
}
camera.setMode(320 , 240 , 15);
camera.setQuality(0,90);
//camera.addEventListener(StatusEvent.STATUS, camera_status);
/**** camera has to be added to video , video has to be added to display part of swf to be viewed. ****/
video = new Video(320 , 240);
video.attachCamera(camera);
video.cacheAsBitmap = true;
myWebCam.addChild(video);
}
}
public function resetStreams():void {
var i:int;
//debug("reset STreams");
// Alert.show("::==>" +sendStream.peerStreams.length );
if (sendStream!=null) {
// debug("sendStream.peerStreams.length="+sendStream.peerStreams.length);
for ( i=0; i<sendStream.peerStreams.length;i++) {
sendStream.peerStreams.close();
}
}
// if (recvStream==null) return;
return;
}
]]>
</fx:Script>
<!--<mx:TextInput x="10" y="40" id="txtSendData"/>
<mx:TextInput x="10" y="70" id="txtReceiveData" width="251"/>-->
<!--<mx:Button x="178" y="40" label="Send data" click="sendSomeData()"/>-->
<!--<mx:Button x="24" y="47" label="initSendStream" click="initSendStream()"/>-->
<!-- <mx:Button x="506" y="10" label="initReceiveStream" click="initRecvStream();"/>-->
<!--<mx:Text x="317" y="48" text="Hint: First running Flash app - click Connect to get Fingerprint PeerID. Copy and paste this PeerID to second running Flash app to the same field and click Connect. Then initSendStream and initReceiveStream on both of them and finally you can write some text and click Send data." width="391" height="83"/>-->
<s:VideoDisplay id="myWebCam" x="9" y="229" width="289" height="319" scaleMode="none"/>
<!--<s:VideoDisplay id="hisWebCam" x="520" y="88" width="375" height="319" scaleMode="none"/>-->
<s:Panel x="1" y="10" width="306" height="201">
<mx:TextInput x="10" y="10" width="284" id="txtFingerprint"/>
<mx:Button x="61" y="54" label="Connect" click="initConnection()"/>
<mx:Button x="169" y="54" label="Reset" click="resetStreams()"/>
<s:Label x="109" y="140" height="18" text="My Video"/>
<mx:HRule x="10" y="119" width="284"/>
</s:Panel>
</s:Application>
**************************************************************
Destination file to recieve peer id and plays video
**************************************************************
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="309" height="558" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script source="imports/imports.as" />
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private const SERVER_ADDRESS:String = "rtmfp://p2p.rtmfp.net/";
private const DEVELOPER_KEY:String = "efc4ea8add28063205e0b187-2bccb3dbca4a";
private var nc:NetConnection;
private var myPeerID:String;
private var farPeerID:String;
public var microphone:Microphone, camera:Camera,video:Video,remoteVideo:Video;
// streams
private var sendStream:NetStream;
private var recvStream:NetStream;
private function initConnection():void{
Security.allowDomain("*");
ExternalInterface.marshallExceptions = true;
if(txtFingerprint.text){
farPeerID = txtFingerprint.text;
}
// Alert.show("farPeerID:" + farPeerID);
// Alert.show( "ExternalInterface :: " +ExternalInterface.available );
// ExternalInterface.addCallback("initRecvStream" , initRecvStream); // 4
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,ncStatus);
nc.connect(SERVER_ADDRESS+DEVELOPER_KEY);
remoteVideo = new Video(320 , 240);
hisWebCam.addChild(remoteVideo);
//prepareWebcamAndMicro(); // 3*/
}
private function ncStatus(event:NetStatusEvent):void{
//Alert.show(event.info.code);
myPeerID = nc.nearID;
txtFingerprint.text = myPeerID;
switch (event.info.code){
case "NetConnection.Connect.Success":
//myUser.idStratus = netConnection.nearID;
//ExternalInterface.call("connected" , myUser.username , myUser.idStratus );
/***********************/
if(ExternalInterface.available){ // 1
try {
//ExternalInterface.addCallback("test", test);
}
catch (error:SecurityError)
{
Alert.show("A SecurityError occurred: " + error.message + "\n");
throw error;
}
catch (error:Error)
{
Alert.show("An Error occurred: " + error.message + "\n");
throw error;
}
}//1
else{
Alert.show("External interface is not available for this container.");
}
/***********************/
break;
case "NetConnection.Connect.Closed":
// myUser.connected = false;
break;
case "NetStream.Connect.Success":
//initRecvStream();
// we get this when other party connects to our control stream our outgoing stream
// debug("Connection from: " + event.info.stream.farID );
/* myUser.connectedWithSomeOne = true; */
break;
case "NetConnection.Connect.Failed":
// debug("Unable to connect\n");
break;
case "NetStream.Connect.Closed":
resetStreams();
// myUser.connectedWithSomeOne = false;
// ExternalInterface.call("endChat");
break;
}
/* var timer:Timer = new Timer(50);
timer.start() */;
}
public function initRecvStream():void{
//s Alert.show("< == initRecvStream == > " +farPeerID);
recvStream = new NetStream(nc,farPeerID);
recvStream.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
recvStream.play("media");
recvStream.client = this;
remoteVideo.attachNetStream(recvStream);
}
private function netStatusHandler(event:NetStatusEvent):void{
switch (event.info.code){
case "NetConnection.Connect.Success":
break;
case "NetConnection.Connect.Closed":
// myUser.connected = false;
break;
case "NetStream.Connect.Success":
//initRecvStream();
// we get this when other party connects to our control stream our outgoing stream
// debug("Connection from: " + event.info.stream.farID );
/* myUser.connectedWithSomeOne = true; */
break;
case "NetConnection.Connect.Failed":
// debug("Unable to connect\n");
break;
case "NetStream.Connect.Closed":
resetStreams();
// myUser.connectedWithSomeOne = false;
// ExternalInterface.call("endChat");
break;
}
}
public function resetStreams():void {
var i:int;
//debug("reset STreams");
if (sendStream!=null) {
// debug("sendStream.peerStreams.length="+sendStream.peerStreams.length);
for ( i=0; i<sendStream.peerStreams.length;i++) {
sendStream.peerStreams.close();
}
}
// if (recvStream==null) return;
return;
}
]]>
</fx:Script>
<!--<mx:TextInput x="10" y="40" id="txtSendData"/>
<mx:TextInput x="10" y="70" id="txtReceiveData" width="251"/>-->
<!--<mx:Button x="178" y="40" label="Send data" click="sendSomeData()"/>-->
<!--<mx:Button x="24" y="47" label="initSendStream" click="initSendStream()"/>-->
<!--<mx:Text x="317" y="48" text="Hint: First running Flash app - click Connect to get Fingerprint PeerID. Copy and paste this PeerID to second running Flash app to the same field and click Connect. Then initSendStream and initReceiveStream on both of them and finally you can write some text and click Send data." width="391" height="83"/>-->
<!--<s:VideoDisplay id="myWebCam" x="62" y="88" width="363" height="319" scaleMode="none"/>-->
<s:VideoDisplay id="hisWebCam" x="9" y="229" width="289" height="319" scaleMode="none"/>
<s:Panel x="10" y="10" width="289" height="207">
<mx:TextInput x="5" y="10" width="277" id="txtFingerprint"/>
<mx:Button x="41" y="47" label="Connect" click="initConnection()"/>
<mx:Button x="145" y="47" label="Receive Video" click="initRecvStream();"/>
<mx:HRule x="10" y="125" width="267" height="8"/>
<s:Label x="117" y="146" height="18" text="His Video"/>
</s:Panel>
</s:Application>
************************
help me out to move forward to work it ,
thanks for your support
