Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [90]

By Root 2537 0
");

}

}

Send a random color value to the group when clicking the sprite:

g.addEventListener(MouseEvent.CLICK, hueYou);

function hueYou(event:MouseEvent):void {

var randomHue:int = Math.round(Math.random()*0xFFFFFF);

var object:Object = {type:"color", hue:randomHue};

group.post(object);

}

Finally, add the functionality to receive the value from other members of the group and color the sprite:

import flash.geom.ColorTransform;

function onStatus(event:NetStatusEvent):void {

...

if (event.info.code == "NetGroup.Posting.Notify") {

if (event.info.message.type == "color") {

applyColor(Number(event.info.message.hue));

}

}

}

function applyColor(hue:int):void {

var colorTransform:ColorTransform = new ColorTransform();

colorTransform.color = hue;

sprite.transform.colorTransform = colorTransform;

}

Companion AIR Application


To make your application unidirectional, as in a remote control-style application, have one client sending messages and the other receiving messages. Only the networked clients registered for the NetGroup.Posting.Notify event receive data.

In Chapter 17, we will build a photo-sharing application between an AIR application on a device and one on the desktop. The two computers complement each other in use and in capabilities.

Mihai Corlan developed an Android remote control for a desktop MP3 player; read about it at http://corlan.org/2010/07/02/creating-multi-screen-apps-for-android-and-desktop-using-air/.

Tom Krcha created a remote controller to send accelerometer, speed, and brake information to a car racing game (see http://www.flashrealtime.com/game-remote-device-controller/).

P2P Over a Remote Network

To use networking remotely, you need an RTMFP-capable server, such as Flash Media Server.

If you do not have access to such a server, Adobe provides a beta developer key to use its Cirrus service. Sign up to instantly receive a developer key and a URL, at http://labs.adobe.com/technologies/cirrus/.

WARNING

If you are behind a firewall, your network needs to be configured to allow outgoing UDP traffic. If it is set to block such traffic, your administrator needs to configure a TURN (Traversal Using Relays around NAT) proxy.

The traditional streaming model requires clients to receive all data from a centralized server cluster. Scaling is achieved by adding more servers. Figure 15-2 shows traditional streaming/communication with the Unicast model and RTMFP in Flash Player/Cirrus.

Figure 15-2. Traditional streaming/communication with the Unicast model (left) and RTMFP in Flash Player 10.1/Cirrus 2 (right)

RTMFP, now in its second generation, supports application-level multicast. Multicasting is the process of sending messages as a single transmission from one source to the group where each peer acts as a relay to dispatch the data to the next peer. It reduces the load on the server and there is no need for a streaming server.

The Cirrus service is only for clients communicating directly. It has low latency and good security. It does not support shared objects or custom server-side programming. You could still use shared objects with Flash Media Server, but via the traditional client-server conduit.

The NetGroup uses ring topology. Its neighborCount property stores the number of peers. Each peer is assigned a peerID, which can be mapped to a group address using Group.convertPeerIDToGroupAddress(connection.nearID). An algorithm is run every few seconds to update ring positions for the group.

When the group is connected, you can obtain statistics such as Quality of Service in bytes per second from NetGroup’s info property:

function onStatus(event:NetStatusEvent):void {

if (event.info.code == NetGroup.Connect.Success") {

trace(event.info.group);

// NetGroupInfo object with Quality of Service statistics

}

}

The NetStream object is now equipped with new multicast properties. For instance, multicastWindowDuration specifies the duration in seconds of the peer-to-peer multicast reassembly window. A short value reduces latency but also quality.

NetGroup is best used

Return Main Page Previous Page Next Page

®Online Book Reader