Juga in mid air
White suite, toe socks and Citizen eco-drive watch.

XIFF chat - Part 4: Invitations

This time the example is for how to use the invitation listener available in XIFF library. Invitations in XMPPrefers to the event when someone invites some other one to join a certain multi user chat room.

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import flash.system.Security;
    import flash.external.ExternalInterface;

    import org.igniterealtime.xiff.conference.*;
    import org.igniterealtime.xiff.core.*;
    import org.igniterealtime.xiff.data.*;
    import org.igniterealtime.xiff.events.*;
    import org.igniterealtime.xiff.exception.*;
    import org.igniterealtime.xiff.util.*;

    [SWF(backgroundColor = '0x5B5B5B', frameRate = '33', width = '600', height = '400')]

    /**
     * @see http://paazmaya.fi/xiff-chat-part-4-invitations
     */
    public class XIFFstep4 extends Sprite
    {
        private const SERVER:String = "192.168.1.37";
        private const PORT:uint = 5222;
        private const USERNAME:String = "flasher";
        private const PASSWORD:String = "flasher";
        private const RESOURCE_NAME:String = "flashPlayer";
        private const CHECK_POLICY:Boolean = true;

        private var _connection:XMPPSocketConnection;
        private var _listener:InviteListener;
        private var _keepAlive:Timer;

        public function XIFFstep4()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            loaderInfo.addEventListener(Event.INIT, onInit);
        }

        private function onInit(event:Event):void
        {
            if (CHECK_POLICY)
            {
                Security.loadPolicyFile("xmlsocket://" + SERVER + ":5229");
            }
            connect();

            _keepAlive = new Timer(2 * 60 * 1000);
            _keepAlive.addEventListener(TimerEvent.TIMER, onKeepAliveLoop);
        }

        private function connect():void
        {
            _connection = new XMPPSocketConnection();
            _connection.addEventListener(XIFFErrorEvent.XIFF_ERROR, onXiffError);
            _connection.addEventListener(LoginEvent.LOGIN, onLogin);

            _connection.useAnonymousLogin = false;
            _connection.username = USERNAME;
            _connection.password = PASSWORD;
            _connection.server = SERVER;
            _connection.port = PORT;
            _connection.resource = RESOURCE_NAME;

            /**
             * flash
             * terminatedFlash
             * standard
             * terminatedStandard
             */
            var type:String = "flash";
            _connection.connect(type);
        }

        private function drawInvDialog(roomName:String, user:String):void
        {
            var sp:Sprite = new Sprite();
            sp.name = roomName;
            sp.mouseChildren = false;
            sp.x = 10 + 20 * numChildren;
            sp.y = 10 + 20 * numChildren;
            sp.addEventListener(MouseEvent.MOUSE_OVER, onInvDialogMouse);
            sp.addEventListener(MouseEvent.CLICK, onInvDialogMouse);

            var w:Number = 300;
            var h:Number = 100;

            var gra:Graphics = sp.graphics;
            gra.beginFill(0x0066FF);
            gra.drawRoundRect(0, 0, w, h, 10, 10);
            gra.endFill();
            gra.beginFill(0x3399FF);
            gra.drawRoundRect(5, 5, w - 10, h - 10, 10, 10);
            gra.endFill();

            var format:TextFormat = new TextFormat("Verdana", 12, 0x121212);

            var field:TextField = new TextField();
            field.name = "field";
            field.multiline = true;
            field.wordWrap = true;
            field.text = "You have been invited to a conference room [" + roomName + "] by user [" + user + "]";
            field.x = 7;
            field.y = 7;
            field.width = sp.width - 14;
            field.height = sp.height - 14;
            sp.addChild(field);

            addChild(sp);
        }

        private function onInvDialogMouse(event:MouseEvent):void
        {
            var sp:Sprite = event.target as Sprite;

            if (event.type == MouseEvent.MOUSE_OVER)
            {
                swapChildren(sp, getChildAt(numChildren - 1));
            }
            else if (event.type == MouseEvent.CLICK)
            {
                var room:Room = new Room(_connection);
                room.roomJID = new UnescapedJID(sp.name);
                room.join();
            }
        }

        private function onInvited(event:InviteEvent):void
        {
            drawInvDialog(event.room.roomJID.toString(), event.from.toString());
        }

        private function onLogin(event:LoginEvent):void
        {
            _keepAlive.start();

            _listener = new InviteListener(_connection);
            _listener.addEventListener(InviteEvent.INVITED, onInvited);

            var presence:Presence = new Presence(null, _connection.jid.escaped, null, null, null, 1);
            _connection.send(presence);
        }

        private function onXiffError(event:XIFFErrorEvent):void
        {
            trace("onXiffError. " + event.toString());
            trace("onXiffError.errorMessage: " + event.errorMessage);
        }

        private function onKeepAliveLoop(event:TimerEvent):void
        {
            if (_connection.isLoggedIn())
            {
                _connection.sendKeepAlive();
            }
        }
    }
}

Documentation for the API is available at another location and its been updated promptly.