This post by Ryan Stewart motivated me for a little example with sockets in AIR. I wanted a way to upload files to a server with the possibility to interrupt uploads at any time and pick them up later.
So to do this I thought it would be cool if it works like streaming video to the client but just the other way around. I developed a mini protocol to do this. It works like this:
1. Client (AIR app) establishes a socket connection to Server (Java based in my case)
2. If connection is succesful client send a request which looks like this: 1 byte = operation type 1, length of the body, filename to upload as UTF bytes
3. Server checks if the file with the given filename already exists if so it responds with 1byte = operation type 1, a long (64bit) value which is the size of the file on the server or 0 if it does not yet exist.
4. Client send a package from the file starting at the size of the file on the server: operation type = 2 (1byte), length of body = length of the package, bytes of the package
and so on and so on… You get the idea.
With this mechanism the upload can be interupted and picked up any time. The client and the server always know what`s happening and were the bytes belomg to.
Warning! This is a very very over symplified, incomplete example. No real error handling, no handling for multiple clients, security?…
It`s just a prove of concept. I`m not a java developer and only hacked this together very quickly. The source is really ugly and normally I would not post something like this but as I always promise to clean up everything and post later (and rarely do ;-)) i just post them here so you can get the idea and build something sophisticated out of it.
Here is a screencast that shows the app in action.
Get the sources (again really dirty sources only use for inspiration)
Leave a Reply