quickbloxquickblox入门


备注

本节概述了quickblox是什么,以及开发人员为什么要使用它。

它还应该提到quickblox中的任何大型主题,并链接到相关主题。由于quickblox的文档是新的,您可能需要创建这些相关主题的初始版本。

创建新的聊天对话框(私人)

QBChatDialog dialog = DialogUtils.buildPrivateDialog("USER_ID of other user");
        

        QBRestChatService.createChatDialog(dialog).performAsync(new QBEntityCallback<QBChatDialog>() {
            @Override
            public void onSuccess(QBChatDialog result, Bundle params) {
              //if dialog created successfully
              //result param return all details about that dialog
            }

            @Override
            public void onError(QBResponseException responseException) {
              //error creating dialog 
            }
        });
 

导入quickblox聊天Android SDK

添加存储库

repositories {
    maven {
        url "https://github.com/QuickBlox/quickblox-android-sdk-releases/raw/master/"
    }
}
 

在Project Gradle中添加聊天功能

        dependencies {
               compile("com.quickblox:quickblox-android-sdk-chat:2.6.1")
             }
 

在Project Gradle中添加视频功能

dependencies {
compile "com.quickblox:quickblox-android-sdk-videochat-webrtc:2.6.1"
 }
 

安装或设置

有关设置或安装quickblox的详细说明。

转到https://admin.quickblox.com并点击顶部的“注册”或点击链接: https//admin.quickblox.com/register

登录聊天

使用用户创建会话并登录QuickBlox聊天

// Initialise Chat service
QBChatService chatService = QBChatService.getInstance();
 
final QBUser user = new QBUser("garrysantos", "garrysantospass");
QBAuth.createSession(user, new QBEntityCallback<QBSession>() {
    @Override
    public void onSuccess(QBSession session, Bundle params) {
        // success, login to chat
 
        user.setId(session.getUserId());
 
        chatService.login(qbUser, new QBEntityCallback() {
            @Override
            public void onSuccess() {
 
            }
 
            @Override
            public void onError(QBResponseException errors) {
 
            }
        });
    }
 
    @Override
    public void onError(QBResponseException errors) {
 
    }
});
 

要处理不同的连接状态,请使用ConnectionListener:

ConnectionListener connectionListener = new ConnectionListener() {
    @Override
    public void connected(XMPPConnection connection) {
 
    }
 
    @Override
    public void authenticated(XMPPConnection connection) {
 
    }
 
    @Override
    public void connectionClosed() {
 
    }
 
    @Override
    public void connectionClosedOnError(Exception e) {
        // connection closed on error. It will be established soon
    }
 
    @Override
    public void reconnectingIn(int seconds) {
 
    }
 
    @Override
    public void reconnectionSuccessful() {
 
    }
 
    @Override
    public void reconnectionFailed(Exception e) {
 
    }
};
 
QBChatService.getInstance().addConnectionListener(connectionListener);
 

准备聊天服务

要初始化聊天服务使用:

QBChatService.setDebugEnabled(真); //启用聊天记录

QBChatService.setDefaultPacketReplyTimeout(10000); //为连接数据包设置回复超时(以毫秒为单位)。可用于登录,加入对话等事件,以便在网络较慢时增加服务器的等待响应时间。

要配置聊天套接字,请使用QBChatService.ConfigurationBuilder;

QBChatService.ConfigurationBuilder chatServiceConfigurationBuilder = new QBChatService.ConfigurationBuilder();
chatServiceConfigurationBuilder.setSocketTimeout(60); //Sets chat socket's read timeout in seconds
chatServiceConfigurationBuilder.setKeepAlive(true); //Sets connection socket's keepAlive option.
chatServiceConfigurationBuilder.setUseTls(true); //Sets the TLS security mode used when making the connection. By default TLS is disabled.
QBChatService.setConfigurationBuilder(chatServiceConfigurationBuilder);
 

登录用户对话请求列表

用于从登录用户的Quickblox服务器接收聊天对话框的代码(使用listview的示例)

private void receiveChatList() {
QBRequestGetBuilder requestBuilder = new QBRequestGetBuilder();
requestBuilder.setLimit(100);

QBRestChatService.getChatDialogs(null, requestBuilder).performAsync(
        new QBEntityCallback<ArrayList<QBChatDialog>>() {
            @Override
            public void onSuccess(final ArrayList<QBChatDialog> result, Bundle params) {
                int totalEntries = params.getInt("total_entries");
                Log.wtf("chat",""+result);
                TrumeMsgAdapter adapter=new TrumeMsgAdapter(this,result);
                chatlistView.setAdapter(adapter);
                chatlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        startActivity(new Intent(this,ChatingActivity.class).putExtra("dialog",result.get(position)));
                    }
                });

            }
            @Override
            public void onError(QBResponseException responseException) {

            }
        });
 

}

适配器代码: -

    public class TrumeMsgAdapter extends BaseAdapter {

private ArrayList<QBChatDialog> chatlist;
private Context context;

public TrumeMsgAdapter(Context c,ArrayList<QBChatDialog> chatlist){
    this.chatlist=chatlist;
    this.context=c;
}
@Override
public int getCount() {
    return chatlist.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View List;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        List = inflater.inflate(R.layout.trume_msg_adapter, null);
        TextView username=(TextView) List.findViewById(R.id.UserName);
        TextView lastmessage=(TextView)List.findViewById(R.id.lastmessage);
        username.setText(chatlist.get(position).getName());
        lastmessage.setText(chatlist.get(position).getLastMessage());
    } else {
        List = convertView;
        TextView username=(TextView) List.findViewById(R.id.UserName);
        TextView lastmessage=(TextView)List.findViewById(R.id.lastmessage);
        username.setText(chatlist.get(position).getName());
        lastmessage.setText(chatlist.get(position).getLastMessage());
    }

    return List;
}
}