Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [89]

By Root 2582 0
onStatus);

The group is composed of neighbors, you as well as others. Using the same NetStatusEvent event, check for its info.code. Wait to receive the NetGroup.Connect.Success event before using the functionality of NetGroup to avoid getting an error.

When a user joins or leaves the group, the code is as follows:

function onStatus(event:NetStatusEvent):void {

switch(event.info.code) {

case " NetGroup.Connect.Success" :

trace("I joined the group");

break;

case "NetGroup.Connect.Rejected" :

case "NetGroup.Connect.Failed" :

trace("I am not a member");

break;

}

}

Others in the group receive the following events. Note that if the group is large, only a subset of members is informed that a new peer has joined or left the group:

function onStatus(event:NetStatusEvent):void {

switch(event.info.code) {

case "NetGroup.Neighbor.Connect" :

trace("neighbor has arrived", neighborCount);

break;

case "NetGroup.Neighbor.Disconnect" :

trace("neighbor has left");

break;

}

}

To send a message, use the NetGroup.post method. It takes an Object as an argument. Messages are serialized in AMF (binary format for serialized ActionScript objects), so a variation of data types can be used, such as Object, Number, Integer, and String types:

var message:Object = new Object();

message.type = "testing";

message.body = {name:"Véronique", greeting:"Bonjour"};

group.post(message);

To receive messages, check for an info.code equal to a NetGroup.Posting.Notify event. The message is received as event.info.message. The message is not distributed to the sender:

function onStatus(event:NetStatusEvent):void {

switch(event.info.code) {

case "NetGroup.Posting.Notify" :

trace(event.info.message); // [Object]

trace(event.info.message.body.greeting); // Bonjour

break;

}

}

Identical messages are not re-sent. To make each message unique, store the current time as a property of the object:

var now:Date = new Date();

message.time = now.getHours() + "_" + now.getMinutes() +

"_" + now.getSeconds();

group.post(message);

If the message only goes in one direction and there will be no overlap between clients, you could use a counter that gets incremented with every new message:

message.count = count++;

When disconnecting, it is important to remove all objects and their listeners:

function onStatus(event:NetStatusEvent):void {

switch(event.info.code) {

case "NetConnection.Connect.Rejected" :

case "Connect.AppShutdown" :

trace("I am not connected");

onDisconnect();

break;

}

}

function onDisconnect():void {

group = null;

netGroup.removeEventListener(NetStatusEvent.NET_STATUS, onStatus);

netGroup = null;

connection.removeEventListener(NetStatusEvent.NET_STATUS, onStatus);

connection = null;

}

Color Exchange


Let’s create a simple example. The hueMe application starts with a shape of a random color. Each client can send a color value to the other client’s application. On the receiving end, the shape changes to the new color (see Figure 15-1).

Figure 15-1. The hueMe application

Draw the initial colored sprite:

var sprite:Sprite = new Sprite();

var g:Graphics = sprite.graphics;

g.beginFill(Math.round(Math.random()*0xFFFFFF));

g.drawRect(20, 20, 200, 150);

g.endFill();

Create the connection for the P2P communication:

var connection:NetConnection = new NetConnection();

connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);

connection.connect("rtmfp:");

Once the connection is established, create the group and check that the user has successfully connected to it:

function onStatus(event:NetStatusEvent):void {

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

var groupSpec:GroupSpecifier = new GroupSpecifier("colorGroup");

groupSpec.addIPMulticastAddress("225.0.0.1:4000");

groupSepc.postingEnabled = true;

groupSepc.ipMulticastMemberUpdatesEnabled = true;

group = new NetGroup(connection,

groupSpec.groupspecWithAuthorizations());

group.addEventListener(NetStatusEvent.NET_STATUS, onStatus);

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

trace("I am part of the group

Return Main Page Previous Page Next Page

®Online Book Reader