Connect to IP in application layer, Adobe Air - actionscript-3

I'm working on a project about "controlling embedded system by smartphones via wifi",
I developed my app on adobe Air, and built my system on PIC microcontroller to accept connections on application layer (Http), the ip address is 192.168.20.60:80(responsed when browsing from web browsers).
Instead of using sockets to connect through port 80, I used urlloader to read HTML pages (urlrequest("x.com")).
But when I tried to connect to ip address IOexception appears,
here's the code:
var PicPage:URLLoader=new URLLoader();
PicPage.load(new URLRequest("192.168.20.60")); //worked using DNS
lgn.addEventListener(Event.COMPLETE,ff);
function ff(e:Event):void{
var s:String=e.target.data;
trace(s);
}

As Pixel Elephant said, You need to have http:// in front of the IP you are trying to contact. You don't need to specify port 80 though.
Try the following code:
var PicPage:URLLoader = new URLLoader();
PicPage.addEventListener(Event.COMPLETE, onLoaded);
PicPage.addEventListener(IOErrorEvent.IO_ERROR, onError);
PicPage.load(new URLRequest("http://192.168.20.60/"));
protected function onLoaded(e:Event):void {
var s:String = e.target.data;
trace(s)
}
protected function onError(e:Event):void {
// Handle the error in any way you'd like
}
I've used this code ( and variations of it ) multiple times myself and it has served me well.

Related

Why am I receiving a flash player security error when trying to access facebook graph api photos?

I'm using the flash debug player(version 16) and I'm trying to access a users photo from within flash as3. Firstly, the Security error i'm receiving, here it is.
SecurityError: Error #2122: Security sandbox violation: Loader.content: http s://graffiti-galore.herokuapp.com/fb/GraffitiGalore.swf?build=0001 cannot access http s://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-1/c46.46.570.570/s50x50/398269_10152199084495525_2139734276_n.jpg?oh=29be37fd74f7d338a7b5c23b9aadd474&oe=5548F552&gda=1431395518_d142c304e8b8afa8df340e6cac093b29. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
at flash.display::Loader/get content()
at PictureBox/onCompleteLoadingImage()
I'm using the free Heroku dino that is being offered and I'm simply doing a fetch call to the graph api users object. fetching the id,first_name, last_name,picture.
The picture url comes back as hosted on https facebook servers.
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-1/c46.46.570.570/s50x50/398269_10152199084495525_2139734276_n.jpg?oh=29be37fd74f7d338a7b5c23b9aadd474&oe=5548F552&gda=1431395518_d142c304e8b8afa8df340e6cac093b29
Links like the one above. For some reason i'm receiving this Security error whenever loading up the image from my flash as3 facebook app.
Below is the code i'm using to fetch the image.
[code]
public function setupPhoto(url:String):void
{
removeAllChildren();
var urlRequest1:URLRequest = new URLRequest(url);
this.loader1 = new Loader();
this.loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadingImage);
this.loader1.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErrorLoadingImage);
this.loader1.load(urlRequest1);
//imageLoader = new ImageLoader(url);
//imageLoader.addEventListener(Event.COMPLETE, onCompleteLoadingImage);
//imageLoader.load();
}
private function onErrorLoadingImage(e:IOErrorEvent):void
{
debugPanel.writeLine("ERROR LOADING IMAGE!!!");
debugPanel.writeLine(e.text);
}
public function onCompleteLoadingImage(e:Event):void
{
//var sprite:Sprite = this.loader1.content as Sprite;
//updatePicture(sprite);
updatePicture1(this.loader1.content);
}
[/code]
this works for me with Facebook:
var lc:LoaderContext = new LoaderContext(true, null, null);
// now add the LoaderContext to your loader
this.loader1.load(urlRequest1, lc);
you don't have to create a LoaderContext every time, just create it once and use everywhere.

How to use httpget on AdobeAIR?

