Hello World



This is a very basic "Hello World" application. Click right button to see the source codes.


For the full screen version click here.
To download the source codes click here.

As you can see it is very easy to communicate to the server with just 2 components. (ServerCommunication and Room)
<Communication:ServerCommunication id="client" onConnected="OnConnected(event)"
ExpansionID="TestExpansion" />

<Rooms:Room id="room" ServerComm="{client}" onRoomJoined="OnJoinedRoom(event)"
onRoomLeft="OnLeftRoom(event)" onRPC="OnRPC(event)" RoomID="HelloWorldTestRoom" />
Below you see the code where we handle some events raised by the room component.

import Lib.Communication.ParameterCollection;
import Lib.Communication.Events.RPCEvent;
import Lib.Communication.Events.RoomEvent;
import Lib.Communication.Rooms.Room;
import Lib.Communication.Events.CommunicationEvent;
import Lib.Communication.Events.ServerEvent;

protected function ApplicationComplete():void
{
this.client.Connect();
}

// Server event handlers
protected function OnConnected(event:ServerEvent):void
{
this.client.LoginUser("guest", null, null, null, null);
}

// Room event handlers
protected function OnJoinedRoom(event:RoomEvent):void
{
this.textPublic.text = event.UserName + " has joined.\n" + this.textPublic.text;
}

protected function OnLeftRoom(event:RoomEvent):void
{
this.textPublic.text = event.UserName + " has left.\n" + this.textPublic.text;
}

protected function OnRPC(event:RPCEvent):void
{
this.textPublic.text = event.RpcParameters.FetchParameterValue("userName") + " said \"Hello World\".\n" + this.textPublic.text;
}

// Button event handler
protected function btnClick():void
{
var params:ParameterCollection = new ParameterCollection();
params.AddParameter("userName", this.client.LoggedInUser.UserName);
this.room.MakeRoomRPC("HelloWorld", params);
}


Note that proper namespaces should be included in the flex application tag.
Example:

xmlns:Communication="Lib.Communication.*"
xmlns:Rooms="Lib.Communication.Rooms.*"

These will be automatically added when you start writing code and don't copy and paste it.

No comments: