<

Flash and Socket Connections - The important bits between the lines

Published on
8,422 Points
2,422 Views
Last Modified:
Approved
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and cross domain files, but after much banging head against desk, and googling, it seems this is quite a common stumbling block and I've not found anything that explicitly describes the process (from the Flash side) for someone with little or no experience doing this.

I created a basic socket class that extends the Socket - to handle the specific messages I'm using, including specific heartbearts, and progress events.

The socket is created in C#.

We got to the point where my swf file in the Flash IDE was connecting, sending and receiving as expected - however when running it in the browser it was connecting, disconnecting but never reconnecting.  Usually when I google a problem I'm having, if a lot of results are returned, I usually take that as a good sign, because if many people are having the same problem, the solution must be out there. However, in this case I didn't find it.

We followed the processes in  this Adobe Article
http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html

In very layman terms, this is how I understand the process:

1. Flash calls socket.connect()
2. This sends a <policy-file-request/> followed by a NULL byte to the port where it is requesting a policy file.
Step 2 happens automatically - you don't need to do anything.
3. The socker server needs to respond to this policy request from flash with the policy document - which is in the form of a string. What we opted to do was have a physical document in a certain location, have the server load and read that document and return it to Flash - which would making editing / updating much easier.

Here is where we had our error.
Our own comms - we have our own "internal" end character which we're using to denote end of message.

However, we  didn't add one to this message from the server.
The Socket was returning the policy as described in the adobe article - with one exception - it didn't have a NULL byte at the end of it. \0

It might be second nature to some, it wasn't to us.
Once that was added, Flash is now able to reconnect and do what is required.
Happiness - hope this little tip helps someone.
0
Author:blue-genie
Enjoy this complimentary article view.

Get unlimited access to our entire library of technical procedures, guides, and tutorials written by certified industry professionals.

Get 7 days free
Click here to view the full article

Using this article for work? Experts Exchange can benefit your whole team.

Learn More
COLLABORATE WITH CERTIFIED PROFESSIONALS
Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Learn from the best.