Tom Krcha's FlashRealtime

Hey amigo! These are my notes. I'm Platform Evangelist with Adobe.


Simple chat with P2P NetGroup in FP 10.1

February 9th, 2010

afcs_logoThis tutorial explains total basics of using P2P/RTMFP Groups in Flash Player 10.1. We are going to build simple multi-user chat using RTMFP Posting - all data will be transferred over P2P! For this purpose we use recently updated (22 Jan 2010) Adobe Stratus - rendezvous service.

Updated: 4:37 PM Feb 9 2010

Final demo: Try (run in few browsers) | Download source

Prerequsities:
- Flash Player 10.1
- UDP enabled network (you can’t be behind firewall, which blocks UDP)
- Flash Builder 4
- Stratus developer key - Get one here if don’t have already. You have to login using your Adobe credentials to get it.
- playerglobal.swc for Flash Player 10.1

Step 1: Create new Flex 4 project
File -> New -> Flex Project
Link new playerglobal.swc (FP10.1 API) to your project in Project Properties and set compiler version in Flex Compiler to 10.1.0

p2pgroups-compiler

Step 2: Connect to Stratus
First of all we need to setup NetConnection to Adobe Stratus. That’s easy part.

private const SERVER:String = "rtmfp://stratus.adobe.com/";
private const DEVKEY:String = "YOUR-STRATUS-DEVELOPER-KEY";
private var nc:NetConnection;
 
private function connect():void{
	nc = new NetConnection();
	nc.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
	nc.connect(SERVER+DEVKEY);	
}

Step 3: Setup NetGroup
We need to setup P2P group and connect to it. GroupSpecifier is a class, which let’s you to define all the parameters of the group. First you give it a name - in this case “myGroup/g1″. Then you set it to use serverChannel, to let it communicate with Stratus. Finally we enable Posting. That’s all you have to do to define the P2P Group.
Then we have to define the actual NetGroup. groupspecWithAuthorizations() returns String - it’s a group identifier. Now you probably ask, what’s the difference between groupspecWithoutAuthorizations() and groupspecWithAuthorizations(). If you set a posting or multicast password, the one “with” can post or multicast, the one “without” is receive-only.

private function setupGroup():void{
	var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/g1");
	groupspec.serverChannelEnabled = true;
	groupspec.postingEnabled = true;
 
	netGroup = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
	netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
 
 
	user = "user"+Math.round(Math.random()*10000);
}

Step 4: Handle NetStatusEvent
We are going to handle at this step just three events. When we connect to Stratus we setup a group. When we connect to NetGroup we reflect it to UI and and when we receive a Posting message we show it in chat history.

private function netStatus(event:NetStatusEvent):void{
	trace(event.info.code);
 
	switch(event.info.code){
		case "NetConnection.Connect.Success":
			setupGroup();
			break;
 
		case "NetGroup.Connect.Success":
			connected = true;
 
			break;
 
		case "NetGroup.Posting.Notify":
			receiveMessage(event.info.message);
			break;
	}
}

Step 5: Sending and receiving message
We have to put together a message object, which handles text, user name, sender ID. Sender ID is useful to have for direct posting. And we also convert NetConnection PeerID to GroupAddress - participant address in the group. When we post() message to a NetGroup, we just distribute it/broadcast it, but it does not come back to us. So that’s why we need to call receiveMessage as well - to display it in history text field.

private function sendMessage():void{
	var message:Object = new Object();
	message.sender = netGroup.convertPeerIDToGroupAddress(nc.nearID);
	message.user = txtUser.text;
	message.text = txtMessage.text;
 
 
	netGroup.post(message);
	receiveMessage(message);
 
	txtMessage.text = "";
}
 
private function receiveMessage(message:Object):void{
	write(message.user+": "+message.text);
}
 
private function write(txt:String):void{
	txtHistory.text += txt+"\n";
}

Step 6: Create UI