Using some home automation software that allows me to use httpget/httppost to get the status and control the state of devices in my home. I can use it just fine when I'm on my home wifi network (using the internal IP address in the URL), but when I attempt the same outside of my network, nothing happens. I have all ports forwarded properly, because if I post the exact same URL into my browser, it will perform whatever task was indicated.
An example would be:
function LampOn(event:MouseEvent):void{
import flash.net.*;
var url:String = "http://" + ipaddy + "/HomeSeer_REST_API.aspx?function=setdevicebyname&param1=lamp&param2=on";
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.load(request);
}
When I use my home IP in the variable 'ipaddy' it works fine, when I use my external IP (in the form username:password#ipaddress:port ) it does nothing.

Can't connect to host using rtmfp

I have a p2p game using Adobe's rtmfp protocol and cirrus server. Everything seems to be working fine when I try it in stand alone flash player, but when I embed the game to the web page, suddenly nobody can't connect to my game. There are no errors, the connection just timeouts after a while.
here is how I create NetConnection on the host:
netConnection = new NetConnection();
netConnection.maxPeerConnections = 20;
netConnection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
netConnection.connect("rtmfp://p2p.rtmfp.net/3a34e[redacted]af45f80/");
public function onNetStatus(event:NetStatusEvent) : void
{
var peer:P2PNetPeer = null;
switch(event.info.code) {
case "NetConnection.Connect.Success":
streamOut = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS);
streamOut.client = {onPeerConnect:onPeerConnect};
streamOut.publish(netConnection.nearID);
break;
}
It's almost same on client side, except when I get NetConnection.Connect.Success I create inStream:
streamIn = new NetStream(netConnection, farId);
streamIn.client = {msg:onMessageReceived};
streamIn.addEventListener(NetStatusEvent.NET_STATUS, onInStreamStatus, false, 0, true);
streamIn.play(farId);
This inStream is the one that causes problem, it never connects to the host specified by his farId (I pair players and exchange near/farIds using my own server) if the game is run from web page. When I run it locally using stand alone player, it connects almost immediatelly.
I am checking for other states and errors as well, but they are not important here (as said, I am not getting any errors, just timeout after a while)
Any idea what might be going on here?

AS3 using the loader CLASS with .content

I've been working on a program, but currently I'm stuck.
My problem is the .content statement in my script. Since I'm loading a PNG from the web I have the Local playback security set to "Access network only". When I load pictures locally (of course with settings at "Access local files only", and local URLRequests) the program works just fine, but when loading from the web the .content makes my program stop, I've found this out with lots of testing. It seems the .content only works when the SWF file only uses local requests, is this correct?
I know that I can use addChild(loader), but I need to get the PNG file out of the loader so that I can turn it into a bitmap and edit it. Any ideas? The script is below.
var loader:Loader = new Loader;
var ar:Array = [];
var teller:Number = 0;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, paste);
knapp.addEventListener(MouseEvent.CLICK, neste);
function paste(evt) {
ar[teller] = loader.content;
ar[teller].x = ar[teller].width*teller;
addChild(ar[teller]);
}
function neste(evt) {
teller ++
loader.load( new URLRequest ("http://www.minecraft.net/skin/Notch.png"));
}
loader.load( new URLRequest ("http://www.minecraft.net/skin/Notch.png"));
Cross-domain policies prohibits image data manipulation for images loaded from another domain than the one the swf file is loaded from:
http://www.dwuser.com/flashslideshow/v4/help/advanced_topics/understanding_flash_player_cross_domain_loading_restrictions.html

Flash SecurityDomain, P2P settings and multiply swfs

I have two swfs:
application swf
p2p client swf that allows to load data using rtmfp replication
technology (through cirrus service)
The main idea is to have one p2p loader on the certain domain that will able to work in p2p network without asking many times for permission for each domain, so for instance:
application 1 (http://domain1.com/app.swf) |
application 2 (http://domain2.com/app.swf) | <--> p2p data loader (http://domainp2p.com/p2pcli.swf)
application N (http://domainN.com/app.swf) |
The p2p client loads binary data by request, content really does not matter I believe.
So, I load p2pclient swf using following class (app.swf)
public class ClientLoader {
// .. some code
public function load(cb:Function, err:Function):void
{
_cb = cb;
_err = err;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, _onIoError);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _onSecurityError);
// note that context has neither application domain nor security domain
loader.load(new URLRequest(_url), new LoaderContext());
}
private function _onLoaded(e:Event):void
{
trace("Loaded. Connecting to the p2p network...");
_client = e.target.content;
_client.addEventListener(Event.CONNECT, _onClientReady);
_client.connect();
}
private function _onClientReady(e:Event):void
{
_cb(_client);
}
}
}
The p2pclient itself (p2pcli.swf):
public class P2P extends Sprite
{
public function SqP2P() {
Security.allowDomain("*");
}
public function connect():void
{
_connection = new NetConnection();
_connection.addEventListener(NetStatusEvent.NET_STATUS, _netStatus);
_connection.connect(CIRRUS_ADDRESS, CIRRUS_KEY);
// after successful connect this method called
_loadGroup();
}
private method _loadGroup():void
{
var spec:GroupSpecifier = new GroupSpecifier(_name);
spec.serverChannelEnabled = true;
spec.objectReplicationEnabled = true;
_group = new NetGroup(connection, spec.groupspecWithAuthorizations());
_group.addEventListener(NetStatusEvent.NET_STATUS, _netStatus);
}
private function _netStatus(event:NetStatusEvent):void
{
trace("NetStatusEvent:", event.info.code);
}
}
But it looks like that Flash Player ignores the security session and is trying to save the pop-up settings for the domain that app.swf belongs to, but not for the p2pcli.swf domain. Why?!
I have absolutely same code, but p2pcli.swf replaced with swf that stores data in Local Shared Object and all the domain1-2-N.com has access to it.
Any ideas?
I know, my English is crap :(
I really am not totally sure, but I'll throw my answer out there in case it is helpful.
Based on the general purpose of such security messages, I'm not entirely sure you CAN keep that dialog from showing up. In some cases, I'm certain Peer Assisted Networking can be a security risk for some people (and anyway, it is using their bandwidth.) The settings for turning on and off that notification are user-side, in the Flash settings dialog (Control Panel in Windows 7...), thus that hints that it is inherently hardwired into the Flash platform.
Of course, since I'm more of an Adobe AIR specialist, I could be completely wrong...for your project's sake, I'm sincerely hoping I AM!
And, for the record, your English was almost perfect. I tweaked one paragraph for clarity, but otherwise, spot on. :D