Android如何配置XMPP会议 - 聊天室

时间:2015-07-02 05:42:43

标签: android tcp xmpp chat muc

我正在开发一个聊天应用程序,但它无法正常工作,给出不同的差异错误,如406或407,所以请建议我我的以下代码是正确与否,

应用程序启动时首次登录:

  public void LoginWithUser() {

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {

            SASLAuthentication.unregisterSASLMechanism("org.jivesoftware.smack.sasl.javax.SASLDigestMD5Mechanism");
            SmackInitialization initialization = new SmackInitialization();
            XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration.builder();

                config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
                config.setServiceName(Constants.SERVICE);
                config.setHost(Constants.HOST);
                config.setPort(Constants.PORT);
                config.setResource("myresource");
                config.setDebuggerEnabled(true);
                config.setKeystoreType("AndroidCAStore");
                config.setConnectTimeout(100000);

                try {

                    config.setUsernameAndPassword(getUserName(), password);
                } catch (Exception e) {

                    e.getMessage();
                }
                Constants.connection = new XMPPTCPConnection(config.build()); //new XMPPConnection(Constants.connConfig);
                try {

                    if (!Constants.connection.isConnected()) {
                        Constants.connection.connect();
                    }

                    Log.i("ChatActivity", "Connected to "
                            + Constants.connection.getHost());
                } catch (XMPPException ex) {
                    Log.e("ChatActivity",
                            "Failed to connect to " + Constants.connection.getHost());
                    Log.e("ChatActivity", ex.toString());
                    //  setConnection(null);

                } catch (IOException | SmackException e) {
                    e.printStackTrace();
                    Log.e("my error outer", e.getMessage() + " <- Understand 0 ? !!!");
                }
                try {
                    Log.d("chat : user name", getUserName());
                    try {
                        Log.d("chat : password", AESCrypt.decrypt(Constants.key_store_pair, enc_login_key));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {

                        SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
                        Constants.connection.login(getUserName(), password);
                        Constants.connection.getServiceName();
                        Log.d("service name=", Constants.connection.getServiceName());

                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("my error inner", e.getMessage() + " <- Understand 1 ? !!!");
                    }

                    Log.i("ChatActivity", "Logged in as    "
                            + Constants.connection.getUser());

                    Log.i("You are valid user",
                            "your Token is  " + Constants.connection.getUser());


                } catch (Exception ex) {


                }


            }

    });
    t.start();

}

成功连接......

当我在那时创建新会议室之后,我通过调用以下方法注册该会议室:

 private MultiUserChat createGroupChat(XMPPConnection connection,String  room_id, String groupName, String registered_beam_iddd) throws XMPPException, SmackException {

    // This code call when creating new conference room

    Constants.mucM = MultiUserChatManager.getInstanceFor(connection); //new MultiUserChatManager(connection, registered_beam_iddd + "@" + groupName);
    Constants.muc = Constants.mucM.getMultiUserChat(registered_beam_iddd + "@" + groupName);

    if(Constants.connection.isConnected())
    {


        if(Constants.connection.isAuthenticated()) {
            Constants.muc.createOrJoin(room_id + "@" + groupName);  //  

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

            Log.d("Room Created : Name : " , room_id + "@" + groupName);
        }
        else
        {
            //Toast.makeText(getActivity(),"Authenicated false",Toast.LENGTH_LONG).show();
            Log.d("ooo", "authentication failed in AddBeam");
        }
    }
    else
    {
        Toast.makeText(getActivity(),"Connection Loss",Toast.LENGTH_LONG).show();
    }
    return Constants.muc;
    //onesecond
}

当我点击会议室号码(listview)时,这意味着当我进入任何一个会议室时,我会按照以下方法来加入会议室并获取历史记录,

  private MultiUserChat joinGroupChat(XMPPConnection connection, String room_id, String groupName, String registered_beam_iddd) throws XMPPException, SmackException {

    // This code called when user enter in Chat-conference room

    if (Constants.connection.isConnected()) {

        if (Constants.muc == null) {
            Constants.mucM = MultiUserChatManager.getInstanceFor(connection); //new MultiUserChatManager(connection, registered_beam_iddd + "@" + groupName);
            Constants.muc = Constants.mucM.getMultiUserChat(registered_beam_iddd + "@" + groupName);
        }
        if (Constants.connection.isAuthenticated()) {

            if (!Constants.muc.isJoined()) {


                DiscussionHistory history = new DiscussionHistory();
                history.setMaxStanzas(20);

                Constants.muc.join(room_id + "@" + groupName, "password", history, Constants.connection.getPacketReplyTimeout());  //  @conference.tubsystems.com  



            } else {
                Log.d("joined: ", room_id + "@" + groupName);
            }
        } else {
            Log.d("ooo", "authentication failed in AddBeam");
        }
    } else {
        Toast.makeText(getApplicationContext(), "Connection Loss", Toast.LENGTH_LONG).show();
        try {

            Constants.connection.connect();
            Log.i("ChatActivity", "Connected to "
                    + Constants.connection.getHost());
        } catch (Exception ex) {
            Log.e("ChatActivity",
                    "Failed to connect to " + Constants.connection.getHost());
            Log.e("ChatActivity", ex.toString());
            //  setConnection(null);
        }
    }
    return Constants.muc;
    //onesecond
}

向会议发送消息:

    private void sendmessage(String text, String room) {

    String to = beamId + "@"+groupname;


    Message msg = new Message(to, Message.Type.groupchat);
    msg.setBody(text);
    if (Constants.connection != null) {
        try {
            Constants.connection.sendPacket(msg);
            Log.d("Send to room  : Name : ", to);
        } catch (Exception e) {
            Log.d("ooo", "msg exception" + e.getMessage());
        }

        messages.add(text);
        msg_send_receive_val = 1;

        new setListAdapter().execute();
     }

  }

但它给了一些时间连接错误,有些时间406或407错误,当我第一次进入会议室时,只有它显示过去的历史消息然后自动删除,并且消息也没有发送一些时间和一些时间发送,我不知道什么是问题,而发送消息它给406-407修改 - 不可接受的错误,有时给出其他错误。

我不知道上面的代码中缺少什么,或者配置会议室所需的其他内容?请尽可能快地帮助我。

提前致谢。

0 个答案:

没有答案