View Full Version : Flash socket policies
Apolva
06-06-2010, 12:13 PM
I was just wondering if anyone's managed to get flash socket policies to work. Everytime I try it apparently "times out while waiting for a response" from my Java server (which serves the xml policy and the actual game data). The Java server definitely gets the "<policy-file-request/>" and answers it, yet it still times out.
My VirtualRoom was going brilliantly until I hit this wall, any help would be appreciated. :(
Tried:
Forwarding port + disabling firewall.
Setting up another policy server on port 843.
Putting a crossdomain.xml in the root of the webserver.
The socket works fine inside Flash itself, but when I view the swf in a webpage it won't connect.
MattFr
06-06-2010, 12:16 PM
This was the biggest problem me and someone else had when trying to do socket connections in AS2. Since moving to AS3, I haven't had the problem, but that may be something to do with the fact I'm still doing everything in a debug player and within a VPN.
I shall get in contact with the person I develop with and see if he can remember how we sorted it before, bear with me :D
Apolva
06-06-2010, 12:39 PM
OK that'd be great thanks :)
This is driving me insane :P
Source
06-06-2010, 01:09 PM
I had the same issue when I was making a chat server for a client. Switched the AS3 and everything just went to much more smoothly.
I would recommend upgrading, but you have so much work already in AS2 that your only saviour would be MattFr's friend. Otherwise, you are re-writing to AS3 :P
MattFr
06-06-2010, 01:14 PM
Yes, definitely switch to AS3, your code will be a lot nicer :D My friend doesn't come on till late, but I'll let you know if he remembers how we fixed it, as I said.
Apolva
06-06-2010, 09:05 PM
Yeah, I do want to switch to AS3 but means most of what I know goes down the drain since I'd need to be familiar with lots of new concepts (+ rewrite all my stuffs).
MattFr
06-06-2010, 09:07 PM
Yeah, I do want to switch to AS3 but means most of what I know goes down the drain since I'd need to be familiar with lots of new concepts (+ rewrite all my stuffs).
Better to do it sooner, you don't have to rewrite as much. It is seriously worth switching. I asked my friend, he can't remember, sorry :(
Apolva
06-06-2010, 09:12 PM
Aww that's annoying, thanks anyway. Was going to get the AS2 client up and running, then rewrite in AS3 while people used the initial copy.
Edit - reading some AS3 examples now :)
Apolva
07-06-2010, 05:26 PM
OK I'm rewriting in AS3 now :) seems a much neater language lol.
Just hope this will solve the problem.
MattFr
07-06-2010, 05:27 PM
OK I'm rewriting in AS3 now :) seems a much neater language lol.
Just hope this will solve the problem.
Good man :D If you need any help just ask.
Apolva
07-06-2010, 05:49 PM
Just wondering if
num1,num2,num3 = 5;
would be the equivalent of
num1=5;
num2=5;
num3=5;
There are no compile errors, would it work? :P
MattFr
07-06-2010, 05:51 PM
If there are no compile errors it should be fine :P
Source
07-06-2010, 08:54 PM
Just do the socket only part first to make sure that it will solve the problem.
OK I'm rewriting in AS3 now :) seems a much neater language lol.
Just hope this will solve the problem.
Apolva
08-06-2010, 08:43 PM
Still no luck :(
Here's what I have so far, having rewritten some of it in AS3 (this is just the connection.as include - someone please tell me if I'm doing someting wrong :P)
Have tried adding crossdomain.xml to the root of the domain, also tried with/without the final null character in the policy response.
Instead of going to the policyfile log, it pops up in an error window now.
Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: http://blah blah blah/VirtualRoom.swf cannot load data from 192.168.1.86:4049.
at VirtualRoom_fla::MainTimeline/frame3()
Extract of Java server code (which responds to policy request)
...
// It's checking the packet it received against a huge wad of if/else's (BP ik)
} else if (line.equals("<policy-file-request/>")) {
System.out.println("Answered policy request."); // This is shown, so the if() is definitely matching
out.print("<cross-domain-policy>"
+ "<allow-access-from domain=\"*\" to-ports=\"*\" />"
+ "</cross-domain-policy>"+EOF);
disconnect = true;
}
...
The connection.as
var serverHost:String = "192.168.1.86";
var serverPort:uint = 4049;
// VirtualRoom Connection functions
// Get socket policy from the same port.
Security.loadPolicyFile("xmlsocket://" + serverHost + ":" + serverPort);
// Show connecting text (timer)
var cddd:int = 0;
var connectionMessage:String="Connecting to server";
var connectingTextTimer:uint = setInterval(function(){
cddd++;
statusBox.text=connectionMessage;
for(var i:int=0;i<cddd%4;i++) statusBox.appendText(".");
},500);
// Socket + Socket Events
var xmlSocket:XMLSocket=new XMLSocket();
xmlSocket.addEventListener(Event.CONNECT,socketCon nect);
xmlSocket.addEventListener(Event.CLOSE,socketClose );
xmlSocket.addEventListener(DataEvent.DATA,socketRe ceive);
function socketConnect(status):void{
if (status){
connectionMessage="Checking server";
sendToServer("h");
}else{ clearInterval(connectingTextTimer);
messagebox("Cannot connect to server","Connection Issues");//,function(){_root.loadMovie(_root._url);});
statusBox.text="Connection failed";
clearInterval(connectingTextTimer);
}
}
function socketReceive(rec):void{
var rec=rec.data.toString().trim().decodeHTML();
if(debugMode) messageBox(rec,"Packet Received");
var packetData=rec.substr(1,rec.length-1);
switch(rec.substr(0,1)){
case "h":
// Handshake packet
loginForm.visible=true;
statusBox.text="Connected";
clearInterval(connectingTextTimer);
break;
case "n":
// Notice message (message box)
packetData=packetData.split("|");
if(!packetData[0]) return void;
if(packetData[1]) messageBox(packetData[1],packetData[0]);
else messageBox(packetData[0]);
return void;
break;
case "a":
// Authenticated
userData["id"]=packetData;
gotoAndStop(4);
break;
default:
if(debugMode) messageBox("Previous packet was unrecognised.","Error");
break;
}
}
function socketClose(event):void{
trace("disconnected");
//unloadRoom();
messagebox("Server connection lost","Disconnected");//,function(){_root.loadMovie(_root._url);});
}
function sendToServer(packet:String=""):void{
xmlSocket.send(packet+"\n");
}
// Connect to server
xmlSocket.connect(serverHost,serverPort);
MattFr
08-06-2010, 08:51 PM
I dunno what to say dude, I haven't crossed this bridge yet. You've probs checked this stuff, but here's some late night reading for you
http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/js/html/wwhelp.htm?href=00001950.html
http://www.adobe.com/devnet/flashplayer/articles/cross_domain_policy.html
http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html
Please, please, please let us know if you find a solution, will help me greatly!
Source
09-06-2010, 01:07 AM
The first call to order is make sure the JAVA server is doing what it is meant to do.
Make a simple java client and see if it can successfully pull the policy file from the java server. Once you have done that, you know you can focus on the flash side of things.
I haven't read all that you have posted, will take a deeper look not to see if anything stands out.
Edit: Just skimming over the policy you are outputting, I'm not sure if flash requires the the xml opening tag. ( <?xml version="1.0"?> )
Edit2: And just another sort of obvious suggestion. In my personal usage I went for the direct route of serving the policy on port 843 (which is where flash checks first, then after a 3 second timeout moves on ) But that then is completely incorrect as you have already stated you are getting the request to give serve the policy.
Apolva
09-06-2010, 10:10 AM
Success!
I found this: http://zappmonkey.com/journal/flash-policy-server/
That did the trick, it seems they've changed it so that you can only serve it on port 843 now, rather than the port you're accessing (at least that's what I've found).
+Rep both for the suggestions
Source
09-06-2010, 12:47 PM
Still makes no sense as to why Flash was requesting the policy from the port you already had a connection to.
Apolva
09-06-2010, 03:24 PM
Still makes no sense as to why Flash was requesting the policy from the port you already had a connection to.
I know, it even requested it without the
Security.loadPolicyFile("xmlsocket://" + serverHost + ":" + serverPort);
Extra note - because *nix machines protect ports 1024 and below, I had to run the server as root (with sudo)
Delatory
09-06-2010, 09:18 PM
My server (C#) handles both the Policy and Server of the program. Whenever a client connects it sends out the Policy whether they want it or not and it seems to work just fine.
Also,
line.equals("<policy-file-request/>")
didn't work because of white space which is on the end of the string.
I use this function
private string removeWhite(string msg)
{
string ret = "";
int z = msg.Length;
for (int i = 0; i < z; i++)
{
int c = msg.Substring(i, 1).ToCharArray()[0];
if (c != 0)
{
ret += msg.Substring(i, 1);
} else {
return ret;
}
}
return ret;
}to remove the white space and return just the part of the string which we want.
or instead you could use
if(line.startsWith("<policy-file-request/>") == true) {
Apolva
10-06-2010, 10:16 AM
Forgot to mention, I had
line = line.trim(); beforehand (hence matching the if statement).
Definitely matches, but mysteriously times out.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2026 vBulletin Solutions Inc. All rights reserved.