5

Connecting Anonymously to an XMPP Group Chat using XIFF

 3 years ago
source link: http://www.mikechambers.com/blog/2008/08/18/connecting-anonymously-to-an-xmpp-group-chat-using-xiff/
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 Anonymously to an XMPP Group Chat using XIFF

Monday, August 18, 2008

I posted some code the other day that showed how to use the XIFF AS3 Library to connect to an XMPP server and join a group chat room.

Below is an slightly modified example that shows how to login anonymously, and connect to a room.

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.useAnonymousLogin = true;
    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.nickname = "frank";
    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);
}

The two differences are that you set:

connection.useAnonymousLogin = true;

and dont send a username and password. Once you join the room, you can then set your nick like so:

room.nickname = "frank";

I have set up a test chat room on my server if you want to play around with the code.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK