Hi All,
after long time I’m back for all the developers are working with the Flash Platform right now!
Sorry for that but it was a really intensive period for me with the organization of “Having fun with Adobe AIR” so I haven’t a lot of time to share with you my new experiments.
Yesterday Adobe MAX is finished with a lots of design news, great and inspire case histories for designers and a lot of amusement during the Sneak Peek where they have shown the real power of Adobe labs with tons of really cool features that we could see in next releases of Adobe’s softwares.
For a developer perspective there weren’t big announces so, as usual, we can do it by ourselves…. and here we are!
During last few days Adobe release the Adobe AIR 3.8 and Flash Player 11.8, both in BETA but you can download and start to play with them.
When Adobe releases a new AIR SDK I always take a look to the release notes to see the new features of my favorite platform, this time I’m glad to announce that they add the opportunity to create TCP/IP and UDP socket server directly on iOS and Android.
This is a very cool feature because you can really create amazing things in particular for applications and games, for example local multiplayer, chat and so on.
I worked a lots with sockets during last years in several projects and my big concern was that I can’t create a socket server on smartphone and tablet with AIR, I could do that only with native code but I was pretty sure to see this feature will be implemented in next releases of AIR!
Today I had few time to spend experimenting new stuff so I decided to try AIR 3.8 BETA on mobile and create something cool to share with you.
As you can see in this short video I create a socket server on my iPhone 4 that interact with a client made on my iPad mini (I tried also with my Android smartphone and it works as well):
To create this sample you needn’t to learn something new, you can use the same APIs you will use on a desktop application, so to create a socket server you write those few lines of code:
//creation of a TCP/IP Socket server
private function createServer():void{
server = new ServerSocket();
server.addEventListener(Event.CONNECT, onConnect);
server.bind(7934); // this is the number of the port where your socket communicate
server.listen();
}
Then, when a client socket will join in the same network and it listens the same port of the server, the magic happens and you can start to comunicate:
//on the server socket application protected function onConnect(event:ServerSocketConnectEvent):void { incomingSocket = event.socket; incomingSocket.addEventListener(ProgressEvent.SOCKET_DATA, onData);} protected function onData(event:ProgressEvent):void { if (incomingSocket.bytesAvailable > 0){ //here you can pass data to the client using writeBytes, writeUTFBytes and many other methods /*an example could be: incomingSocket.writeUTFBytes(String("HELLO!"); incomingSocket.flush();*/ } } // on the client socket application you have to create the connection and then manage (send and receive) data from the server private function createSocketConnection():void{ socket = new Socket() socket.addEventListener(Event.CONNECT, connectedToServer); socket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData); socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); socket.addEventListener(Event.CLOSE, closeSocket}); //pass to connect method the server IP and the port to comunicate socket.connect("127.0.0.1", 7934); } protected function receiveData(event:ProgressEvent):void { // here you can read all the packets sent from the server } protected function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); } private function connectedToServer(e:Event):void{ //yes! you are connected to the socket server } private function closeSocket(e:Event):void{ //your socket connection is closed }
After that you can start to experiments with this new feature as I’ve just done.
Last but not least, as you can see on the release notes, Adobe adds another great feature, that is the capability to stop all movieclips are running on the stage calling a new method “stopAllChildren()” directly from the stage instance.
Simple, easy and useful!