[置顶] smack连接ejabberd服务笔记三

时间:2021-02-14 19:54:02

1、加好友

public boolean addFriendInEjabberd(String fromName, String toName) {
// TODO Auto-generated method stub
XMPPConnection connection = ConnectionFactory.getConnection();
Roster roster = connection.getRoster();
try {
roster.createEntry(fromName+"@"+ConfigUtils.domin, toName, null);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}


2、发起单聊

public boolean sendMessage(String messsage, String from, String to) {
XMPPConnection connection = XmppConnectionPools.getConnFromPools(Long.parseLong(from));
ChatManager chatManager = connection.getChatManager();
Message messages = new Message();
messages.setBody(messsage);
messages.setFrom(ConfigUtils.opUserName+(int)(Long.parseLong(from)%Integer.parseInt(ConfigUtils.connMax))+"@"+ConfigUtils.domin);
messages.setTo(to+"@"+ConfigUtils.domin);
Chat chat = chatManager.createChat(to+"@"+ConfigUtils.domin, new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.err.println("Received Message : " + message.getBody());
}
});
try {
chat.sendMessage(messages);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}


3、创建聊天室

public boolean createMutRoomInEjabberd(String roomKey, String roomName,
String[] inviteNames, boolean isTemp, long userId) {
XMPPConnection connection = null;
try {
connection=XmppConnectionPools.getConnFromPools(userId);
MultiUserChat muc = new MultiUserChat(connection, roomKey
+ ConfigUtils.roomSuffix);
muc.create(roomName);
for (String inviteName : inviteNames) {// 邀请人
muc.invite(inviteName+"@"+ConfigUtils.domin, "");
}
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());
}
}
if (isTemp) {// 临时的
submitForm.setAnswer("muc#roomconfig_persistentroom", false);
} else {
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
}
submitForm.setAnswer("muc#roomconfig_membersonly", false);
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
muc.sendConfigurationForm(submitForm);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}


4、邀请进入聊天室

public boolean inviteUserJoinRoom(String[] invitedNames, String reason,
String roomKey, long userId) {
XMPPConnection connection = null;
try {
connection = XmppConnectionPools.getConnFromPools(userId);
MultiUserChat muc = new MultiUserChat(connection, roomKey
+ ConfigUtils.roomSuffix);
for (String invitedName : invitedNames)
//批量邀请人
muc.invite(invitedName + "@" + ConfigUtils.domin, reason);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}


5、从聊天室踢人

public boolean kickUserFromRoom(String[] kickedNames, String reason,
String roomKey, long userId) {
XMPPConnection connection = null;
try {
connection = XmppConnectionPools.getConnFromPools(userId);
MultiUserChat muc = new MultiUserChat(connection, roomKey
+ ConfigUtils.roomSuffix);
for (String kickedName : kickedNames)
// 批量踢人
muc.kickParticipant(kickedName, reason);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}


6、发起群聊

@Override
public boolean sendGroupMessage(String message, String roomKey, long userId) {
// TODO Auto-generated method stub
XMPPConnection connection = null;
try {
connection = XmppConnectionPools.getConnFromPools(userId);
MultiUserChat muc = new MultiUserChat(connection, roomKey
+ ConfigUtils.roomSuffix);
muc.join("IM代理服务器");
muc.sendMessage(message);
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}