Wouter Verweirder

My Personal Blog

Communicating Between Websockets, Local P2P, Flash Sockets & AIR

I’ve shown you how to handle websockets in adobe air, and how to set up communication between flash sockets, websockets and adobe air using a library I wrote. I’ve extended the library with support for local P2P, using Cocoon P2P.

AIRServer, Sockets, Websockets and RTMFP

It’s another option to create a multiuser game with different inputs. Right now you can use flash sockets, websockets and rtmfp (P2P) connectivity in a mixed environment to communicate with eachother.

1
2
3
4
5
6
7
8
9
10
11
server = new AIRServer();
//adding endpoints
server.addEndPoint(new SocketEndPoint(1234, new AMFSocketClientHanderFactory(new NativeObjectSerializer())));
server.addEndPoint(new SocketEndPoint(1235, new WebSocketClientHandlerFactory(new JSONSerializer())));
server.addEndPoint(new CocoonP2PEndPoint("be.aboutme.airserver.demos.Messages"));
//add event listeners
server.addEventListener(AIRServerEvent.CLIENT_ADDED, clientAddedHandler, false, , true);
server.addEventListener(AIRServerEvent.CLIENT_REMOVED, clientRemovedHandler, false, , true);
server.addEventListener(MessageReceivedEvent.MESSAGE_RECEIVED, messageReceivedHandler, false, , true);
//start the server
server.start();

Adding a cocoon endpoint, is even simpler than adding socket endpoints, all you have to do is pass an instance of the CocoonP2PEndpoint class to the addEndpoint function of the server, and specify a groupname for the P2P netgroup. You can handle the messages just like any other message on the server, it doesn’t matter if the message comes from a socket, websocket or via rtmfp.

You can download an updated version of the library project and an updated version of the demo’s with a cocoon P2P client. Big thanks to Peter Elst for CocoonP2P, it makes P2P on the local network a lot easier to implement!

You can find the latest version on github

Comments