Skip to content

Instantly share code, notes, and snippets.

@yangboz
Created July 17, 2012 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangboz/3127420 to your computer and use it in GitHub Desktop.
Save yangboz/3127420 to your computer and use it in GitHub Desktop.
Flare3dP2pExample
package
{
//--------------------------------------------------------------------------
//
// Imports
//
//--------------------------------------------------------------------------
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.GroupSpecifier;
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.text.TextField;
import flash.text.TextFieldType;
[SWF(frameRate = 60, width = 600, height = 400, backgroundColor = 0xffffff)]
/**
* Flare3dP2pExample.as class.
* @author yangboz
* @langVersion 3.0
* @playerVersion 11.2+
* @airVersion 3.2+
* Created Jul 17, 2012 11:48:18 AM
*/
public class Flare3dP2pExample extends Sprite
{
//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------
private var netConnection:NetConnection;
private var netGroup:NetGroup;
private var inputText:TextField;
private var outputText:TextField;
private var submitButton:TextField;
//----------------------------------
// CONSTANTS
//----------------------------------
//--------------------------------------------------------------------------
//
// Public properties
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// Protected properties
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
public function Flare3dP2pExample()
{
super();
//
this.inputText = new TextField();
this.inputText.border = true;
this.inputText.width = 100;
this.inputText.height = 20;
this.inputText.type = TextFieldType.INPUT;
//
this.outputText = new TextField();
this.outputText.border = true;
this.outputText.width = 200;
this.outputText.height = 400;
this.outputText.multiline = true;
//
this.submitButton = new TextField();
this.submitButton.border = true;
this.submitButton.text = "SUBMIT";
this.submitButton.width = 60;
this.submitButton.height = 20;
this.submitButton.mouseEnabled = true;
this.submitButton.alwaysShowSelection = false;
this.submitButton.mouseWheelEnabled = false;
this.submitButton.addEventListener(MouseEvent.CLICK,submitBtnClickHandler);
//set up
this.netConnection = new NetConnection();
//listen
this.netConnection.addEventListener(NetStatusEvent.NET_STATUS,netConnHandler);
//connect to lan
this.netConnection.connect("rtmfp:");
//
this.addEventListener(Event.ADDED_TO_STAGE,addToStageHandler);
}
//--------------------------------------------------------------------------
//
// Public methods
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// Protected methods
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// Private methods
//
//--------------------------------------------------------------------------
//
private function addToStageHandler(event:Event):void
{
this.addChild(this.inputText);
this.inputText.x = 250;
this.inputText.y = 20;
this.addChild(this.outputText);
this.outputText.x = 10;
this.outputText.y = 20;
this.addChild(this.submitButton);
this.submitButton.x = 500;
this.submitButton.y = 20;
}
//
private function netConnHandler(event:NetStatusEvent):void
{
trace(event.info.code);
//update UI
// outputText.text += "[EVENT]\n"+event.info.code+"\n";
this.outputText.appendText( "[EVENT]\n"+event.info.code+"\n");
//handle event
switch(event.info.code)
{
//connection succeeded so setup a group
case "NetConnection.Connect.Success":
setupNetGroup();
break;
//group setup succeeded so enable submit
case "NetGroup.Connect.Success":
// submitButton.enabled = true;
break;
//posting received so add to output
case "NetGroup.Posting.Notify":
// outputText.text += "[RECEIVED]\n"+event.info.message.txt+"\n";
this.outputText.appendText("[RECEIVED]\n"+event.info.message.txt+"\n");
break;
}
}
//
private function submitBtnClickHandler(event:MouseEvent):void
{
//update UI
// outputText.text += "[SENDING]\n"+inputText.text+"\n";
this.outputText.appendText("[SENDING]\n"+inputText.text+"\n");
// Create an object to hold the data to send
var obj:Object = new Object();
//add the data
obj.txt = inputText.text;
//add some extra data to ensure every sent object is unique
obj.id = new Date().time;
//send to everyone in group
this.netGroup.post(obj);
}
//
private function setupNetGroup():void
{
//create a group specifier object
var groupSpec:GroupSpecifier = new GroupSpecifier("Flare3dGroup");
//enable posting (to entire group)
groupSpec.postingEnabled = true;
//allow data to be exchanged on IP multiply-cast sockets
groupSpec.ipMulticastMemberUpdatesEnabled = true;
//set the IP address and port to use.
groupSpec.addIPMulticastAddress("225.225.0.1:30000");
//create a NetGroup with NetConnection using GroupSpecifier details
this.netGroup = new NetGroup(this.netConnection,groupSpec.groupspecWithAuthorizations());
//listen for result of setup.
this.netGroup.addEventListener(NetStatusEvent.NET_STATUS,netConnHandler);
}
}
}
@sacu
Copy link

sacu commented Jul 17, 2012

rtmfp://p2p.rtmfp.net/99ead580edaf280c060675f9-f614dd07a932

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment