Update: I’ve added some more features to FlashAIM, such as text formatting.
The community has been posting some pretty cool things made with Flex2. Andre Michelle’s blog has been filled with all sorts of interesting 3D and bitmap experiments. Darron Schall presented a preview of his VNC client which takes advantage of bitmap drawing and the new Socket api. Of course, I couldn’t let everyone upstage me, so with a couple weekends of work, I put together some classes for connecting to and interacting with AIM via the TOC 2.0 protocol. I made the basic user interface below just so I’d have a screenshot of the code in action.
At the moment, the TocConnection class allows a user to log in, keep track of the status of buddies, send and receive messages, and set his or her own user info. I mainly wanted to get the most vital functions in place before releasing any code. I don’t know if I’ll expand it, but maybe I’ll start a project on OS-Flash so that the community can contribute.
Here’s an example of how to use TocConnection in your own code. In the example, event handlers simple trace the output to the console. We start with initialization of the connection and adding listeners to the events. Generally, when a server message is received from AOL, an event is dispatched so listeners can update their status.
After initialization, and once you receive the connect event, you can start sending and receiving messages, and accessing other data. Here you’ll notice I set my user info right away, and sent a message to SOME_USER. Normally you wouldn’t send automated messages like this, but you can start sending messages to other users after you receive the connection event. Finally, I loop through the list buddies for my account and trace them to the console.
package
{
import net.zeusdesign.im.aoltoc.*;
import flash.display.Sprite;
import flash.util.trace;
public class ActionTalkTest extends Sprite
{
private var _aol:TocConnection; //the main connection class
public function ActionTalkTest()
{
//the constructor takes host and port as params, but there are defaults
this._aol = new TocConnection();
this._aol.addEventListener(TocEventType.CONNECT, onAolTocConnect);
this._aol.addEventListener(TocEventType.DISCONNECT, onAolTocDisconnect);
this._aol.addEventListener(TocEventType.ERROR, onAolTocError);
this._aol.addEventListener(TocEventType.RECEIVE_MESSAGE, onAolTocReceiveMessage);
this._aol.addEventListener(TocEventType.UPDATE_BUDDY, onAolTocUpdateBuddy);
this._aol.connect("YOUR_SCREEN_NAME", "YOUR_PASSWORD"); //sorry, can't have mine
}
private function onAolTocConnect(event:TocEvent):Void
{
//set your user info
this._aol.setUserInfo("This user is connected with ActionTalk.");
//sendMessage takes a screen name and the message to send to it
this._aol.sendMessage("SOME_USER", "Hi, I'm talking to you from Flash!");
//the following adds all your buddies to a string and traces it out
var output:String = "";
for(var i:int = 0; i < this._aol.groups.length; i++)
{
//a buddy group is basically an array specific to the Buddy class
var group:BuddyGroup = this._aol.groups[i];
for(var j:int = 0; j < group.length; j++)
output += group.buddyAt(j).screenName + " ";
}
trace("buddies: " + output);
}
//disconnect event handler
private function onAolTocDisconnect(event:TocEvent):Void
{
//raw event data is usually the preparsed string from AOL
trace("Disconnected. " + event.raw); //raw event data is usually the preparsed string from AOL
}
Other events include ERROR, RECEIVE_MESSAGE, and UPDATE_BUDDY. As far as I know, errors that you receive after sign-in aren't fatal. If you receive it beforehand, the server will drop the connection. When another user sends you a message, a specialized event tells you the screen name , his or her message, and if the message was automattcally sent (an away message). Another event is triggered whenever the state of one of your buddies changes. Typically, this happens one someone goes offline, becomes idle, or receives a warning.
//error event handler
private function onAolTocError(event:TocErrorEvent):Void
{
trace("Toc Error: " + event.errorID + " " + event.message + " [" + event.raw + "]");
}
//receive message event handler
private function onAolTocReceiveMessage(event:TocImEvent):Void
{
var output:String = "";
//auto variable in TocImEvent marks auto-replies such as away messages
if(event.auto) output += "Auto-Reply from ";
//screenName refers to who sent the message and message is what they said
output += event.screenName + ": " + event.message;
trace(output);
}
//update buddy event handler
private function onAolTocUpdateBuddy(event:TocUpdateBuddyEvent):Void
{
//generally updates online status, warning level, and idle time
trace("Update Buddy: " + event.screenName + " " + event.online + " " + event.warningLevel);
}
That's it. The instant messenger source code is available. Get the free Flex 2 SDK from Adobe.