smack连接ejabberd服务笔记三

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;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值