Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

P2P in Desktop, Mobile, and Tablet Flex Apps

DZone's Guide to

P2P in Desktop, Mobile, and Tablet Flex Apps

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

Using the open source Flex SDK, developers can easily build desktop, mobile, and tablet applications that use Peer to Peer (P2P) communication. I’ve created a video that walks through demos and code illustrating how to use the P2P APIs in Adobe AIR applications. Check it out:



Grab the code for the demos in the video from github:


Just as the video shows, it’s incredibly easy to use the P2P APIs. Here is a quick walk through. First create a new NetConnection that is connected to “rtmfp:” like so:

localNc = new NetConnection();
localNc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
localNc.connect("rtmfp:");
In the netStatus event listener wait for the “NetConnection.Connect.Success” event and then set up the NetGroup:
private function netStatus(event:NetStatusEvent):void
{
switch(event.info.code)
{
case "NetConnection.Connect.Success":
setupGroup();
break;
}
}

private function setupGroup():void
{
var groupspec:GroupSpecifier = new GroupSpecifier(GROUP_ID);
groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.multicastEnabled = true;
groupspec.routingEnabled = true;
groupspec.postingEnabled = true;

// This is a multicast IP address. More info: http://en.wikipedia.org/wiki/Multicast_address
groupspec.addIPMulticastAddress("239.254.254.2:30304");

netGroup = new NetGroup(localNc, groupspec.groupspecWithAuthorizations());
netGroup.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
}

Now handle a “NetGroup.SendTo.Notify” event when a message is received over the P2P connection:

// in the netStatus switch block
case "NetGroup.SendTo.Notify":
var data:Object = event.info.message;
break;

And finally to send a P2P message to everyone who is listening simply do:

netGroup.sendToAllNeighbors(objectToSend);

That’s it! Super simple and super fun! :)

Thanks to Tom Krcha for all of his great blogs on how to do this. Also thanks to Mark Dong and James Li (Flash Platform Evangelists in China) for helping me build P2Pong.

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}