很久没有写博客了,回顾了一下原先学习的,接着没有写完的继续写咯。(ps:告诉大家一个秘密,其实写写博客对自己的帮助是非常大的哦~)
好了废话不多说,进入主题吧。
一、单人聊天
1)发送消息:
首先要获取一个聊天窗口,getConnection()为获取连接connection的方法,调用getFriendChat()获取
-
private Map chatManage = new HashMap();
-
-
-
-
-
-
-
-
public Chat getFriendChat(String friend, MessageListener listenter) {
-
if(getConnection()==null)
-
return null;
-
-
for (String fristr : chatManage.keySet()) {
-
if (fristr.equals(friend)) {
-
-
return chatManage.get(fristr);
-
}
-
}
-
-
Chat chat = getConnection().getChatManager().createChat(friend + "@"+
-
getConnection().getServiceName(), listenter);
-
-
chatManage.put(friend, chat);
-
return chat;
-
}
friend为好友名,不是JID;listener 监听器可以传null,利用聊天窗口对象调用sendMessage发送消息
这里sendMessage我传的是一个JSON字符串,以便更灵活的控制,发送消息完成,很简单吧~
-
Chat chat = getFriendChat(friend,null);
-
try {
-
String msgjson = "{\"messageType\":\""+messageType+"\",\"chanId\":\""+chanId+"\",\"chanName\":\""+chanName+"\"}";
-
chat.sendMessage(msgjson);
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
2)接受消息:
创建一个消息监听器,这里我单独写了一个类
-
package com.techrare.listener;
-
-
import org.jivesoftware.smack.Chat;
-
import org.jivesoftware.smack.ChatManagerListener;
-
import org.jivesoftware.smack.MessageListener;
-
import org.jivesoftware.smack.packet.Message;
-
import org.jivesoftware.smack.util.StringUtils;
-
import org.json.JSONException;
-
import org.json.JSONObject;
-
-
import com.techrare.utils.XmppConnection;
-
-
-
-
-
-
-
-
public class TaxiChatManagerListener implements ChatManagerListener {
-
-
public void chatCreated(Chat chat, boolean arg1) {
-
chat.addMessageListener(new MessageListener() {
-
public void processMessage(Chat arg0, Message msg) {
-
-
StringUtils.parseName(XmppConnection.getInstance().getConnection().getUser());
-
-
msg.getFrom();
-
-
String body = msg.getBody();
-
boolean left = body.substring(0, 1).equals("{");
-
boolean right = body.substring(body.length()-1, body.length()).equals("}");
-
if(left&&right){
-
try {
-
JSONObject obj = new JSONObject(body);
-
String type = obj.getString("messageType");
-
String chanId = obj.getString("chanId");
-
String chanName = obj.getString("chanName");
-
} catch (JSONException e) {
-
e.printStackTrace();
-
}
-
-
}
-
}
-
});
-
}
-
}
添加监听,最好是放在登录方法中,在关闭连接方法中,移除监听,原因是为了避免重复添加监听,接受重复消息
退出程序应该关闭连接,移除监听,该监听可以接受所有好友的消息,很方便吧~
-
TaxiChatManagerListener chatManagerListener = new TaxiChatManagerListener();
-
getConnection().getChatManager().addChatListener(chatManagerListener);
-
connection.getChatManager().removeChatListener(chatManagerListener);
二、多人聊天(会议室)
1)、发送消息
首先要获取会议室对象(MultiUserChat),有两种方法获取
两个方法前面都有讲到,这里再回顾一下
1、创建会议室
-
-
-
-
-
-
public MultiUserChat createRoom(String user, String roomName,String password) {
-
if (getConnection() == null)
-
return null;
-
-
MultiUserChat muc = null;
-
try {
-
-
muc = new MultiUserChat(getConnection(), roomName + "@conference."
-
+ getConnection().getServiceName());
-
-
muc.create(roomName);
-
-
Form form = muc.getConfigurationForm();
-
-
Form submitForm = form.createAnswerForm();
-
-
for (Iterator fields = form.getFields(); fields
-
.hasNext();) {
-
FormField field = (FormField) fields.next();
-
if (!FormField.TYPE_HIDDEN.equals(field.getType())
-
&& field.getVariable() != null) {
-
-
submitForm.setDefaultAnswer(field.getVariable());
-
}
-
}
-
-
List owners = new ArrayList();
-
owners.add(getConnection().getUser());
-
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
-
-
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
-
-
submitForm.setAnswer("muc#roomconfig_membersonly", false);
-
-
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
-
if (!password.equals("")) {
-
-
submitForm.setAnswer("muc#roomconfig_passwordprotectedroom",
-
true);
-
-
submitForm.setAnswer("muc#roomconfig_roomsecret", password);
-
}
-
-
-
-
submitForm.setAnswer("muc#roomconfig_enablelogging", true);
-
-
submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
-
-
submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
-
-
submitForm.setAnswer("x-muc#roomconfig_registration", false);
-
-
muc.sendConfigurationForm(submitForm);
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
return null;
-
}
-
return muc;
-
}
2、加入会议室,
-
-
-
-
-
-
-
-
-
-
-
public MultiUserChat joinMultiUserChat(String user, String roomsName,
-
String password) {
-
if (getConnection() == null)
-
return null;
-
try {
-
-
MultiUserChat muc = new MultiUserChat(getConnection(), roomsName
-
+ "@conference." + getConnection().getServiceName());
-
-
DiscussionHistory history = new DiscussionHistory();
-
history.setMaxChars(0);
-
-
-
muc.join(user, password, history,
-
SmackConfiguration.getPacketReplyTimeout());
-
Log.i("MultiUserChat", "会议室【"+roomsName+"】加入成功........");
-
return muc;
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
Log.i("MultiUserChat", "会议室【"+roomsName+"】加入失败........");
-
return null;
-
}
-
}
调用这个两个方法都可以获取到MultiUserChat,根据需求选择一个就可以。
利用MultiUserChat对象调用sendMessage()方法即可,很容易吧~
-
try {
-
multiUserChat.sendMessage(message);
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
2)接受消息
创建会议室消息监听器,这里我也单独写了一个类
-
package com.techrare.listener;
-
-
import org.jivesoftware.smack.PacketListener;
-
import org.jivesoftware.smack.packet.Message;
-
import org.jivesoftware.smack.packet.Packet;
-
-
-
-
-
-
-
-
public class TaxiMultiListener implements PacketListener {
-
-
@Override
-
public void processPacket(Packet packet) {
-
Message message = (Message) packet;
-
String body = message.getBody();
-
-
}
-
}
添加监听器,每个会议室聊天对象都要添加一个消息监听器,为了避免重复监听
应该把会议室聊天对象放在一个集合当中,需要用到的时候取出来用即可。
-
multiUserChat.addMessageListener(new TaxiMultiListener());
希望能够給正在学习的同学们一点点帮助!
阅读(2533) | 评论(0) | 转发(0) |