Skip to content

Instantly share code, notes, and snippets.

@thanksmister
Created April 15, 2011 17:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanksmister/673f0a77f701d4ae98a7 to your computer and use it in GitHub Desktop.
Save thanksmister/673f0a77f701d4ae98a7 to your computer and use it in GitHub Desktop.
AS3 P2P Helper Class to send actions and data between AIR/Flash/Mobile clients. Just add this class to two different clients and make sure they have the same group and session id set when calling the connect() method.
package
{
import flash.events.Event;
public class P2PHelperEvent extends Event
{
public static const MESSAGE_EVENT:String = "messageEvent";
public var action:String;
public var data:Object;
public function P2PHelperEvent( action:String, data:Object = null)
{
this.action = action;
this.data = data;
super( MESSAGE_EVENT );
}
}
}
package
{
import events.P2PHelperEvent;
import flash.events.EventDispatcher;
import flash.events.NetStatusEvent;
import flash.net.GroupSpecifier;
import flash.net.NetConnection;
import flash.net.NetGroup;
[Event(name="messageEvent", type="events.P2PHelperEvent")]
public class P2PHelper extends EventDispatcher
{
public static const ACTION_OPEN:String = "open";
public static const ACTION_CLOSE:String = "close";
public static const ACTION_GET:String = "get";
public static const ACTION_NEXT:String = "next";
public static const ACTION_PAUSE:String = "pause";
public static const ACTION_PLAY:String = "play";
public static const ACTION_ADD:String = "add";
public static const ACTION_DELETE:String = "delete";
public static const ACTION_RESULTS:String = "results";
public static const ACTION_CONNECTED:String = "connected";
private var nc:NetConnection;
private var group:NetGroup;
private var connected:Boolean = false;
private var groupid:String;
private var sessionid:String;
public function P2PHelper()
{
}
public function connect(groupid:String="mygroup", sessionid:String="mysession"):void
{
this.sessionid = sessionid;
this.groupid = groupid;
if(!nc) {
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.connect("rtmfp:");
}
}
public function sendMessage(action:String, data:Object = null):void
{
if(!connected) return;
var message:Object = new Object();
message.sender = group.convertPeerIDToGroupAddress(nc.nearID);
message.action = action; // add, get, delete, open, close
message.data = data; // string, array, object
message.timestamp = new Date().time; // add time stamp to make each call unique
group.post(message);
receiveMessage(message);
}
private function netStatus(event:NetStatusEvent):void
{
trace("netStatus: " + event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
setupGroup();
break;
case "NetGroup.Connect.Success" :
connected = true;
this.dispatchEvent( new P2PHelperEvent(ACTION_CONNECTED) );
break;
case "NetGroup.Posting.Notify" :
//If something gets posted to the group...
receiveMessage(event.info.message);
break;
}
}
private function setupGroup():void
{
var groupspec:GroupSpecifier = new GroupSpecifier(groupid + "/" + sessionid);
groupspec.postingEnabled = true;
groupspec.ipMulticastMemberUpdatesEnabled = true;
groupspec.addIPMulticastAddress("225.225.0.1:12345");
group = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
group.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
}
private function receiveMessage(message:Object):void
{
trace(message.action + " : " + message.data + " : " + message.timestamp);
var action:String = (message.action)? message.action:null;
var data:Object = (message.data)? message.data:null;
this.dispatchEvent( new P2PHelperEvent(action, data) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment