Connecting to an XMPP / Jabber Server with the XIFF AS3 Library
As I mentioned earlier, I am starting to build an XMPP chat client for the XMPP / Jabber community server I set up today. I am using the XIFF ActionScript 3 library. While the library seems to be pretty solid, there is not a ton of documentation for it right now.
Here is a simple example that shows how to connect to an XMPP / Jabber server, join a room, and send a message to the room. The example assumes that you already have a username and password on the server (I haven’t figured out how to get it to connect anonymously yet):
// ActionScript file
import org.jivesoftware.xiff.conference.Room;
import org.jivesoftware.xiff.core.JID;
import org.jivesoftware.xiff.core.XMPPSocketConnection;
import org.jivesoftware.xiff.events.LoginEvent;
import org.jivesoftware.xiff.events.RoomEvent;
import org.jivesoftware.xiff.events.XIFFErrorEvent;
private var connection:XMPPSocketConnection;
private function onCreationComplete():void
{
connection = new XMPPSocketConnection();
connection.username = "test";
connection.password = "test";
connection.server = "mesh.local";
connection.port = 5222;
connection.addEventListener(LoginEvent.LOGIN, onLogin);
connection.addEventListener(XIFFErrorEvent.XIFF_ERROR, onError);
connection.connect("standard");
}
private function onLogin(e:LoginEvent):void
{
var room:Room = new Room(connection);
room.roomJID = new JID("test@conference.mesh.local");
room.addEventListener(RoomEvent.ROOM_JOIN, onRoomJoin);
room.join();
}
private function onRoomJoin(e:RoomEvent):void
{
Room(e.target).sendMessage("im here");
}
private function onError(e:XIFFErrorEvent):void
{
trace(e.errorCode);
}
Thanks go out to Mitchell Hashimoto who helped me figure this out over on the XIFF forums.