Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [91]

By Root 2584 0
for an application with a many-to-many spectrum. NetStream is for a one-to-many or few-to-many spectrum.

Communication can be done in different ways:

Posting is for lots of peers sending small messages.

Multicasting is for any size group, but with a small number of the peers being senders, and for continuous/live data which is large over time.

Direct routing is for sending messages to specific peers in the group using methods such as sendToAllNeighbors, sendToNeighbor, and sendToNearest.

Object replication is for more reliable data delivery whereby information is sent in packets between clients and reassembled.

Matthew Kaufman explains this technology in depth in his MAX 2009 presentation, at http://tv.adobe.com/watch/max-2009-develop/p2p-on-the-flash-platform-with-rtmfp.

Simple Text Chat


This example is very similar to the one we created for P2P over a local network, except for a few minor, yet important, changes.

The connection is made to a remote server using the NetConnection object and RTMFP. If you have the Adobe URL and developer key, use them as demonstrated in the following code:

const SERVER:String = "rtmfp://" + YOUR_SERVER_ADDRESS;

const KEY:STRING = YOUR_DEVELOPER_KEY;

var connection:NetConnection = new NetConnection();

connection.addEventListener(NetStatusEvent.NET_STATUS, onStatus);

connection.connect(SERVER, KEY);

NOTE

You may find some examples that use different syntax for connecting as: SERVER + KEY. The new syntax was added in January 2011. It is more secure because your key is no longer appended to the connect URI, but is handled as a separate parameter.

Connecting to a traditional streaming server would still use the URI construct as "rtmfp://server/application/instance" and additional optional parameters to connect, such as a login and password.

The GroupSpecifier now needs serverChannelEnabled set to true to use the Cirrus server, and helps in peer discovery. PostingEnabled is still on to send messages. The IPMulticastAddress property is optional but can help optimize the group topology if the group is large:

function onStatus(event:NetStatusEvent):void {

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

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

groupSpec.postingEnabled = true;

groupSpec.serverChannelEnabled = true;

group = new NetGroup(connection,

groupSpec.groupspecWithAuthorizations());

group.addEventListener(NetStatusEvent.NET_STATUS, onStatus);

}

}

The exchange of messages is very similar to the local example. Note that a post method is well suited for many peers sending small messages, as in a chat application that is not time-critical:

function sendMessage():void {

var object:Object = new Object();

object.user = "Véronique";

object.message = "This is a chat message";

object.time = new Date().time;

group.post(object);

}

function onStatus(event:NetStatusEvent):void {

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

trace(event.info.message);

}

}

Multicast Streaming


This example demonstrates a video chat between one publisher and many receivers who help redistribute the stream to other receivers.

The application connects in the same way as in the previous example, but instead of a NetGroup, we create a NetStream to transfer video, audio, and messages.

Publisher


This is the code for the publisher sending the stream.

To access the camera, add the permission in your descriptor file:

Set the GroupSpecifier and the NetStream. The GroupSpecifier needs to have multicastEnabled set to true to support streaming:

import flash.net.NetStream;

var outStream:NetStream;

function onStatus(event:NetStatusEvent):void {

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

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

groupSpec.serverChannelEnabled = true;

groupSpec.multicastEnabled = true;

outStream = new NetStream(connection,

groupSpec.groupspecWithAuthorizations());

outStream.addEventListener(NetStatusEvent.NET_STATUS,

Return Main Page Previous Page Next Page

®Online Book Reader