P2P Flash On A Local Network Part 3

Following on from part one and two, we now deal with sending messages to specific peers in a large group (where large means 15+ members).

To get around the limitation of neighbours (see the end of part two) we need to create a system of our own to let each peer know about all the other peers in the group. Then we can use the techniques of part two to send messages to any peer.

Here it is in action (open some copies of this page to get the member count to 16 or above if you wish):

{P2PLanExample3}






Update: This example does not work in Chrome browser with PPAPI version of Flash Player. To use, goto chrome://plugins and enable NPAPI version.

How It Works

There are no methods, classes or events in the code that aren’t covered in part 1 or 2 so i won’t explain the actual code used (the commented source code is available at the bottom of the post). Rather, i will explain the flow of messaging that allows every peer to know the id of every other.

When a new peer joins the group it sends out a group post (ie: to all peers in the group) containing the peer id of the new member. All peers store the received id, so every peer now knows about the new guy. Next, each peer sends a directed message back to the new peer containing their peer id – so now the new member knows about every other peer. Bingo! Now we can send a text message to anyone in the group using the techniques of part 2.

When a peer leaves the group, it would be nice if it could send out a group posting letting all the peers know to remove it from their list, but unfortunately this won’t work (for me at least) if the swf is running in a browser and the browser window/tab is closed (in short, there’s no way to send out the “i am disconnecting” group post message before the NetConnection has been closed). Luckily, the solution is straight-forward.

When a peer disconnects, its neighbours receive a NetGroup.Neighbor.Disconnect event. So we just get the neighbour to send out a “he is disconnecting” group post message instead. Easy peasy!

Of course, if you’re using AIR you can listen for the application exiting event and send the “i am disconnecting” group post directly, but this way works for a swf environment too.

Notes On The Code

This code does result in some duplication of messaging – for instance, every neighbour of a disconnecting peer will send out the same group post which could result in up to 13 identical post messages where only 1 is needed. Improving efficiency i leave as an exercise for the reader (a few pleadings in the comments might persuade me to get off my lazy backside).

We listen for the NetGroup.Neighbor.Connect event rather than the NetGroup.Connect.Success before dispatching the “i have connected” message (setting a flag to stop later NetGroup.Neighbor.Connect events triggering repeated “i have connected” messages). There’s no point sending a message if there are no other peers in the group!

The code uses messaging objects with either a cmd property (for behind-the-scenes messages, ie: managing the peer lists as explained above) or a txt property for messages that the user is sending.

In response to Rackdolls wise comment in part 2, i have used a belt and braces approach to unique ids for the messaging object this time – in addition to the date in milliseconds used before, each object now also contains the senders peer id and a random number.

Further Reading

If you want to see exactly how p2p is working under the hood there are some good sessions from MAX on adobe tv:

http://tv.adobe.com/watch/max-2009-develop/p2p-on-the-flash-platform-with-rtmfp/

http://tv.adobe.com/watch/max-2011-develop/advanced-p2p-with-rtmfp-tips-and-tricks/

For a ton of tutorials and info try:

http://www.flashrealtime.com/

And lastly, here’s the source code for this post: https://github.com/LeeBurrows/blog-source-code

One thought on “P2P Flash On A Local Network Part 3

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>