<s:TextArea left="10" right="10" top="10" bottom="40" id="txtHistory"/>
<s:TextInput x="10" id="txtUser" text="{user}" bottom="10"/>
<s:TextInput left="145" right="88" id="txtMessage" bottom="10" enter="sendMessage()"/>
<s:Button label="Send" click="sendMessage()" enabled="{connected}" bottom="10" right="10"/>

Step 7: Run it

E-seminar: Drupal and Flex

January 27th, 2010

Flex Let me invite you to e-seminar on Feb/05/2010 about Drupal and Flex presented by my fellow from Adobe - Mihai Corlan.

Find out what were the challenges and issues in creating a new Drupal Image Module using Flash and Flex technology. Get the inside tips from an Adobe Evangelist on what it takes to complete this kind of project, and stand a chance to win your own copy of Flex Builder* to help you get started with your development.
Walk through:

  • Front end design of widgets and main page
  • Back-end integration with Drupal
  • Importing your libraries from Flickr and Picassa
  • How to account for loading issues when having large libraries of images
  • Why Flex and ActionScript where the technology of choice for such a project

Join Mihai Corlan for a 30 minute e-seminar on how this module was built. http://drupalimagemodule-evng.eventbrite.com

Flex 4 beginner series in Czech

January 26th, 2010

adobe-flex-1Just wanted to point my Czech readers to the Flex 4 beginner series I am
publishing at the popular developers magazine Zdrojak.cz.


The first issue is out:
Flex 4: Beginning http://zdrojak.root.cz/clanky/flex-4-zaciname/

I am planning to publish an article per week.

Check it out!

ColdFusion 9 Free Workshop
in Prague on February 23

January 25th, 2010

ColdFusion Prague

My good friend and Adobe ColdFusion specialist Claude Englebert will be leading ColdFusion 9 workshop in Prague on February 23 2010 in the afternoon. I will join Claude and show some Flex 4/CF9 stuff. We have a limited number of seats so feel free to register now and reserve your place.

Location: Adobe Office Prague, Radlická 714/113a, Praha 5

Events

Updated Stratus with Groups and Multicast

January 23rd, 2010

afcs_logoToday we launched newly updated Adobe Stratus (rendezvous-service for P2P clients). This update includes new P2P capabilities available in Flash Player 10.1 and AIR 2.0:

  • RTMP Groups
  • App Multicast
  • Object Replication
  • Posting
  • Directed Routing

Other sources:
- Read more about new Stratus capabilities here at Kevin’s blog.
- Also check out the new Labs page about RTMFP Groups.
- My article about Multicast in Flash Player 10.1
- Read about differences between Stratus and LCCS

Flash Game Development for Beginners

January 22nd, 2010

This is a great tutorial by Emanuele Feronato for all of you trying to understand basic game development concepts and properties like

  • Power
  • Speed
  • Friction
  • Wind
  • Rotation
  • Gravity
  • Thrust

Although these tutorials are written in ActionScript 2.0, you might find it very useful and easy to rewrite them in AS3.

Check it out here

FreeSpin3D: full 3D in Flash Professional

January 21st, 2010

Recently I was pointed to FreeSpin3D - extension to Flash CS4 Professional that enables full 3D interaction right in the IDE - not just the 2.5D available today. So I gave it a try and it suprised me. It actually works the way you’d expect. It’s very similar to Swift3D, but you’re still in Flash authoring.

“It’s a useful extension for 3D animation and can be used by anyone.”
Check out the examples.

freespin3d-timeline
Read the rest of this entry »

Upcoming Conferences Jan/Feb/Mar 10

January 16th, 2010

Flash on devices

Just a little heads-up of upcoming events, where I will be speaking:

Jan 23 2010 - iPhoneDevCamp, Prague
First Czech iphone dev camp organized in Prague. I will be talking about iPhone development in Flash CS5. If you are in town - check it out.

Feb 15-18 2010 Mobile World Congress, Barcelona
Premiere world event in the mobile space. You can catch me at the Adobe Booth every day.

Mar 20 2010 JuniorInternet, Prague
Conference for young creative people organized in 4 countries. I am hosting an inspirational Flash session. Adobe is general partner.

Mar 27-28 2010 JuniorInternet, Bratislava