Using AppWarp UDP API on AS3

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.

21 Using AppWarp UDP API on AS3

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.

1
2
3
4
5
6
7
8
                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.

1
2
3
4
5
6
7
8
9
10
11
12
        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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        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

twitter Using AppWarp UDP API on AS3facebook Using AppWarp UDP API on AS3google Using AppWarp UDP API on AS3linkedin Using AppWarp UDP API on AS3pinterest Using AppWarp UDP API on AS3reddit Using AppWarp UDP API on AS3stumbleupon Using AppWarp UDP API on AS3tumblr Using AppWarp UDP API on AS3

Leave a Reply

Your email address will not be published. Required fields are marked *


− four = 5

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>