I wrote some basic functions in ActionScript in order to use RTMFP:
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 initializes the NC, sendInit initializes the send
stream, receiveInit initializes the incoming stream based on the far peer
id which I copy paste into incomingID.text
I print every results into receivedMessages.text
I do not have firewall, nor NAT.
The Adobe sample application
(http://labs.adobe.com/technologies/cirrus/samples/) works perfectly.
The procedure I follow:
Initialize NC on application 1. (sender)
Initialize NC on application 2. (receiver)
I copy the far peer id to application 2. and run receveInit.
I initialize the sending stream (sendInit) on application 1.
Note: I tried to reverse the last 2 procedure, doesent work either way.
After this, I execute sendMessage() which reads the message from toSendText.text.
Does not working. Why?
What is wrong with my codes?
Thank you for every helpful answer in advance.
because cc.rtmfp.net is the connectivity checker, not the P2P introduction service (p2p.rtmfp.net). cc performs some quick tests, sends the results, and disconnects. it performs no other functions nor provides any other services.
I found out.
For developing and testing, I use Mac and I tested the application there. Did not work. Still doesen't work.
On Windows, it works perfectly.
Strange.
You have to add Handler for receiveMessage(). Otherwise it is just a function that you have to call it.
Related
Hello there I am trying to force a promise to end to get the result from it but it just stucks on loading.
public class CacheController extends Controller {
private AsyncCacheApi cache;
public Result cache()
{
String test = "nice";
cache.set("item.key", test, 15);
Customer user = new Customer("Ana", 12);
CompletionStage<Done> result = cache.set(user.getName(), user);
block(result);
return ok("Cached");
}
public Result checkCache() throws Exception
{
Logger.info("start");
//CompletionStage<String> news = cache.get("item.key");
//news.thenRun(() -> System.out.println("works"));
CompletionStage<Customer> result = cache.get("Ana");
Logger.info("step 1");
Logger.info(cache.get("Ana").toString());
Logger.info("Step 2");
Customer c = block(result);
Logger.info("Step 3 " + c.getName());
//result.thenRun(() -> setUser(result)).thenRun(() -> Logger.info(user.getName() + " " + user.getAge()));
return ok("cancan");
}
private <T> T block(CompletionStage<T> stage) {
try {
return stage.toCompletableFuture().get();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}
When trying to load the page it gets stuck after step2 at line 56: Customer c = block(result); by my guesses
Any ideas to fix it?
#Codrin
I had the same problem. But, see https://www.playframework.com/documentation/2.6.x/JavaCache#Setting-the-execution-context
By default, all Ehcache operations are blocking, and async implementations will block threads in the default execution context.
Maybe CompletableFuture.get() gets stuck because it is executed in the same thread with the caller.
Referring to the linked page, I added snippet below to my application.conf and it worked.
play.cache.dispatcher = "contexts.blockingCacheDispatcher"
contexts {
blockingCacheDispatcher {
fork-join-executor {
parallelism-factor = 3.0
}
}
}
When I execute the following:
private function setUpRecvStream():void {
writeText("Connecting to: " + theirID);
recvStream = new NetStream(netConnection, theirID);
recvStream.addEventListener(NetStatusEvent.NET_STATUS, netStreamHandler);
//play the recvStream
recvStream.play("media");
//attach the stream videoRecv
videoRecv.attachNetStream(recvStream);
}
Using a vaild string for theirID I'm getting the following:
Connecting to: 8640d30e760528b2c88662eacef67d693527e52549eb5e29fc405355a7db147e
NetStream.Connect.Closed
I'm expecting NetStream.Connect.Success since I'm passing in a vaild string for theirID. Any ideas why this might be happening?
Are you initializing your netConnection? Also, the stream you are connecting to might not be publishing "media".
The client you are connecting to must do sendStream.publish("media")
before using this you need to connect to FMS
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
nc.connect('rtmfp://stratus.adobe.com/cbd2224f9a56771b3d4d05c3-bd9b549abca2');
private function netStatusEvent(event:NetStatusEvent):void {
trace('NetConnection status event (1): ' + event.info.code);
if (event.info.code == 'NetConnection.Connect.Success'){
}
}
I installed a FMS 3.5 on my machine and created a new application with main.asc like this :
application.onAppStart = function()
{
/* Allow debugging */
this.allowDebug = true;
}
//Client is connected
application.onConnect = function( client )
{
//Accept the connection
application.acceptConnection( client );
client.allo = function(o) {
trace("test : " + o ) ;
trace("length : " + o.length ) ;
trace("objectEncoding : " + o.objectEncoding ) ;
return o ;
}
}
//Client disconnected
application.onDisconnect = function( client )
{
//Trace on the FMS Application console
trace( client+" is disconnected" );
}
This code prepare a function I call with my flex application, named "allo" and it returns the same byteArray in response.
The flex code is :
var anotherArray:ByteArray = new ByteArray();
anotherArray.objectEncoding = ObjectEncoding.AMF3;
anotherArray.writeObject(new String("foo"));
nconn.call(func, echoResponder, anotherArray);
As a result, I get an empty ByteArray with only length,encoding, endian and position parameters. And a tcpdump trace shows that the ByteArray is empty.
So I wonder if it's only a pointer which is sent, or maybe I misconfigured something.
Do you know a way to investigate further or solve this ?
Thanks for any help,
MP
I tried your code.
Sending...
var bytes:ByteArray = new ByteArray();
bytes.objectEncoding = ObjectEncoding.AMF3;
bytes.writeObject(new String("foo"));
nc.call("allo", new Responder(_onResult, _onStatus), bytes);
... and receiving...
private function _onResult(result:*):void
{
var bytes:ByteArray = ByteArray(result);
var str:String = String(bytes.readObject());
trace(str);
}
traces foo
I think your code is OK. Only difference is that I use FMS 4.
How do I use crossdomain with ftp?
I am trying to do a "hello world" level test of FTP in Flex, but for three days now, I cannot overcome the issue with how to coerce flex into accepting my crossdomain policy - even for testing purposes.
Here is my code: The exact error text follows.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="onInitialize()" layout="vertical">
<mx:Script>
<![CDATA[
import mx.utils.*;
import mx.controls.Alert;
private var fileRef:FileReference;
private var fileSize:uint;
private var fileContents:ByteArray;
//you need to initiate two scokets one for sending
//commands and second for sending data to FTP Server
//socket for sending commands to FTP
private var s:Socket
//responce from FTP
private var ftpResponce:String;
//socket for sending Data to FTP
private var dataChannelSocket:Socket;
//responce from FTP when sending Data to FTP
private var dataResponce:String;
//will hold the IP address of new socket created by FTP
private var dataChannelIP:String;
//will hold the Port number created by FTP
private var dataChannelPort:int;
private var user:String="I have the right user"; //FTP usernae
private var pass:String="the pw is correct"; //FTP Password
private function receiveReply(e:ProgressEvent):void {
ftpResponce=s.readUTFBytes(s.bytesAvailable)
var serverResponse:Number=Number(ftpResponce.substr(0, 3));
if (ftpResponce.indexOf('227') > -1) {
//get the ip from the string response
var temp:Object=ftpResponce.substring(ftpResponce.indexOf("(") + 1
, ftpResponce.indexOf(")"));
var dataChannelSocket_temp:Object=temp.split(",");
dataChannelIP=dataChannelSocket_temp.slice(0, 4).join(".");
dataChannelPort=parseInt(dataChannelSocket_temp[4]) * 256 +
int(dataChannelSocket_temp[5]);
//create new Data Socket based on dataChannelSocket and dataChannelSocket port
dataChannelSocket=new Socket(dataChannelIP, dataChannelPort);
dataChannelSocket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData);
}
//few FTP Responce Codes
switch (String(serverResponse)) {
case "220":
//FTP Server ready responce
break;
case "331":
//User name okay, need password
break;
case "230":
//User logged in
break;
case "250":
//CWD command successful
break;
case "227":
//Entering Passive Mode (h1,h2,h3,h4,p1,p2).
break;
default:
}
//for more please
//http://http://www.altools.com/image/support/alftp/ALFTP_35_help/
//FTP_response_codes_rfc_959_messages.htm
traceData(ftpResponce);
}
private function receiveData(e:ProgressEvent):void {
dataResponce=dataChannelSocket.readUTFBytes(
dataChannelSocket.bytesAvailable);
traceData("dataChannelSocket_response—>" + dataResponce);
}
private function showError(e:IOErrorEvent):void {
traceData("Error—>" + e.text);
}
private function showSecError(e:SecurityErrorEvent):void {
traceData("SecurityError–>" + e.text);
}
private function onInitialize():void {
Security.loadPolicyFile("http://www.myUrlIsCorrectInMyProgram.com/crossdomain.xml");
}
private function createRemoteFile(fileName:String):void {
if (fileName != null && fileName != "") {
s.writeUTFBytes("STOR " + fileName + "\n");
s.flush();
}
}
private function sendData():void {
fileContents=fileRef.data as ByteArray;
fileSize=fileRef.size;
dataChannelSocket.writeBytes(fileContents, 0, fileSize);
dataChannelSocket.flush();
}
//initialize when application load
private function upLoad():void {
fileRef=new FileReference();
//some eventlistener
fileRef.addEventListener(Event.SELECT, selectEvent);
fileRef.addEventListener(Event.OPEN, onFileOpen);
//this function connects to the ftp server
connect();
//send the usernae and password
this.userName(user);
this.passWord(pass);
//if you want to change the directory for upload file
this.changeDirectory("/test/"); //directory name
//enter into PASSV Mode
s.writeUTFBytes("PASV\n");
s.flush();
}
private function onFileOpen(event:Event):void {
}
private function traceData(event:Object):void {
var tmp:String="================================\n";
ta.text+=event.toString() + "\n";
ta.verticalScrollPosition+=20;
}
private function ioErrorEvent(event:IOErrorEvent):void {
Alert.show("IOError:" + event.text);
}
private function selectEvent(event:Event):void {
btn_upload.enabled=true;
filename.text=fileRef.name;
fileRef.load();
}
private function uploadFile():void {
createRemoteFile(fileRef.name);
sendData();
}
private function connect():void {
s=new Socket("ftp.myUrlIsCorrectInMyProgram.com", 21);
s.addEventListener(ProgressEvent.SOCKET_DATA, receiveReply);
s.addEventListener(IOErrorEvent.IO_ERROR, showError);
s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, showSecError);
s.addEventListener(Event.CONNECT, onSocketConnect);
s.addEventListener(Event.CLOSE, onSocketClose);
s.addEventListener(Event.ACTIVATE, onSocketAtivate);
}
private function onSocketConnect(evt:Event):void {
//traceData("OnSocketConnect–>"+evt.target.toString());
}
private function onSocketClose(evt:Event):void {
//traceData("onSocketClose–>"+evt.target.toString());
}
private function onSocketAtivate(evt:Event):void {
//traceData("onSocketAtivate–>"+evt.target.toString());
}
private function userName(str:String):void {
sendCommand("USER " + str);
}
private function passWord(str:String):void {
sendCommand("PASS " + str);
}
private function changeDirectory(str:String):void {
sendCommand("CWD " + str);
}
private function sendCommand(arg:String):void {
arg+="\n";
s.writeUTFBytes(arg);
s.flush();
}
]]>
[SWF] /FTP-debug/FTP.swf - 739,099 bytes after decompression
Warning: Domain www.myUrlIsCorrectInMyProgram.com does not specify a meta-policy. Applying default meta-policy 'master-only'. This configuration is deprecated. See http://www.adobe.com/go/strict_policy_files to fix this problem.
Warning: Timeout on xmlsocket://ftp.myUrlIsCorrectInMyProgram.com:843 (at 3 seconds) while waiting for socket policy file. This should not cause any problems, but see http://www.adobe.com/go/strict_policy_files for an explanation.
Warning: [strict] Ignoring policy file at xmlsocket://ftp.myUrlIsCorrectInMyProgram.com:21 due to incorrect syntax. See http://www.adobe.com/go/strict_policy_files to fix this problem.
* Security Sandbox Violation *
Connection to ftp.myUrlIsCorrectInMyProgram.com:21 halted - not permitted from http://localhost/FTP-debug/FTP.swf
Error: Request for resource at xmlsocket://ftp.myUrlIsCorrectInMyProgram.com:21 by requestor from http://localhost/FTP-debug/FTP.swf is denied due to lack of policy file permissions.
The "Information" at the URL's listed above is categorically unintelligable to me.
Please, someone help!
I also had the same issue but was able to fix it using the flash policy server that I downloaded from http://www.flash-resources.net/download.html.
I ran this on the same machine that I have my tomcat server installed and made the call
Security.loadPolicyFile("xmlsocket://:843");
from the application and it worked perfectly. No errors.
I also had the same issue but was able to fix it using the flash policy server that I downloaded from here.
I ran this on the same machine that I have my tomcat server installed and made the call
Security.loadPolicyFile("xmlsocket://Machine Name:843");
from the application and it worked perfectly. No errors.
Watch the typo around the Machine Name in the last post.
See the crossdomain spec:
http://learn.adobe.com/wiki/download/attachments/64389123/CrossDomain_PolicyFile_Specification.pdf?version=1
This covers the warning you have and can help you get this working.
Is it possible in an air application to start a download, pause it and after that resume it?
I want to download very big files (1-3Gb) and I need to be sure if the connection is interrupted, then the next time the user tries to download the file it's start from the last position.
Any ideas and source code samples would be appreciated.
Yes, you would want to use the URLStream class (URLLoader doesn't support partial downloads) and the HTTP Range header. Note that there are some onerous security restrictions on the Range header, but it should be fine in an AIR application. Here's some untested code that should give you the general idea.
private var _us:URLStream;
private var _buf:ByteArray;
private var _offs:uint;
private var _paused:Boolean;
private var _intervalId:uint;
...
private function init():void {
_buf = new ByteArray();
_offs = 0;
var ur:URLRequest = new URLRequest( ... uri ... );
_us = new URLStream();
_paused = false;
_intervalId = setInterval(500, partialLoad);
}
...
private function partialLoad():void {
var len:uint = _us.bytesAvailable;
_us.readBytes(_buf, _offs, len);
_offs += len;
if (_paused) {
_us.close();
clearInterval(_intervalId);
}
}
...
private function pause():void {
_paused = true;
}
...
private function resume():void {
var ur:URLRequest = new URLRequest(... uri ...);
ur.requestHeaders = [new URLRequestHeader("Range", "bytes=" + _offs + "-")];
_us.load(ur);
_paused = false;
_intervalId = setInterval(500, partialLoad);
}
if you are targeting mobile devices, maybe you should take a look at this native extension: http://myappsnippet.com/download-manager-air-native-extension/ it supports simultaneous resumable downloads with multi-section chunks to download files as fast as possible.