In our latest AppWarp SDK update, we have made UDP available for AS3. UDP is great for action based games in which the message exchange frequency is expected to be high and the inherent unreliability of the protocol is acceptable. With UDP, the OS doesn’t have the overhead of marshaling, buffering, tracking and acknowledging messages. Thus, UDP messages typically arrive at the destination application with a shorter delay than do TCP messages.
This feature is available on Android, iOS and desktop AIR profiles. The SDK solution is designed in such a way that if your app is running in an environment where UDP will not work, the server will fallback to TCP and ensure that your app still receives the messages. We have used our Smiley Space shooter sample and modified it to use our UDP update API instead. Complete sample code is available on our git repo.
Some code highlights
1. Initialize AppWarp to use UDP
This must be done once after the client is successfully connected with AppWarp cloud.
try{
WarpClient.getInstance().initUDP();
_owner.udpEnabled = true;
}
catch(e:Error){
// happens when running in flash player (browser)
_owner.udpEnabled = false;
}
2. Send the update using the appropirate variant of send update API.
private function sendMessage(tag:String, x:int, y:int):void
{
var msg:String = localUsername+","+tag+","+x+","+y;
var msgBytes:ByteArray = new ByteArray();
msgBytes.writeUTF(msg);
if(udpEnabled){
WarpClient.getInstance().sendUdpUpdate(msgBytes);
}
else{
WarpClient.getInstance().sendUpdate(msgBytes);
}
}
3. Handle the received byte array update to more the remote projectile/player
public function onUpdatePeersReceived(update:ByteArray, fromUDP:Boolean):void
{
var msg:String = update.readUTF();
var msgArray:Array = msg.split(",");
var sender:String = msgArray[0];
var tag:String = msgArray[1];
var x:int = parseInt(msgArray[2]);
var y:int = parseInt(msgArray[3]);
if(sender == _owner.localUsername){
return;
}
if(tag == "player"){
_owner.moveRemotePlayer(x, y);
}
else if(tag == "projectile"){
_owner.moveRemoteProjectile(x, y);
}
}
Write to us on support@shephertz.com for questions and feedback
Leave A Reply