Remotair Template
forked from Shmups with Remotair (diff: 1544)
Remotair application template, with the complete library block at the end of the code. Just copy it and paste it in your code to interact with any android phone. 1. download and install Remotair app from Android market 2. connect Remotair to get your channel 3. insert channel here into text input, click connect
ActionScript3 source code
/**
* Copyright Glav ( http://wonderfl.net/user/Glav )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/kaHF
*/
//
// Remotair application template
//
// forked from jozefchutka's Shmups with Remotair
/*
1. download and install Remotair app from Android market
2. connect Remotair to get your channel
3. insert channel here into text input, click connect
4. in Remotair click Accelerometer to turn acc on, than go back to menu
5. in Remotair click Arrows and use UP button to start the game and to shoot
6. have fun
http://remotair.yoz.sk
watch me playing on http://www.youtube.com/watch?v=51_RwaENsgo
*/
package {
import com.bit101.components.*;
import flash.display.*;
import flash.events.*;
[SWF(width="465", height="465", backgroundColor="#ffffff", frameRate="30")]
public class Main extends Sprite {
private var receiver:Receiver = new Receiver( GenericConnector.HANDSHAKE_URL, GenericConnector.DEVELOPER_KEY, GenericConnector.CHANNEL_SERVICE);
private var plop:Label;
function Main() {
var box:HBox = new HBox(this, 100, 180);
new Label(box, 0, 0, "channel:");
var channel:InputText = new InputText(box, 0, 0, "");
new PushButton(box, 0, 0, "connect", function (... args):void {
receiver.connectChannel(channel.text);
receiver.addEventListener(GenericConnectorEvent.PEER_CONNECTED, onConnect );
receiver.accelerometerEvents.addEventListener(AccelerometerEvent.UPDATE, onAccelerometerUpdate);
});
plop = new Label(this, 10, 230, "Connected !");
}
private function onConnect(event:GenericConnectorEvent):void {
plop = new Label(this, 10, 230, "Connected !");
}
private function onAccelerometerUpdate(event:AccelerometerEvent):void {
plop.text = event.accelerationX + " - " + event.accelerationY + " - " + event.accelerationZ;
}
}
}
//<remotair library block>
import flash.display.*;import com.adobe.serialization.json.JSON;import flash.events.*;import flash.net.*;
import flash.utils.*;class Serializer{public function Serializer(){}public static function unserialize(
className:String,rawData:Object):Object{with(rawData){switch(className){case "flash.events::MouseEvent":
return new MouseEvent(type,false,true,localX,localY);case "flash.events::KeyboardEvent":return new
KeyboardEvent(type,false,true,charCode,keyCode);case "flash.events::TextEvent":return new TextEvent(
type,false,true,text);case"flash.events::ActivityEvent":return new ActivityEvent(type,false,true,activating);
case"flash.events::AccelerometerEvent":return new AccelerometerEvent(type,false,true,timestamp,accelerationX,
accelerationY,accelerationZ);case "flash.events::TouchEvent":return new TouchEvent(type,false,true,
touchPointID,isPrimaryTouchPoint,localX,localY,sizeX,sizeY,pressure);case "flash.events::GestureEvent":
return new GestureEvent(type,false,true,phase,localX,localY);case "flash.events::PressAndTapGestureEvent":
return new PressAndTapGestureEvent(type,false,true,phase,localX,localY,tapLocalX,tapLocalY);case
"flash.events::TransformGestureEvent":return new TransformGestureEvent(type,false,true,phase,localX,localY,
scaleX,scaleY,rotation,offsetX,offsetY);case "flash.events::GeolocationEvent":return new GeolocationEvent
(type,false,true,latitude,longitude,altitude,horizontalAccuracy,verticalAccuracy,speed,heading,timestamp);
case "remotair.events::ViewEvent":return new ViewEvent(type,viewClass);case "sk.yoz.remotair.events::JoystickEvent":
return new JoystickEvent(type,joystickID,x,y);case "sk.yoz.remotair.events::CameraEvent":return new CameraEvent
(type,cameraName,cameraWidth,cameraHeight,cameraFPS,cameraQuality);case "sk.yoz.remotair.events::MicrophoneEvent":
return new MicrophoneEvent(type,microphoneName,microphoneCodec,microphoneGain,microphoneRate,microphoneEncodeQuality);
case "sk.yoz.remotair.events::RemotairEvent":return new RemotairEvent(type,data);default:return data;}}}
public static function serialize(rawData:Object):Object{var className:String=getQualifiedClassName(rawData);
with(rawData)switch(className){case "flash.events::MouseEvent":return{type:type,localX:localX,localY:localY};
case "flash.events::KeyboardEvent":return{type:type,charCode:charCode,keyCode:keyCode};case "flash.events::TextEvent":
return{type:type,text:text};case "flash.events::ActivityEvent":return{type:type,activating:activating};case
"flash.events::AccelerometerEvent":return{type:type,timestamp:timestamp,accelerationX:accelerationX,
accelerationY:accelerationY,accelerationZ:accelerationZ};case "flash.events::TouchEvent":return{type:type,
touchPointID:touchPointID,isPrimaryTouchPoint:isPrimaryTouchPoint,localX:localX,localY:localY,sizeX:sizeX,
sizeY:sizeY,pressure:pressure};case "flash.events::GestureEvent":return{type:type,phase:phase,localX:localX,
localY:localY};case "flash.events::PressAndTapGestureEvent":return{type:type,phase:phase,localX:localX,
localY:localY,tapLocalX:tapLocalX,tapLocalY:tapLocalY};case "flash.events::TransformGestureEvent":return{
type:type,phase:phase,localX:localX,localY:localY,scaleX:scaleX,scaleY:scaleY,rotation:rotation,offsetX:offsetX,
offsetY:offsetY};case "flash.events::GeolocationEvent":return{type:type,latitude:latitude,longitude:longitude,
altitude:altitude,horizontalAccuracy:horizontalAccuracy,verticalAccuracy:verticalAccuracy,speed:speed,heading:heading,
timestamp:timestamp};case "remotair.events::ViewEvent":return{type:type,viewClass:getQualifiedClassName(
viewClass)};case "sk.yoz.remotair.events::JoystickEvent":return{type:type,joystickID:joystickID,x:x,y:y};
case "sk.yoz.remotair.events::CameraEvent":return{type:type,cameraName:cameraName,cameraWidth:cameraWidth,
cameraHeight:cameraHeight,cameraFPS:cameraFPS,cameraQuality:cameraQuality};case "sk.yoz.remotair.events::MicrophoneEvent":
return{type:type,microphoneName:microphoneName,microphoneCodec:microphoneCodec,microphoneGain:microphoneGain,
microphoneRate:microphoneRate,microphoneEncodeQuality:microphoneEncodeQuality};case "sk.yoz.remotair.events::RemotairEvent":
return{type:type,data:data};default:return data;}}}class CameraEvent extends Event{public static const
CAMERA_PUBLISHED:String="CameraEventCAMERA_PUBLISHED";public static const CAMERA_UPDATED:String=
"CameraEventCAMERA_UPDATED";public static const CAMERA_UNPUBLISHED:String="CameraEventCAMERA_UNPUBLISHED";
private var _cameraName:String;private var _cameraWidth:uint;private var _cameraHeight:uint;private var _cameraFPS:
Number;private var _cameraQuality:int;public function CameraEvent(type:String,cameraName:String=null,cameraWidth:
uint=0,cameraHeight:uint=0,cameraFPS:Number=NaN,cameraQuality:int=0){super(type,false,true);_cameraName=cameraName;
_cameraWidth=cameraWidth;_cameraHeight=cameraHeight;_cameraFPS=cameraFPS;_cameraQuality=cameraQuality;}
public function get cameraName():String{return _cameraName;}public function get cameraWidth():uint{return _cameraWidth;}
public function get cameraHeight():uint{return _cameraHeight;}public function get cameraFPS():Number{
return _cameraFPS;}public function get cameraQuality():int{return _cameraQuality;}override public function
toString():String{return '[CameraEvent type="'+type+'"'+' cameraName='+cameraName+' cameraWidth='+cameraWidth+
' cameraHeight='+cameraHeight+' cameraFPS='+cameraFPS+' cameraQuality='+cameraQuality+' bubbles='+bubbles+
' cancelable='+cancelable+' eventPhase='+eventPhase+']';}}class GenericConnectorEvent extends Event{
public static const NET_CONNECTED:String="GenericConnectorEventNET_CONNECTED";public static const NET_DISCONNECTED:String
="GenericConnectorEventNET_DISCONNECTED";public static const PEER_CONNECTED:String="GenericConnectorEventPEER_CONNECTED";
public static const PEER_DISCONNECTED:String="GenericConnectorEventPEER_DISCONNECTED";public function
GenericConnectorEvent(type:String){super(type,false,false);}}class JoystickEvent extends Event{public static
const CHANGE:String="JoystickEventCHANGE";private var _joystickID:uint;private var _x:Number;private var _y:Number;
public function JoystickEvent(type:String,joystickID:uint,x:Number,y:Number){super(type,false,true);_joystickID
=joystickID;_x=x;_y=y;}public function get joystickID():uint{return _joystickID;}public function get x():Number
{return _x;}public function get y():Number{return _y;}override public function toString():String{
return '[JoystickEvent type="'+type+'"'+' joystickID='+joystickID+' x='+x+' y='+y+' bubbles='+bubbles+' cancelable='
+cancelable+' eventPhase='+eventPhase+']';}}class MicrophoneEvent extends Event{public static const
MICROPHONE_PUBLISHED:String="MicrophoneEventMICROPHONE_PUBLISHED";public static const MICROPHONE_UPDATED:
String="MicrophoneEventMICROPHONE_UPDATED";public static const MICROPHONE_UNPUBLISHED:String=
"MicrophoneEventMICROPHONE_UNPUBLISHED";private var _microphoneName:String;private var _microphoneCodec:
String;private var _microphoneGain:Number;private var _microphoneRate:int;private var _microphoneEncodeQuality:
int;public function MicrophoneEvent(type:String,microphoneName:String=null,microphoneCodec:String=null,
microphoneGain:Number=NaN,microphoneRate:int=0,microphoneEncodeQuality:int=0){super(type,false,true);
_microphoneName=microphoneName;_microphoneCodec=microphoneCodec;_microphoneGain=microphoneGain;_microphoneRate=
microphoneRate;_microphoneEncodeQuality=microphoneEncodeQuality;}public function get microphoneName():String{
return _microphoneName;}public function get microphoneCodec():String{return _microphoneCodec;}public function
get microphoneGain():Number{return _microphoneGain;}public function get microphoneRate():int{return _microphoneRate;
}public function get microphoneEncodeQuality():int{return _microphoneEncodeQuality;}override public function
toString():String{return '[MicrophoneEvent type="'+type+'"'+' microphoneName='+microphoneName+' microphoneCodec='
+microphoneCodec+' microphoneGain='+microphoneGain+' microphoneRate='+microphoneRate+' microphoneEncodeQuality='
+microphoneEncodeQuality+' bubbles='+bubbles+' cancelable='+cancelable+' eventPhase='+eventPhase+']';}}class
ReceiverEvent extends Event{public static const PACKET_RECEIVED:String="ReceiverEventPACKET_RECEIVED";
public static const EVENT_RECEIVED:String="ReceiverEventEVENT_RECEIVED";private var _packet:UNIPacket;public function
ReceiverEvent(type:String,packet:UNIPacket=null){super(type,false,false);_packet=packet;}public function get
packet():UNIPacket{return _packet;}override public function toString():String{return '[ReceiverEvent type="'+
type+' packet='+packet+' bubbles='+bubbles +' cancelable='+cancelable+' eventPhase='+eventPhase+']';}}class
RemotairEvent extends Event{public static const TEXT_CHANGE:String="RemotairEventTEXT_CHANGE";
public static const VIEW_CHANGE:String="RemotairEventVIEW_CHANGE";public static const MOUSE_LEFT_DOWN:String=
"RemotairEventMOUSE_LEFT_DOWN";public static const MOUSE_LEFT_UP:String="RemotairEventMOUSE_LEFT_UP";
public static const MOUSE_LEFT_CLICK:String="RemotairEventMOUSE_LEFT_CLICK";public static const
MOUSE_LEFT_ROLL_OUT:String="RemotairEventMOUSE_LEFT_ROLL_OUT";public static const MOUSE_RIGHT_DOWN:String=
"RemotairEventMOUSE_RIGHT_DOWN";public static const MOUSE_RIGHT_UP:String="RemotairEventMOUSE_RIGHT_UP";
public static const MOUSE_RIGHT_CLICK:String="RemotairEventMOUSE_RIGHT_CLICK";public static const MOUSE_RIGHT_ROLL_OUT:
String="RemotairEventMOUSE_RIGHT_ROLL_OUT";public static const STAGE_SIZE:String="RemotairEventSTAGE_SIZE";
private var _data:Object;public function RemotairEvent(type:String,data:Object=null){super(type,false,false);
_data=data;}public function get data():Object{return _data;}override public function toString():String{
return '[ReceiverEvent type="'+type+' data='+data+' bubbles='+bubbles+' cancelable='+cancelable+' eventPhase='+
eventPhase+']';}}class ViewEvent extends Event{public static const CHANGE:String="ViewEventCHANGE";
private var _viewClass:String;public function ViewEvent(type:String,viewClass:String){super(type,false,
false);_viewClass= viewClass;}public function get viewClass():String{return _viewClass;}override public
function toString():String{ return '[ViewEvent type="'+type+'"'+' viewClass='+viewClass+' bubbles='+
bubbles+' cancelable='+cancelable+' eventPhase='+eventPhase+']';}}class GenericConnector extends
EventDispatcher{public static const HANDSHAKE_URL:String="rtmfp://p2p.rtmfp.net";public static const
DEVELOPER_KEY:String="a2299ef15861a53c65699063-7c3e48fa4695";public static const CHANNEL_SERVICE:String=
"http://remotair.yoz.sk";protected static const PACKET_METHOD:String="onPacket";protected static const
PUBLISH_NAME:String="remotair";private var connection:NetConnection;private var _stream:NetStream;
private var _channel:String;private var _inited:Boolean;private var _netConnected:Boolean;
private var _peerID:String;private var handshakeURL:String;private var developerKey:String;private var
channelService:String;public function GenericConnector(handshakeURL:String,developerKey:String,channelService:String)
{super();this.handshakeURL=handshakeURL;this.developerKey=developerKey;this.channelService=channelService;}
protected function get inited():Boolean{return _inited;}protected function set netConnected(value:Boolean):
void{_netConnected=value;var type:String=value?GenericConnectorEvent.NET_CONNECTED:GenericConnectorEvent.NET_DISCONNECTED;
dispatchEvent(new GenericConnectorEvent(type));if(!value)protected::peerID=null;}public function get netConnected()
:Boolean{return _netConnected;}protected function set peerID(value:String):void{_peerID=value;var type:String=
value?GenericConnectorEvent.PEER_CONNECTED:GenericConnectorEvent.PEER_DISCONNECTED;dispatchEvent(new
GenericConnectorEvent(type));}public function get peerID():String{return _peerID;}public function get
peerConnected():Boolean{return public::peerID?true:false;}public function get nearID():String{
return(connection && connection.connected)?connection.nearID:null;}public function get stream():NetStream{
return _stream;}private function set stream(value:NetStream):void{_stream=value;}public function get channel():
String{return _channel;}protected function set channel(value:String):void{_channel=value;}protected function
init():void{_inited=true;connection=new NetConnection();connection.maxPeerConnections=2;connection.addEventListener
(NetStatusEvent.NET_STATUS,onNetStatus);connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError);
connection.addEventListener(IOErrorEvent.IO_ERROR,onIOError);connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
onSecurityError);}protected function destroy():void{_inited=false;if(public::netConnected)protected::netConnected=
false;if(public::peerConnected)protected::peerID=null;protected::channel=null;try{connection.removeEventListener(
NetStatusEvent.NET_STATUS,onNetStatus);connection.removeEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError);
connection.removeEventListener(IOErrorEvent.IO_ERROR,onIOError);connection.removeEventListener(
SecurityErrorEvent.SECURITY_ERROR,onSecurityError);connection.close();connection=null;}catch(error:Error){}var
stream:NetStream=public::stream;try{stream.removeEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
stream.removeEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError);stream.removeEventListener(
IOErrorEvent.IO_ERROR,onIOError);stream.close();stream=null;}catch(error:Error){}}public function connect():void{
connection.connect(handshakeURL+"/"+developerKey);}public function disconnect():void{connection.close();}
protected function publish(peerID:String,client:Object):void{var stream:NetStream=new NetStream(connection,
peerID);stream.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);stream.addEventListener(
AsyncErrorEvent.ASYNC_ERROR,onAsyncError);stream.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
stream.client=client;stream.publish(PUBLISH_NAME);private::stream=stream;}protected function play(peerID:
String,client:Object):void{var stream:NetStream=new NetStream(connection,peerID);stream.addEventListener(
NetStatusEvent.NET_STATUS,onNetStatus);stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError);
stream.addEventListener(IOErrorEvent.IO_ERROR,onIOError);stream.client=client;stream.play(PUBLISH_NAME);
private::stream=stream;}protected function createChannel(connection:String,onComplete:Function):void{var
url:String=channelService+"/channel/create/"+connection;var loader:URLLoader=new URLLoader();var request:
URLRequest=new URLRequest(url);loader.load(request);loader.addEventListener(Event.COMPLETE,onComplete);}
protected function getChannelConnection(channel:String,onComplete:Function):void{var url:String=channelService+
"/channel/get/"+channel;var loader:URLLoader=new URLLoader();var request:URLRequest=new URLRequest(url);
loader.load(request);loader.addEventListener(Event.COMPLETE,onComplete);}private function onNetStatus(event:
NetStatusEvent):void{var code:String=event.info.code;var fName:String="f"+code.replace(/\./g,"");trace(code);
try{var fFunction:Function=this[fName] as Function;fFunction(event);}catch(error:Error){trace("missing",fName);
}}protected function fNetConnectionConnectSuccess(event:NetStatusEvent):void{protected::netConnected=true;}
protected function fNetConnectionConnectFailed(event:NetStatusEvent):void{protected::netConnected=false;}
protected function fNetConnectionConnectClosed(event:NetStatusEvent):void{protected::netConnected=false;}
protected function fNetStreamPublishStart(event:NetStatusEvent):void{}protected function fNetStreamPlayStart
(event:NetStatusEvent):void{}protected function fNetStreamPlayReset(event:NetStatusEvent):void{}
protected function fNetStreamPlayFailed(event:NetStatusEvent):void{}protected function
fNetStreamConnectClosed(event:NetStatusEvent):void{}protected function fNetConnectionConnectNetworkChange(
event:NetStatusEvent):void{}protected function onAsyncError(event:AsyncErrorEvent):void{trace(
"asyncErrorHandler",event.text);}protected function onIOError(event:IOErrorEvent):void{trace(
"ioErrorHandler",event.text);}protected function onSecurityError(event:SecurityErrorEvent):void{trace(
"securityErrorHandler",event.text);}}class UNIPacket{public var header:UNIPacketHeader;public var data:*;AVM1Movie;
public function UNIPacket(header:UNIPacketHeader=null,data:*=null){this.header=header;this.data=data;}
public static function create(data:Object):UNIPacket{return new UNIPacket(UNIPacketHeader.create(
data.header),data.data);}}dynamic class UNIPacketHeader{public var id:uint;public var hash:String;public var
sender:String;public function UNIPacketHeader(){}public static function create(data:Object):UNIPacketHeader{
var header:UNIPacketHeader=new UNIPacketHeader();for(var key:String in data)header[key]=data[key];return header;
}}class Receiver extends GenericConnector{public var mouseEvents:EventDispatcher=new EventDispatcher;public var
keyboardEvents:EventDispatcher=new EventDispatcher;public var textEvents:EventDispatcher=new EventDispatcher;
public var joystickEvents:EventDispatcher=new EventDispatcher;public var cameraEvents:EventDispatcher=new
EventDispatcher;public var microphoneEvents:EventDispatcher=new EventDispatcher;public var activityEvents:EventDispatcher
=new EventDispatcher;public var accelerometerEvents:EventDispatcher=new EventDispatcher;public var
statusEvents:EventDispatcher=new EventDispatcher;public var touchEvents:EventDispatcher=new EventDispatcher;
public var gestureEvents:EventDispatcher=new EventDispatcher;public var geolocationEvents:EventDispatcher=
new EventDispatcher;public var viewEvents:EventDispatcher=new EventDispatcher;public var remotairEvents:EventDispatcher=
new EventDispatcher;public function Receiver(handshakeURL:String,developerKey:String,channelService:String)
{super(handshakeURL,developerKey,channelService);}public function connectChannel(channel:String):void{
protected::channel=channel;!inited && init();connect();}protected function onChannelConnectionCompleted(
event:Event):void{var loader:URLLoader=URLLoader(event.currentTarget);var data:Object=JSON.decode(
loader.data.toString());var client:Object={};client[PACKET_METHOD]=onPacketMethod;play(data.connection,client);
}protected function onPacketMethod(data:Object):void{var packet:UNIPacket=UNIPacket.create(data);var
className:String=packet.header.className;var type:String=ReceiverEvent.PACKET_RECEIVED;dispatchEvent(new ReceiverEvent(
type,packet));try{packet.data=unserialize(className,packet.data);dispatch(packet);}catch(error:Error){}}
protected function unserialize(className:String,data:Object):Object{return Serializer.unserialize(className,data);}
override protected function fNetConnectionConnectSuccess(event:NetStatusEvent):void{super.fNetConnectionConnectSuccess(event);
getChannelConnection(public::channel,onChannelConnectionCompleted);}override protected function
fNetConnectionConnectFailed(event:NetStatusEvent):void{super.fNetConnectionConnectFailed(event);destroy();}
override protected function fNetConnectionConnectClosed(event:NetStatusEvent):void{super.fNetConnectionConnectClosed(
event);destroy();}override protected function fNetStreamPlayStart(event:NetStatusEvent):void{super.fNetStreamPlayStart(
event);protected::peerID="unknown";}override protected function fNetStreamConnectClosed(event:NetStatusEvent):
void{super.fNetStreamConnectClosed(event);protected::peerID=null;}protected function dispatch(packet:UNIPacket):
Boolean{var type:String=ReceiverEvent.EVENT_RECEIVED;if(packet.data is Event)dispatchEvent(new ReceiverEvent(type,
packet));if(packet.data is RemotairEvent)return remotairEvents.dispatchEvent(packet.data);if(packet.data is ViewEvent)
return viewEvents.dispatchEvent(packet.data);if(packet.data is MouseEvent)return mouseEvents.dispatchEvent(
packet.data);if(packet.data is KeyboardEvent)return keyboardEvents.dispatchEvent(packet.data);if(
packet.data is TextEvent)return textEvents.dispatchEvent(packet.data);if(packet.data is JoystickEvent)
return joystickEvents.dispatchEvent(packet.data);if(packet.data is CameraEvent)
return cameraEvents.dispatchEvent(packet.data);if(packet.data is MicrophoneEvent)
return microphoneEvents.dispatchEvent(packet.data);if(packet.data is ActivityEvent)
return activityEvents.dispatchEvent(packet.data);if(packet.data is StatusEvent)
return statusEvents.dispatchEvent(packet.data);if(packet.data is AccelerometerEvent)
return accelerometerEvents.dispatchEvent(packet.data);if(packet.data is TouchEvent)
return touchEvents.dispatchEvent(packet.data);if(packet.data is PressAndTapGestureEvent)
return gestureEvents.dispatchEvent(packet.data);if(packet.data is TransformGestureEvent)
return gestureEvents.dispatchEvent(packet.data);if(packet.data is GestureEvent)
return gestureEvents.dispatchEvent(packet.data);if(packet.data is GeolocationEvent)
return geolocationEvents.dispatchEvent(packet.data);return false;}}
//</remotair library block>