Browse All Articles
> Flash and Socket Connections - The important bits between the lines
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
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.