Connecting to AIM with ActionScript 3 Sockets

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.

Thumbnail image of basic client 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 

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.

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. Luke

    Wait a second, what are you using that is using ActionScript 3.0? Even Flash Player 8 that just came out is AS 2.0.

  2. Josh

    Luke, an early version of AS3 has been released by Macromedia with FlexBuilder 2 and Flash Player 8.5. You can find it at labs.macromedia.com. I should warn you that it is an alpha pre-release, so don’t consider it ready for an actual website or anything. It’s just something to get us all salivating and hungry for more.

  3. Michael

    Any idea how to fix a sandbox error? Is this a crossdomain.xml problem? I’ve set the Flash Player Global Security Settings via the link you posted above, so that’s not it.

    Error #2044: Unhandled SecurityErrorEvent:.
    text=Error #2048: Security sandbox violation: http://trip/hup/run/FlashAIM.swf
    cannot load data from aimexpress.oscar.aol.com:5190.
    at zeuslabs.im.aoltoc::TocConnection$iinit()
    at FlashAIM/::_TocConnection1_i()
    at FlashAIM$iinit()
    at mx.managers::SystemManager/create()
    at mx.managers::SystemManager/::initializeTopLevelWindow()
    at mx.managers::SystemManager/::frameEndHandler()

  4. Michael

    All right then. The following run from OSX 10.4.7, Flash Player 9, Safari and Opera with Flash Player Security Settings to allow /Users/m/ and /Users/m/FlashAIM.swf

    How about:

    Error #2044: Unhandled SecurityErrorEvent:. text=Error #2048:
    Security sandbox violation:
    https://joshblog.net/FlashAIM.swf
    cannot load data from aimexpress.oscar.aol.com:5190.
    at zeuslabs.im.aoltoc::TocConnection$iinit()
    at FlashAIM/::_TocConnection1_i()
    at FlashAIM$iinit()
    at mx.managers::SystemManager/create()
    at mx.managers::SystemManager/::initializeTopLevelWindow()
    at mx.managers::SystemManager/::frameEndHandler()

    And

    Error #2044: Unhandled SecurityErrorEvent:. text=Error #2048:
    Security sandbox violation:
    file:///Users/m/FlashAIM.swf
    cannot load data from aimexpress.oscar.aol.com:5190.
    at zeuslabs.im.aoltoc::TocConnection$iinit()
    at FlashAIM/::_TocConnection1_i()
    at FlashAIM$iinit()
    at mx.managers::SystemManager/create()
    at mx.managers::SystemManager/::initializeTopLevelWindow()
    at mx.managers::SystemManager/::frameEndHandler()

    Running the swf is no problem, but clicking ‘submit’ after typing in a sign on name and password results in a wait of a couple of minutes and the above security errors. Originally I thought this was a crossdomain problem but there’s no way to set policy at aol.com right, and I’m sure you got this working, so there must be something odd with my setup?

  5. Anz

    Hi,
    I am getting security error when I try to upload file to my server using FileReferenceList. I have tried your application and my own source code, both showing sandbox violation.

    Error #2044: Unhandled SecurityErrorEvent:. text=Error #2049: Security sandbox violation: http://digitalmesh.com/AnzTest/Upload/Upload_wt1.swf cannot upload data to http://www.digitalmesh.com/AnzTest/upload/default.aspx.

    Please check http://digitalmesh.com/AnzTest/Upload/FileUpload.html
    or
    http://digitalmesh.com/AnzTest/Upload/Upload_wt1.html

    Please help,
    Anz

  6. Jos Yule

    Hello. I’ve just downloaded your code and compiled it and have it running. I can log in and see my buddies list. However, when i try to send a message to the flash client, it does not seem to receive it, and when i send a message out from the flash client, it never reaches its target buddy. I’m just wondering if you have any suggestions for places that i could check, in the code, to see what i’m doing wrong (i’m running your example app), or if the protocol has changed…

    Thanks!

  7. Josh Tynjala

    Jos Yule, I recently fixed some bugs with sending messages. If you have a subversion client, the latest version of my AIM source code (now under the name MumbleLib), is available on Google Code.

  8. Jos Yule

    Excellent. I’ve done a quick look over of the code – i like the new layout. I see that you’ve made it ready for other IM formats. Looks great – i’m excited to prototype some applications with this.

  9. Jos Yule

    I’ve been working through your original example flex app, FlashAIM, trying to retro-fit the new MumbleLib code. While this is illuminating, it would be a huge help if you had any example code using the new lib available. If not, i will continue on with this conversion, and post it back to you when it is done (and working!).

  10. Jos Yule

    Here is the proof-of-concept code that i’m using to test/work out how to use the new lib:

    http://www.theorganization.net/work/jos/MumbleAIR.mxml

    Now, it signs on, and i can see the signed on user. However, i am not receiving any messages sent in, or able to send any out. Do i need to have someone on my buddy list before i can send/receive messages? (i don’t think so).

    Is there anything obvious that i’m missing/doing wrong?

    Thanks again!

  11. Josh Tynjala

    I’m currently working on an AIR client that uses MumbleLib. I intend to release an alpha version with source code as soon as I finish implementing a couple more features.

    Jos, your code looks fine to me. It’s possible that AIMService may not know how to receive messages from contacts that aren’t on the user’s buddy list. I seem to remember running into that problem. I’ll put that on my list of things to fix.

  12. Jos Yule

    Thanks for the update. I’ve added a check to see who is on the buddy list, and even when a user is on the buddy list i still can’t send out or receive messages.
    Looking forward to your AIM AIR client!

  13. Steven

    Thanks Josh! This is a great help. Does anyone know of any Flex implementations for Yahoo, MSN Google?

    Where can I obtain the latest AIM code?

    Thanks,
    -Steven

  14. Josh Tynjala

    Steven, I remember that someone tried to connect to MSN in AS3 a while back, but he ran into some trouble with SSL and security stuff, so I think the project was filed away and never finished.

    If you want the latest AIM code, check out my Mumble Messenger project.

  15. Pingback: » Connecting to AIM with ActionScript 3 Sockets - ScatteredGenius.com

  16. Joe

    Hi guys I’m having some trouble with this error only using firefox.
    I’m try to play a playlist on imeem.com and i always get this this error. I see it mention here but I don’t know what to do exactly in order to solve it since I’ve never done this before. Here’s the error :

    Error #2044: Unhandled SecurityErrorEvent:. text=Error #2048: Security sandbox violation: http://resources.imeem.com/resources/versioned/64/flash/audio_player3.swf cannot load data from http://ads.imeem.com/ads/amf/.
    	at imeem.api.player.implementation._private.apisnapshot.services::ServiceCall/overrideDefaultGateway()
    	at imeem.api.player.implementation._private.apisnapshot.services::InternalAPIService/getAd()
    	at imeem.api.player.implementation::MediaPlayer/play()
    	at imeem.player::ImeemPlayer/play()
    	at imeem.player::ImeemPlayer/playCurrent()
    	at imeem.audioplayer::AudioPlayerControl/playCurrent()
    	at imeem.audioplayer::AudioPlayerComponentRetro/::onUIReady()
    	at Function/http://adobe.com/AS3/2006/builtin::apply()
    	at mx.core::UIComponent/::callLaterDispatcher2()
    	at mx.core::UIComponent/::callLaterDispatcher()

    Thank you in advance!

  17. mnps

    Can anyone just tell me what is AIM?
    Im doing a project that needs to connect VC++ to AS3 using sockets.Is there any sockets available in AS3 with which i can connect my VC++ code to AS3??
    Awaiting for a positive reply.
    Thanking you
    mnps