コード例 #1
0
 public static MultiUserChat createConferenceRoom(
     ImConnection talkConnection, String roomName, List<String> memberList) throws XMPPException {
   try {
     MultiUserChat muc = getMultiUserChat(talkConnection, roomName);
     if (muc != null) {
       // 创建聊天室
       muc.join(talkConnection.getXMPPConnection().getUser());
       //                muc.create(roomName); // roomName房间的名字
       // 获得聊天室的配置表单
       Form form = muc.getConfigurationForm();
       // 根据原始表单创建一个要提交的新表单。
       Form submitForm = form.createAnswerForm();
       // 向要提交的表单添加默认答复
       for (Iterator<FormField> 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<String> owners = new ArrayList<String>();
       owners.add(talkConnection.getXMPPConnection().getUser()); // 用户JID
       submitForm.setAnswer("muc#roomconfig_roomowners", owners);
       // 设置聊天室是持久聊天室,即将要被保存下来
       submitForm.setAnswer("muc#roomconfig_persistentroom", true);
       // 房间仅对成员开放
       submitForm.setAnswer("muc#roomconfig_membersonly", false);
       // 允许占有者邀请其他人
       submitForm.setAnswer("muc#roomconfig_allowinvites", true);
       // 登录房间对话
       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);
       // 发送已完成的表单(有默认值)到服务器来配置聊天室
       submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
       // 添加群成员
       for (String tempMember : memberList) {
         muc.grantMembership(tempMember);
       }
       // 发送已完成的表单(有默认值)到服务器来配置聊天室
       muc.sendConfigurationForm(submitForm);
     }
     return muc;
   } catch (XMPPException e) {
     e.printStackTrace();
     throw new XMPPException("");
   }
 }
コード例 #2
0
ファイル: MucChannel.java プロジェクト: MasterLindi/bif4-itp
  public static void createMucChannel(
      String channelname, String channeltopic, int userid, int channelid) {
    try {
      if (!Application.mucchannels.containsKey(channelid)) {
        // create MUC
        MultiUserChat muc =
            new MultiUserChat(Application.conn, channelname + "@conference.webchat");
        System.out.println("Channelname: " + channelname);
        muc.create(channelname);

        Application.mucchannels.put(channelid, muc);

        Form form = muc.getConfigurationForm();
        Form submitForm = form.createAnswerForm();

        for (Iterator<FormField> 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<String> owners = new ArrayList<String>();
        owners.add("webchat@webchat");
        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        muc.sendConfigurationForm(submitForm);
        muc.invite(
            models.User.find.byId(userid).username + "@webchat/Smack",
            "Einladung in Channel " + channelname);
        final int chanid = channelid;
        muc.sendMessage("Willkommen: Topic lautet " + channeltopic + "!");
        muc.addMessageListener(
            new PacketListener() {
              @Override
              public void processPacket(Packet packet) {
                if (packet instanceof Message) {
                  String[] temp;
                  temp = ((Message) packet).getFrom().split("/");
                  if (!temp[1].equals(models.Channel.find.byId(chanid).name)) {
                    System.out.println("Received user: "******"@webchat/Smack",
            "Einladung in Channel " + channelname);
        muc.sendMessage("Willkommen: Topic lautet" + channeltopic + "!");
      }
    } catch (XMPPException exp) {
      exp.printStackTrace();
    }
  }