Пример #1
0
 @Override
 public void onMessageReceipt(
     AVIMTypedMessage message, AVIMConversation conversation, AVIMClient client) {
   if (client.getClientId().equals(chatManager.getSelfId())) {
     chatManager.onMessageReceipt(message);
   } else {
     client.close(null);
   }
 }
Пример #2
0
 @Override
 public void onMessage(
     AVIMTypedMessage message, AVIMConversation conversation, AVIMClient client) {
   if (chatManager.getSelfId() == null) {
     LogUtils.d("selfId is null, please call setupManagerWithUserId ");
   }
   if (client.getClientId().equals(chatManager.getSelfId())) {
     chatManager.onMessage(message, conversation, client);
   } else {
     // 收到其它的client的消息,可能是上一次别的client登录未正确关闭,这里关边掉。
     client.close(null);
   }
 }
Пример #3
0
 public static AVIMClient getImClient() {
   if (imClient == null) {
     imClient = getNewImClient(UserID);
   }
   imClient.getClientStatus(
       new AVIMClientStatusCallback() {
         @Override
         public void done(AVIMClient.AVIMClientStatus avimClientStatus) {
           if (avimClientStatus != AVIMClient.AVIMClientStatus.AVIMClientStatusOpened) {
             imClient.open(
                 new AVIMClientCallback() {
                   @Override
                   public void done(AVIMClient avimClient, AVIMException e) {
                     if (null != e) {
                       Log.e(TAG, "AVIMClient链接失败");
                       e.printStackTrace();
                     } else {
                       Log.i(TAG, "AVIMClient链接成功");
                     }
                   }
                 });
           }
         }
       });
   return imClient;
 }
Пример #4
0
 /**
  * 获取和 userId 的对话,先去服务器查之前两人有没创建过对话,没有的话,创建一个
  *
  * @param userId
  * @param callback
  */
 public void fetchConversationWithUserId(
     String userId, final AVIMConversationCreatedCallback callback) {
   final List<String> members = new ArrayList<>();
   members.add(userId);
   members.add(selfId);
   AVIMConversationQuery query = imClient.getQuery();
   query.withMembers(members);
   query.whereEqualTo(ConversationType.ATTR_TYPE_KEY, ConversationType.Single.getValue());
   query.orderByDescending(KEY_UPDATED_AT);
   query.limit(1);
   query.findInBackground(
       new AVIMConversationQueryCallback() {
         @Override
         public void done(List<AVIMConversation> conversations, AVIMException e) {
           if (e != null) {
             callback.done(null, e);
           } else {
             if (conversations.size() > 0) {
               callback.done(conversations.get(0), null);
             } else {
               Map<String, Object> attrs = new HashMap<>();
               attrs.put(ConversationType.TYPE_KEY, ConversationType.Single.getValue());
               imClient.createConversation(members, attrs, callback);
             }
           }
         }
       });
 }
Пример #5
0
  /**
   * 获取新的AVIMClient 并将旧的关闭
   *
   * @param userID 用户ID
   * @return AVIMClient
   */
  public static AVIMClient getNewImClient(String userID) {
    if (imClient != null) {
      imClient.close(null);
    }
    imClient = AVIMClient.getInstance(userID);

    imClient.open(
        new AVIMClientCallback() {
          @Override
          public void done(AVIMClient avimClient, AVIMException e) {
            if (null != e) {
              Log.e(TAG, "AVIMClient链接失败");
              e.printStackTrace();
            } else {
              Log.i(TAG, "AVIMClient链接成功");
            }
          }
        });
    return imClient;
  }
Пример #6
0
 /**
  * 连接聊天服务器,用 userId 登录,在进入MainActivity 前调用
  *
  * @param callback AVException 常发生于网络错误、签名错误
  */
 public void openClient(final AVIMClientCallback callback) {
   if (this.selfId == null) {
     throw new IllegalStateException("please call setupManagerWithUserId() first");
   }
   imClient = AVIMClient.getInstance(this.selfId);
   imClient.open(
       new AVIMClientCallback() {
         @Override
         public void done(AVIMClient avimClient, AVIMException e) {
           if (e != null) {
             setConnectAndNotify(false);
           } else {
             setConnectAndNotify(true);
           }
           if (callback != null) {
             callback.done(avimClient, e);
           }
         }
       });
 }
Пример #7
0
 @Override
 public void onMessage(AVIMMessage message, AVIMConversation conversation, AVIMClient client) {
   String clientId;
   clientId = AVImClientManager.getInstance().getClientId();
   if (client.getClientId().equals(clientId)) {
     if (!message.getFrom().equals(clientId)) {
       //				mChatAdapter.addData(message);
       EventBus.getDefault().post(new EventReceiveMessage(message, conversation));
     }
   }
 }
Пример #8
0
 /**
  * 请在应用一启动(Application onCreate)的时候就调用,因为 SDK 一启动,就会去连接聊天服务器 如果没有调用此函数设置 messageHandler ,就可能丢失一些消息
  *
  * @param context
  */
 public void init(Context context) {
   this.context = context;
   messageHandler = new MessageHandler();
   AVIMMessageManager.registerMessageHandler(AVIMTypedMessage.class, messageHandler);
   //    try {
   //      AVIMMessageManager.registerAVIMMessageType(AVIMUserInfoMessage.class);
   //    } catch (AVException e) {
   //      e.printStackTrace();
   //    }
   AVIMClient.setClientEventHandler(this);
   // 签名
   // AVIMClient.setSignatureFactory(new SignatureFactory());
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   String otherClientName = intent.getStringExtra("otherClientId");
   String conversationId = intent.getStringExtra("conversation");
   AVIMClient client = AVIMClient.getInstance(otherClientName);
   if (client == null) {
     gotoLoginActivity(context);
   } else {
     if (!TextUtils.isEmpty(conversationId)) {
       gotoChatActivity(context);
     }
   }
 }
Пример #10
0
  /**
   * 用户注销的时候调用,close 之后消息不会推送过来,也不可以进行发消息等操作
   *
   * @param callback AVException 常见于网络错误
   */
  public void closeWithCallback(final AVIMClientCallback callback) {
    imClient.close(
        new AVIMClientCallback() {

          @Override
          public void done(AVIMClient avimClient, AVIMException e) {
            if (e != null) {
              LogUtils.logException(e);
            }
            if (callback != null) {
              callback.done(avimClient, e);
            }
          }
        });
    imClient = null;
    selfId = null;
  }
Пример #11
0
 /**
  * 创建对话,为了不暴露 AVIMClient,这里封装一下
  *
  * @param members 成员,需要包含自己
  * @param attributes 对话的附加属性
  * @param callback AVException 聊天服务断开时抛出
  */
 public void createConversation(
     List<String> members,
     Map<String, Object> attributes,
     AVIMConversationCreatedCallback callback) {
   imClient.createConversation(members, attributes, callback);
 }
Пример #12
0
 /**
  * 获取 AVIMConversationQuery,用来查询对话
  *
  * @return
  */
 public AVIMConversationQuery getConversationQuery() {
   return imClient.getQuery();
 }