Permalink
Browse files

Added ClickCommand Example on how to create your own HydraCommand

  • Loading branch information...
1 parent 6bffed6 commit 1b31587f636e732e53821075bdddd0c5ef2e63c9 @devboy devboy committed Sep 7, 2010
@@ -33,7 +33,7 @@ package org.devboy.hydra.examples.chat
private function init() : void
{
- var stratusServiceUrl : String = "rtmfp://stratus.rtmfp.net/YOUR-API_KEY";
+ var stratusServiceUrl : String = "rtmfp://stratus.rtmfp.net/YOUR-API-KEY";
_hydraService = new HydraService("HydraChatExample", stratusServiceUrl);
_hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_SUCCESS, serviceEvent);
_hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_FAILED, serviceEvent);
@@ -0,0 +1,40 @@
+package org.devboy.hydra.examples.clickcommand
+{
+ import org.devboy.hydra.commands.HydraCommand;
+
+ /**
+ * @author Dominic Graefen - devboy.org
+ */
+ public class ClickCommand extends HydraCommand
+ {
+ public static const TYPE : String = "org.devboy.hydra.examples.clickcommand.ClickCommand.TYPE";
+
+ private var _x : Number;
+ private var _y : Number;
+
+ public function ClickCommand( x : Number, y : Number )
+ {
+ _y = y;
+ _x = x;
+ super(TYPE);
+ }
+
+ override public function get info() : Object
+ {
+ var info : Object = new Object();
+ info.x = _x;
+ info.y = _y;
+ return info;
+ }
+
+ public function get x() : Number
+ {
+ return _x;
+ }
+
+ public function get y() : Number
+ {
+ return _y;
+ }
+ }
+}
@@ -0,0 +1,31 @@
+package org.devboy.hydra.examples.clickcommand
+{
+ import org.devboy.hydra.commands.IHydraCommand;
+ import org.devboy.hydra.commands.IHydraCommandCreator;
+
+ /**
+ * @author Dominic Graefen - devboy.org
+ */
+ public class ClickCommandCreator implements IHydraCommandCreator
+ {
+ public function createCommand(type : String, timestamp : uint, userId : String, senderPeerId : String, info : Object) : IHydraCommand
+ {
+ if( type != commandType )
+ throw new Error("Command type mismatch");
+
+ var x : Number = info.x;
+ var y : Number = info.y;
+
+ var clickCommand : ClickCommand = new ClickCommand(x, y);
+ clickCommand.timestamp = timestamp;
+ clickCommand.userId = userId;
+ clickCommand.senderPeerId = senderPeerId;
+ return clickCommand;
+ }
+
+ public function get commandType() : String
+ {
+ return ClickCommand.TYPE;
+ }
+ }
+}
@@ -0,0 +1,101 @@
+package org.devboy.hydra.examples.clickcommand
+{
+ import org.devboy.hydra.HydraChannel;
+ import org.devboy.hydra.HydraEvent;
+ import org.devboy.hydra.HydraService;
+ import org.devboy.hydra.commands.HydraCommandEvent;
+
+ import flash.display.DisplayObject;
+ import flash.display.Shape;
+ import flash.display.Sprite;
+ import flash.events.Event;
+ import flash.events.MouseEvent;
+ import flash.net.GroupSpecifier;
+
+ /**
+ * @author Dominic Graefen - devboy.org
+ */
+ public class ClickCommandExample extends Sprite
+ {
+ private var _hydraService : HydraService;
+ private var _clickChannel : HydraChannel;
+ private var _shapeContainer : Sprite;
+ public function ClickCommandExample()
+ {
+ init();
+ _shapeContainer = new Sprite();
+ _shapeContainer.graphics.beginFill(0, 0);
+ _shapeContainer.graphics.drawRect(0, 0, 1000, 1000);
+ _shapeContainer.graphics.endFill();
+ addChild(_shapeContainer);
+ addEventListener(Event.ENTER_FRAME, enterFrame);
+ addEventListener(MouseEvent.CLICK, mouseClick );
+ }
+
+ private function init() : void
+ {
+ var stratusServiceUrl : String = "rtmfp://stratus.rtmfp.net/YOUR-API-KEY";
+ _hydraService = new HydraService("HydraClickExample", stratusServiceUrl);
+ _hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_SUCCESS, serviceEvent);
+ _hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_FAILED, serviceEvent);
+ _hydraService.addEventListener(HydraEvent.SERVICE_CONNECT_REJECTED, serviceEvent);
+ var groupSpecifier : GroupSpecifier = new GroupSpecifier("HydraClickExample/ClickChannel");
+ groupSpecifier.postingEnabled = true;
+ groupSpecifier.serverChannelEnabled = true;
+ _clickChannel = new HydraChannel(_hydraService, "HydraClickExample/ClickChannel", groupSpecifier, false );
+ _clickChannel.addEventListener(HydraCommandEvent.COMMAND_RECEIVED, handleCommand);
+ _hydraService.commandFactory.addCommandCreator(new ClickCommandCreator());
+ _hydraService.connect(new Date().time.toFixed(0)+"/"+(Math.random()*100000).toFixed());
+ }
+
+ private function enterFrame(event : Event) : void
+ {
+ var invisible : Vector.<DisplayObject> = new Vector.<DisplayObject>();
+ var displayObject : DisplayObject;
+ var i : int = 0;
+ var l : int = _shapeContainer.numChildren;
+ for( ;i<l;i++ )
+ {
+ displayObject = _shapeContainer.getChildAt(i);
+ displayObject.alpha -= 0.2;
+ displayObject.scaleX -= 0.2;
+ displayObject.scaleY -= 0.2;
+ if( displayObject.alpha < 0 )
+ invisible.push(displayObject);
+ }
+ for each(displayObject in invisible)
+ _shapeContainer.removeChild(displayObject);
+ }
+
+ private function mouseClick(event : MouseEvent) : void
+ {
+ _clickChannel.sendCommand( new ClickCommand(mouseX, mouseY) );
+ }
+
+ private function handleCommand(event : HydraCommandEvent) : void
+ {
+ switch( event.command.type )
+ {
+ case ClickCommand.TYPE:
+ handleRemoteClick( event.command as ClickCommand );
+ break;
+ }
+ }
+
+ private function handleRemoteClick(clickCommand : ClickCommand) : void
+ {
+ var shape : Shape = new Shape();
+ shape.graphics.beginFill(Math.random()*0xFFFFFF);
+ shape.graphics.drawCircle(0, 0, 20);
+ shape.graphics.endFill();
+ shape.x = clickCommand.x;
+ shape.y = clickCommand.y;
+ _shapeContainer.addChild(shape);
+ }
+
+ private function serviceEvent(event : HydraEvent) : void
+ {
+ trace(event);
+ }
+ }
+}

0 comments on commit 1b31587

Please sign in to comment.