大家搜到这篇文章应该在架设openfire+asmack的及时消息服务吧
算了。写到一半发现东西比较多,大家直接拿demo吧,主要看看里面的ConnectManager类
这里简单列出几个函数的实现
1:登录
public static ConnectionConfiguration connConfig;
public static XMPPConnection xmppConnection;
//函数传入的参数为,openfire IP,端口一般为5222,用户名,密码,
//以及含域名的用户名,如admin@xmpptest
public static void Login(final String ServerIP, final int serverport,
final String Username, final String Password, String userid) {
connConfig = new ConnectionConfiguration(ServerIP, serverport);
connConfig.setSASLAuthenticationEnabled(false);
xmppConnection = new XMPPConnection(connConfig); //设定connection配置
new Thread() {
@Override
public void run() {
try {
xmppConnection.connect();
xmppConnection.login(Username, Password); //登录
} catch (Exception ex) {
}
}
}.start();
}
登录完了以后便是收发消息
2:接收一对一的对话(不含群组会话)
public static Chat chat;
public static ChatManager chatManager;
// 注册单人对话监听
protected static void RegisterMessageListener() {
chatManager = xmppConnection.getChatManager();
chatManager.addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat, boolean arg1) {
chat.addMessageListener(new MessageListener() { //通过添加一个messaagelitener 来接收消息
public void processMessage(Chat arg0, Message message) {
String msg = message.getBody(); //消息主体
sendhandlemsg(message.getFrom(), msg, false);
}
});
}
});
}
3:发送一对一的对话
public static void semdmessage(final String msg, final String msgto) {
// 初始化发送消息,chatManager在前文初始化过
chat = chatManager.createChat(msgto, null);
new Thread() {
@Override
public void run() {
try {
//消息发送出去
chat.sendMessage(msg);
sendhandlemsg(User, msg, true);
} catch (XMPPException e) {
e.printStackTrace();
}
}
}.start();
}
接下来是加入房间
代码比较多,前两段是查找房间,并储存在一个map中,第三段是加入
4:加入房间
// 获取房间列表的adpter,当然也可以直接返回list
public static RoomListAdapter GetRoomAdapter() {
RoomListAdapter adapter;
//储存系统最上层的房间名称
List<Map<String, String>> groups = new ArrayList<Map<String, String>>(); // 一级目录组
//储存系统最上层房间名称,及其子房间的名称与jid
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); // 二级目录组
Map<String, String> group;
try {
//第一次第二个参数为空,可以获取到系统最上层的房间,包括公共房间等
//但是此时获取的房间是不能加入的
Collection<HostedRoom> ServiceCollection = MultiUserChat
.getHostedRooms(xmppConnection, "");
//遍历讲上文获取到的房间的Jid传入第二个参数,从而获取子房间
//此时获取的房间便能加入
for (HostedRoom s : ServiceCollection) {
group = new HashMap<String, String>();
group.put("group", s.getName());
groups.add(group);
childs.add(GetRoomFromServers(s.getJid()));
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
adapter = new RoomListAdapter(groups, childs, activity);
return adapter;
}
// 根据服务器获取房间列表
private static List<Map<String, String>> GetRoomFromServers(String jid) {
List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
Map<String, String> result;
try {
Collection<HostedRoom> ServiceCollection = MultiUserChat
.getHostedRooms(xmppConnection, jid);
for (HostedRoom s : ServiceCollection) {
result = new HashMap<String, String>();
result.put("child", s.getName());
result.put("childid", s.getJid());
resultList.add(result);
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultList;
}
// 加入一个房间
public static void JoinRoom(String jid) {
MultiUserChat multiUserChat = new MultiUserChat(xmppConnection, jid);
curmultchat = multiUserChat;
try {
multiUserChat.join(User); //user为你传入的用户名
RegisterRoomMessageListener();
} catch (XMPPException e) {
e.printStackTrace();
}
}
然后便是房间内消息的收发
房间发送消息与单人发送消息的方法一致