<?xml version="1.0" encoding="utf-8"?>
<!--
测试成功项:
1、用户登录 connect()
2、为指定用户发送消息 sendMessage
3、登录会议室 Room
4、在会议室中发送消息 chatRoom.sendMessage
5、会议室接收成功 addEventListener(MessageEvent.MESSAGE, messageHandler);
6、接收服务器广播信息(有时候会出现空指针异常,这个是XIFF更新后造成的,需要修改下源代码即可) addEventListener(MessageEvent.MESSAGE, messageHandler);
7、接收其他人发送的信息成功(只有该用户处于在线状态才能接收) addEventListener(MessageEvent.MESSAGE, messageHandler);
8、修改在线状态成功,使用Presence类 Presence
9、接收好友邀请 grantSubscription
10、获取好友列表 Roster
未解决:
1、在其他地方登录后,自动下线
2、注册
3、修改注册信息
4、获取组列表
5、添加维护组
6、新建、维护会议室
7、用户头像:客户端自己实现
8、添加好友
9、获取会议室列表
-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()" height="100%" width="100%">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import org.igniterealtime.xiff.conference.Room;
import org.igniterealtime.xiff.core.EscapedJID;
import org.igniterealtime.xiff.core.UnescapedJID;
import org.igniterealtime.xiff.core.XMPPConnection;
import org.igniterealtime.xiff.data.Message;
import org.igniterealtime.xiff.data.Presence;
import org.igniterealtime.xiff.data.events.MessageEventExtension;
import org.igniterealtime.xiff.data.im.RosterItemVO;
import org.igniterealtime.xiff.events.ConnectionSuccessEvent;
import org.igniterealtime.xiff.events.LoginEvent;
import org.igniterealtime.xiff.events.MessageEvent;
import org.igniterealtime.xiff.events.RoomEvent;
import org.igniterealtime.xiff.events.RosterEvent;
import org.igniterealtime.xiff.events.XIFFErrorEvent;
import org.igniterealtime.xiff.im.Roster;
public const SERVER_NAME:String = "charles"; //服务器名称
public const RESOURCE:String = "spark"; //客户端资源名称
public const CHATROOM:String = "testroom"; //聊天室名称
private var chatRoom:Room;
private var _conn:XMPPConnection;
private function initApp():void
{
_conn = new XMPPConnection();
_conn.addEventListener(LoginEvent.LOGIN, onLogin);
_conn.addEventListener(ConnectionSuccessEvent.CONNECT_SUCCESS, connectSuccess);
_conn.addEventListener(MessageEvent.MESSAGE, messageHandler);
_conn.addEventListener(XIFFErrorEvent.XIFF_ERROR, error);
// #################################### 好友列表 ############################
_people = new ArrayCollection();
_people.addItem({displayName: "--好友--"});
rosterList.dataProvider = _people;
// 【处理Roster 好友列表】:测试成功 (必须放在登录之前加载)
_roster.connection = _conn;
_roster.addEventListener(RosterEvent.ROSTER_LOADED, onRoster); // 加载好友列表
_roster.addEventListener(RosterEvent.SUBSCRIPTION_DENIAL, onRoster);
_roster.addEventListener(RosterEvent.SUBSCRIPTION_REQUEST, onRoster); //当别人要加你为好友时
_roster.addEventListener(RosterEvent.SUBSCRIPTION_REVOCATION, onRoster);
_roster.addEventListener(RosterEvent.USER_ADDED, onRoster); // 添加好友
_roster.addEventListener(RosterEvent.USER_AVAILABLE, onRoster);
_roster.addEventListener(RosterEvent.USER_PRESENCE_UPDATED, onRoster); // 当你的好友更新了状态时
_roster.addEventListener(RosterEvent.USER_REMOVED, onRoster);
_roster.addEventListener(RosterEvent.USER_SUBSCRIPTION_UPDATED, onRoster);
_roster.addEventListener(RosterEvent.USER_UNAVAILABLE, onRoster);
}
// 【处理XIFF错误】:成功
private function error(event:XIFFErrorEvent):void
{
Alert.show(event.errorCode + " : " + event.errorMessage);
}
// 【连接成功】
private function connectSuccess(event:ConnectionSuccessEvent):void
{
chatContent.htmlText += "连接成功...";
}
// ############################### 用户登录 #################################
private function doLogin():void
{
if (!_conn.isLoggedIn())
{
_conn.username = username.text;
_conn.password = password.text;
_conn.server = SERVER_NAME;
_conn.resource = RESOURCE;
_conn.port = 5222;
_conn.connect();
}
else
{
_conn.disconnect();
loginBT.label = "登录";
inputMsg.enabled = false;
sendBtn.enabled = false;
msg.enabled = false;
send2Btn.enabled = false;
}
}
private var _roster:Roster = new Roster();
private var _people:ArrayCollection; // 好友列表
private function onLogin(event:LoginEvent):void
{
inputMsg.enabled = true;
sendBtn.enabled = true;
msg.enabled = true;
send2Btn.enabled = true;
loginBT.label = "注销";
chatContent.htmlText += "Welcome " + username.text + "<br/>";
// 【登录房间】:成功
chatRoom = new Room(_conn);
chatRoom.roomJID = new UnescapedJID(CHATROOM + "@" + "conference." + SERVER_NAME);
chatRoom.roomName = CHATROOM; // 组名称
chatRoom.join();
chatRoom.addEventListener(RoomEvent.GROUP_MESSAGE, groupMessage);
}
/**
* 【设置用户在线状态】:已成功
*/
private function changeStatus():void
{
// 参数一:在线状态,Presence中的SHOW_,参数二:状态信息,一般写的都是“在线”、“忙碌”、“隐身”等汉字
// 方式1:使用Roster封装方法变更。
_roster.setPresence(Presence.SHOW_AWAY, "状态信息", 0);
// 方式2:直接变更
// var recipient:EscapedJID = new EscapedJID(_conn.domain);
// var unavailablePresence:Presence = new Presence(recipient, null, null,Presence.SHOW_AWAY, "状态信息");
// _conn.send(unavailablePresence);
}
/**
* 【注销】:已成功
*/
public function logout():void
{
// Send an unavilable presence
_roster.setPresence(Presence.SHOW_AWAY, "状态信息", 0);
var recipient:EscapedJID = new EscapedJID(_conn.domain);
var unavailablePresence:Presence = new Presence(recipient, null, Presence.TYPE_UNAVAILABLE, null, "下线原因");
_conn.send(unavailablePresence);
// Now disconnect
_conn.disconnect();
}
// #################################### 好友列表 ############################
// 处理好友列表所有事件
private function onRoster(event:RosterEvent):void
{
trace("onRoster. " + event.toString());
switch (event.type)
{
case RosterEvent.ROSTER_LOADED:
// 【加载好友列表】:测试成功,返回的是RosterItemVO对象
var r:Roster = event.currentTarget as Roster
for (var i:int = 0; i < r.length; i++)
{
_people.addItem(r[i]);
}
break;
case RosterEvent.SUBSCRIPTION_DENIAL:
break;
case RosterEvent.SUBSCRIPTION_REQUEST:
// If the JID is in the roster, accept immediately
if (_roster.getPresence(event.jid) != null)
{ // 【处理好友添加请求】:测试成功
var fromJid:UnescapedJID = event.jid; //联系人JID
receiveMsg.htmlText += "<font color='#8e2800'><b>" + fromJid + " 请求将你列为好友!<br/>";
//同意对方将自己加为好友
_roster.grantSubscription(fromJid, true);
//拒绝对方将自己加为好友
//_roster.denySubscription(fromJid);
}
break;
case RosterEvent.SUBSCRIPTION_REVOCATION:
break;
case RosterEvent.USER_ADDED:
// _people[event.jid.toString()] = event.data;
break;
case RosterEvent.USER_AVAILABLE:
break;
case RosterEvent.USER_PRESENCE_UPDATED:
break;
case RosterEvent.USER_REMOVED:
break;
case RosterEvent.USER_SUBSCRIPTION_UPDATED:
break;
case RosterEvent.USER_UNAVAILABLE:
break;
}
}
// ############################### 会议室 #################################
// 【接收会议室信息】:测试成功
private function groupMessage(event:RoomEvent):void
{
displayUserMessage(getNick