Skip to content
lyokato edited this page Sep 14, 2010 · 7 revisions

DESCRIPTION

This library allows you to build IM client for services which supports XMPP protocol.

USAGE

prepare config


import org.coderepos.net.xmpp.XMPPConfig;

var config:XMPPConfig = new XMPPConfig();
config.host = "talk.google.com"
config.port = 5222;
config.username = "[email protected]";
config.password = "mypassword";

// these information is used for XEP-0115(Entity Capabilities) or XEP-0092(Software Version)
config.applicationName = "MyIM";
config.applicationVersion = "0.0.1";
config.applicationLanguage = "ja";
config.applicationNode = "http://example.org/product/MyIM";

// set by default, but if you need to change,
//config.applicationType = "pc";
//config.applicationCategory = "client";

build and run stream


import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;

import org.coderepos.net.xmpp.stream.XMPPStream;
import org.coderepos.net.xmpp.events.XMPPStreamEvent;
import org.coderepos.net.xmpp.events.XMPPErrorEvent;
import org.coderepos.net.xmpp.events.XMPPMessageEvent;
import org.coderepos.net.xmpp.events.XMPPPresenceEvent;
import org.coderepos.net.xmpp.events.XMPPSubscriptionEvent;
import org.coderepos.net.xmpp.events.XMPPRosterEvent;

var stream:XMPPStream = new XMPPStream(config);
stream.addEventListener(Event.CONNECT, connectHandler);
stream.addEventListener(Event.CLOSE, closeHandler);
stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
stream.addEventListener(XMPPErrorEvent.PROTOCOL_ERROR, protocolErrorHandler);
stream.addEventListener(XMPPErrorEvent.AUTH_ERROR, authErrorHandler);
stream.addEventListener(XMPPStreamEvent.START, starthandler);
stream.addEventListener(XMPPStreamEvent.TLS_NEGOTIATING, startTLSHandler);
stream.addEventListener(XMPPStreamEvent.AUTHENTICATIONG, authenticatingHandler);
stream.addEventListener(XMPPStreamEvent.BINDING_RESOURCE, bindingHandler);
stream.addEventListener(XMPPStreamEvent.ESTABLISHING_SESSION,, sessionHandler);
stream.addEventListener(XMPPStreamEvent.LOADING_ROSTER, rosterHandler);
stream.addEventListener(XMPPStreamEvent.READY, readyHandler);

stream.addEventListener(XMPPRosterEvent.CHANGED, rosterChangedHandler);
stream.addEventListener(XMPPPresenceEvent.CHANGED, presenceChangedHandler);
stream.addEventListener(XMPPPresenceEvent.LEAVED, presenceLeavedHandler);
stream.addEventListener(XMPPMessageEvent.RECEIVED, messageReceivedHandler);
stream.addEventListener(XMPPSubscriptionEvent.RECEIVED, subscriptionReceivedHandler);

stream.start();

Events

Stream Transaction Event

Roster Event



import org.coderepos.net.xmpp.JID;
import org.coderepos.net.xmpp.roster.RosterItem;

stream.addEventListener(XMPPRosterEvent.CHANGED, rosterChangedHandler);

private function rosterChangedHandler(e:XMPPRosterEvent):void
{
  var contact:JID = e.contact;
  var item:RosterItem = stream.getRosterItem(contact);
  var name:String = item.name;
  var subscription:String = item.subscription;
  var ask:String = item.ask;
  var groups:Array = item.groups;
}

subscription

status of subscription.
both, from, to none

ask

if ask is ‘subscribe’, now requesting ‘subscribe’, but not get respnse yet.
if ask is ‘unsubscribe’, now requesting ‘unsubscribe’, but not get response yet.

Presence Event

send presence


import org.coderepos.net.xmpp.StatusType;

stream.changePresence(StatusType.AWAY, "sleepy...");

StatusType.CHAT
StatusType.AWAY
StatusType.XA
StatusType.DND

if you want to set priority,


stream.changePresence(StatusType.AWAY, "sleepy...", 10);

received available presence


import org.coderepos.net.xmpp.roster.ContactResource;

stream.addEventListener(XMPPPresenceEvent.CHANGED, presenceChangedHandler);

private function presenceChangedHandler(e:XMPPPresenceEvent):void
{
  var contact:JID = e.contact;
  var cr:ContactResource = stream.getContactResource(contact);
  var show:String  = cr.show;
  var priority:uint    = cr.priority;
  var status:String = cr.status;
}

status – chat/away/xa/dnd

received unavailable presence


stream.addEventListener(XMPPPresenceEvent.LEAVED, presenceLeavedHandler);

private function presenceLeavedHandler(e:XMPPPresenceEvent):void
{
  var contact:JID = e.contact;
  showMessage(contact.toString() + " goes offline");
}

Message Event

send message

received message


import org.coderepos.net.xmpp.XMPPMessage;

stream.addEventListener(XMPPMessageEvent, messageReceivedHandler);

private function messageReceivedHandler(e:XMPPMessageEvent):void
{
  var msg:XMPPMessage = e.message;
  var subject:String = msg.subject;
  var type:String = msg.type;
  var body:String = msg.body;
  var threadID:String = msg.thread;
  var date:Date = msg.date;
}

MessageType.NORMAL
MessageType.CHAT
MessageType.GROUPCHAT
MessageType.HEADLINE
MessageType.ERROR

Subscription Event

when you want to send subscription-reqest


stream.subscribe(new JID("[email protected]"));

when you received subscription-request


stream.addEventListener(XMPPSubscriptionEvent.RECEIVED, subscriptionReceivedHandler);

private function subscriptionReceivedHandler(e:XMPPSubscriptionEvent):void
{
  var contact:JID = e.contact;
  showMessageBox(contact.toString() + " is requesting to follow you.");
}

// if user choosed to accept,
stream.accpetSubscriptionRequest(contact);
// and if needed
stream.subscribe(contact);

// if user decided to reject
stream.denySubscriptionRequest(contact);

Roster Management

Contact Resource and Presence Management