Openfire 单人聊天和多人聊天(发送消息、接收消息)
1、单人聊天java
1)发送消息:json
首先要获取一个聊天窗口,getConnection()为获取链接connection的方法,调用getFriendChat()获取c#
-
private Map<String, Chat> chatManage = new HashMap<String, Chat>();
-
-
-
-
-
-
-
-
public Chat getFriendChat(String friend, MessageListener listenter) {
-
if(getConnection()==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);
-
-
friend为好友名,不是JID;listener 监听器能够传null,利用聊天窗口对象调用sendMessage发送消息服务器
这里sendMessage我传的是一个JSON字符串,以便更灵活的控制,发送消息完成,很简单吧~ide
-
Chat chat = getFriendChat(friend,
null);
-
-
String msgjson =
"{\"messageType\":\""+messageType+"\",\"chanId\":\""+chanId+"\",\"chanName\":\""+chanName+"\"}";
-
chat.sendMessage(msgjson);
-
}
catch (XMPPException e) {
-
-
2)接受消息:spa
建立一个消息监听器,这里我单独写了一个类.net
-
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());
-
-
-
-
String body = msg.getBody();
-
boolean left = body.substring(0, 1).equals("{");
-
boolean right = body.substring(body.length()-1, body.length()).equals("}");
-
-
-
JSONObject obj =
new JSONObject(body);
-
String type = obj.getString(
"messageType");
-
String chanId = obj.getString(
"chanId");
-
String chanName = obj.getString(
"chanName");
-
}
catch (JSONException e) {
-
-
-
-
-
-
-
-
添加监听,最好是放在登陆方法中,在关闭链接方法中,移除监听,缘由是为了不重复添加监听,接受重复消息code
退出程序应该关闭链接,移除监听,该监听能够接受全部好友的消息,很方便吧~orm
-
TaxiChatManagerListener chatManagerListener =
new TaxiChatManagerListener();
-
getConnection().getChatManager().addChatListener(chatManagerListener);
connection.getChatManager().removeChatListener(chatManagerListener);
2、多人聊天(会议室)对象
1)、发送消息
首先要获取会议室对象(MultiUserChat),有两种方法获取
两个方法前面都有讲到,这里再回顾一下
一、建立会议室
-
-
-
-
-
-
public MultiUserChat createRoom(String user, String roomName,String password) {
-
if (getConnection() == null)
-
-
-
MultiUserChat muc =
null;
-
-
-
muc =
new MultiUserChat(getConnection(), roomName + "@conference."
-
+ getConnection().getServiceName());
-
-
-
-
Form form = muc.getConfigurationForm();
-
-
Form submitForm = form.createAnswerForm();
-
-
for (Iterator<FormField> fields = form.getFields(); fields
-
-
FormField field = (FormField) fields.next();
-
if (!FormField.TYPE_HIDDEN.equals(field.getType())
-
&& field.getVariable() !=
null) {
-
-
submitForm.setDefaultAnswer(field.getVariable());
-
-
-
-
List<String> owners =
new ArrayList<String>();
-
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",
-
-
-
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) {
-
-
-
-
-
二、加入会议室,
-
-
-
-
-
-
-
-
-
-
-
public MultiUserChat joinMultiUserChat(String user, String roomsName,
-
-
if (getConnection() == null)
-
-
-
-
MultiUserChat muc =
new MultiUserChat(getConnection(), roomsName
-
+
"@conference." + getConnection().getServiceName());
-
-
DiscussionHistory history =
new DiscussionHistory();
-
-
-
-
muc.join(user, password, history,
-
SmackConfiguration.getPacketReplyTimeout());
-
Log.i(
"MultiUserChat", "会议室【"+roomsName+"】加入成功........");
-
-
}
catch (XMPPException e) {
-
-
Log.i(
"MultiUserChat", "会议室【"+roomsName+"】加入失败........");
-
-
-
调用这个两个方法均可以获取到MultiUserChat,根据需求选择一个就能够。
利用MultiUserChat对象调用sendMessage()方法便可,很容易吧~
-
-
multiUserChat.sendMessage(message);
-
}
catch (XMPPException e) {
-
-
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 {
-
-
-
public void processPacket(Packet packet) {
-
Message message = (Message) packet;
-
String body = message.getBody();
-
-
-
添加监听器,每一个会议室聊天对象都要添加一个消息监听器,为了不重复监听
应该把会议室聊天对象放在一个集合当中,须要用到的时候取出来用便可。
multiUserChat.addMessageListener(new TaxiMultiListener());https://blog.csdn.net/qq_17169057/article/details/50437925