6

Connecting to an XMPP / Jabber Server with the XIFF AS3 Library

 3 years ago
source link: http://www.mikechambers.com/blog/2008/08/14/connecting-to-an-xmpp-jabber-server-with-the-xiff-as3-library/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Connecting to an XMPP / Jabber Server with the XIFF AS3 Library

Thursday, August 14, 2008

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 havent 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("[email protected]");
    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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK