最近由于需求需要建聊天室,后來使用了leancloud來做,這個以前免費的,后來企業(yè)級要收費了,具體的可以看官網(wǎng)
這里主要是建立聊天室,消息存儲那些都是一次性的,所以這里主要講幾個方法,從建立聊天室,發(fā)送一條默認(rèn)的消息,到別人加入聊天室,發(fā)送消息
github代碼鏈接
首先是建立聊天室:
接入leancloud,只需要看管方的流程就可以完成了(這里是接入成功以后的使用)
public void createChatRoom() {
tom = AVIMClient.getInstance("Tom");
tom.open(new AVIMClientCallback() {
@Override
public void done(AVIMClient client, AVIMException e) {
if (e == null) {
//登錄成功
//創(chuàng)建一個 名為 "PK" 的暫態(tài)對話
client.createConversation(Collections.emptyList(), "PK", null, true,
new AVIMConversationCreatedCallback() {
@Override
public void done(AVIMConversation conv, AVIMException e) {
if (e == null) {
//獲得一個 Id conv.getConversationId().惟一room的憑證
//記得打印出來,這個數(shù)是隨機生成的,記下來,之后加入這個聊天室要用 就是roomId
mConverId = conv.getConversationId();
sendMessage(mConverId,"創(chuàng)建聊天室成功");
Log.i("----conv", conv.getConversationId());
TomQueryWithLimit();
} else {
Log.i("----Fail", e.toString());
}
}
});
}
}
});
}
以上代碼建立了聊天室,并且獲取到了getConversationId 這個id,這是一個聊天室惟一的憑證,
sendMessage(mConverId,"創(chuàng)建聊天室成功")和TomQueryWithLimit(),分別為發(fā)送第一條消息,和獲取人數(shù)的方法
代碼如下:
/**
* 進(jìn)入聊天室后,發(fā)送一條消息
*
* @param
*/
private void sendMessage(String sId, String msg) {
//這是第一個人.創(chuàng)建聊天室的人
AVIMConversation avimConversation = tom.getConversation(sId);//由id,得到聊天室的對象,發(fā)送消息,接收消息
AVIMTextMessage avimTextMessage = new AVIMTextMessage();
Map<String, Object> map = new HashMap<>();
avimTextMessage.setAttrs(map);
avimTextMessage.setText(msg);
avimConversation.sendMessage(avimTextMessage, new AVIMConversationCallback() {
@Override
public void done(AVIMException e) {
if (e == null) {
Log.i("發(fā)送成功", msg);
} else {
e.printStackTrace();
}
}
});
}
獲取人數(shù):
//獲取會話人數(shù)
private void TomQueryWithLimit() {
AVIMClient tom = AVIMClient.getInstance("Tom");
tom.open(new AVIMClientCallback() {
@Override
public void done(AVIMClient client, AVIMException e) {
if (e == null) {
//登錄成功
AVIMConversationQuery query = tom.getQuery();
query.setLimit(1);
//獲取第一個對話
query.findInBackground(new AVIMConversationQueryCallback() {
@Override
public void done(List<AVIMConversation> convs, AVIMException e) {
if (e == null) {
if (convs != null && !convs.isEmpty()) {
AVIMConversation conv = convs.get(0);
//獲取第一個對話的
conv.getMemberCount(new AVIMConversationMemberCountCallback() {
@Override
public void done(Integer count, AVIMException e) {
if (e == null) {
Log.i("-----count", "conversation got " + count + " members");
}
}
});
}
}
}
});
}
}
});
}
加入聊天室的方法如下:
public void joinRoom() {
//這是jerry加入聊天室
jerry = AVIMClient.getInstance("Jerry");
//建立連接
jerry.open(new AVIMClientCallback() {
@Override
public void done(AVIMClient avimClient, AVIMException e) {
//獲取聊天室
AVIMConversation avimConversation = jerry.getConversation(mConverId);
//加入聊天室
avimConversation.join(new AVIMConversationCallback() {
@Override
public void done(AVIMException e) {
if (e == null) {
//獲取聊天室人數(shù)
Toast.makeText(ChatActivity.this, "joinsuccess", Toast.LENGTH_SHORT).show();
//發(fā)送一條默認(rèn)消息,類型為MSG_NEWPEOPLE
sendMessages(mConverId, "jerry加入聊天室了");
} else {
Log.i("----roomjoinFail", e.toString());
Toast.makeText(ChatActivity.this, "roomjoinFail", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
加入了聊天室,并且向聊天室發(fā)送了一條消息
發(fā)送消息都是一樣的,這里需要獲取convenientId ,由這個Id獲取到聊天室的對象
sendMessages(mConverId, "jerry加入聊天室了")
private void sendMessages(String roomId, String msg) {
//這是加入聊天室的人
AVIMConversation avimConversation = jerry.getConversation(roomId);//由id,得到聊天室的對象,發(fā)送消息,接收消息
AVIMTextMessage avimTextMessage = new AVIMTextMessage();
Map<String, Object> map = new HashMap<>();
map.put("msg", "attr message");//發(fā)送附加消息,頭像啥的都可以
avimTextMessage.setAttrs(map);
avimTextMessage.setText(msg);
avimConversation.sendMessage(avimTextMessage, new AVIMConversationCallback() {
@Override
public void done(AVIMException e) {
if (e == null) {
Log.i("發(fā)送成功", msg);
} else {
e.printStackTrace();
}
}
});
}
其中聊天室主要由 mConverId = conv.getConversationId();這里獲取到的id將所有人聯(lián)系起來,這樣才能加入到一起
消息獲取
消息獲取和官網(wǎng)提供的方法一樣在APPLICATION里面,全局的獲取
可以查看我的MyApp類 (MyApp extends Application)
public static class CustomMessageHandler extends AVIMMessageHandler {
//接收到消息后的處理邏輯
@Override
public void onMessage(AVIMMessage message, AVIMConversation conversation, AVIMClient client){
if(message instanceof AVIMTextMessage){
String from = ((AVIMTextMessage) message).getFrom();
String name = (String)conversation.getAttribute("name");
// Log.d("aa & bb",((AVIMTextMessage)message).getText());
EventBus.getDefault().post(new OnEventMessage(((AVIMTextMessage)message).getText(),message));
}
}
public void onMessageReceipt(AVIMMessage message,AVIMConversation conversation,AVIMClient client){
}
}
消息的獲取用了EventBus來傳對象,如下:
/獲到消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(OnEventMessage event) {/* Do something */
AVIMMessage avimMessage = event.avimMessage;
String from = avimMessage.getFrom();
Map<String, Object> attrs = ((AVIMTextMessage) avimMessage).getAttrs();
String attsMsg = (String) attrs.get("msg");//獲取到的附加消息
String chatMessage = ((AVIMTextMessage) avimMessage).getText();//獲取到的文本消息
Log.i("------msg",chatMessage+"---"+attsMsg);
Toast.makeText(ChatActivity.this, chatMessage + "==" + attsMsg, Toast.LENGTH_SHORT).show();
mList.add(chatMessage);
mItemAdapter.notifyDataSetChanged();
mRvChatlist.smoothScrollToPosition(mList.size()-1);
}
以上就能獲得消息了,具體的需要實際操作才能理解,只是做了一些簡單的操作,可以建立聊天室,并且加入,實現(xiàn)雙方的聊天功能
這里是銷毀聊天室:
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
if (tom == null) {
return;
}
AVIMConversation avimConversation = tom.getConversation(mConverId);
avimConversation.quit(new AVIMConversationCallback() {
@Override
public void done(AVIMException e) {
if (e == null) {
}
tom.close(new AVIMClientCallback() {
@Override
public void done(AVIMClient avimClient, AVIMException e) {
if (e == null) {
}
}
});
}
});
}