ChatConnect.java 5.29 KB
package com.cnlive.gundam.stream.util;

import android.content.Context;
import android.util.Log;

import com.cnlive.libs.util.chat.ChatUtil;
import com.cnlive.libs.util.chat.base.IChat;
import com.cnlive.libs.util.chat.model.CNChatRoomInfo;
import com.cnlive.libs.util.chat.model.CNMessage;
import com.cnlive.libs.util.chat.model.CNUserInfo;

/**
 * Created by Lynn on 5/23/2017.
 */

public class ChatConnect implements IChat.OnConnectStatusListener, IChat.OnReceiveMessageListener {
    private static final String TAG = "ChatConnect";

    public static final int ERROR_CONNECT = 1001;
    public static final int ERROR_CHAT = 1002;
    public static final int ERROR_DISCONNECT = 1003;

    public static final int STATE_CONNECT = 2001;
    public static final int STATE_JOIN = 2002;
    public static final int STATE_DISCONNECT = 2003;
    public static final int STATE_CONNECTING = 2004;

    private String userId;
    private String userName;
    private String headUrl;

    private String mRoomId;
    private Context mContext;
    private ChatCallback callback;
    private CNChatRoomInfo mCnChatRoomInfo;

    public ChatConnect(Context mContext, ChatCallback callback) {
        this.mContext = mContext;
        this.callback = callback;
    }

    public void connect(final String roomId, CNUserInfo cnUserInfo) {
        userId = cnUserInfo.getUserId();
        userName = cnUserInfo.getUserName();
        headUrl = cnUserInfo.getHeadUrl();

        Log.e(TAG, userId + " : " + userName + " : " + headUrl);
        this.mRoomId = roomId;

        ChatUtil.setOnReceiveMessageListener(this);
//        ChatUtil.setConnectStatusListener(this);
        ChatUtil.setOnConnectStatusListener(this);

        IChat.OnConnectListener onConnectListener = new IChat.OnConnectListener() {
            @Override
            public void onTokenIncorrect(String s) {
                if (callback != null) callback.onChatState(ERROR_CONNECT);
            }

            @Override
            public void onSuccess(String s) {
                if (callback != null) callback.onChatState(STATE_CONNECT);
                if (userId == null || userId == "") ChatUtil.modifyUser(userName, "");
                ChatUtil.joinChatRoom(mRoomId, 0, new IChat.OnOperationListener() {
                    @Override
                    public void onSuccess(int i, String s) {
                        if (callback != null) callback.onChatState(STATE_JOIN);
                    }

                    @Override
                    public void onError(int i, String s) {
                        if (callback != null) callback.onChatState(ERROR_CHAT);
                    }
                });
            }

            @Override
            public void onError(int i, String s) {
                if (callback != null) callback.onChatState(ERROR_CONNECT);
            }
        };

        if (userId != null || userId != "") {
            ChatUtil.connect(new CNUserInfo(String.valueOf(userId), userName, headUrl), onConnectListener);
        } else {
            ChatUtil.connect(onConnectListener);
        }
    }

    int viewCount = 0;

    public int getChatRoomInfo() {
        ChatUtil.getChatRoomInfo(mRoomId, 500, CNChatRoomInfo.ChatRoomMemberOrder.RC_CHAT_ROOM_MEMBER_ASC, new IChat.CNResultCallback<CNChatRoomInfo>() {
            @Override
            public void onSuccess(CNChatRoomInfo cnChatRoomInfo) {
                mCnChatRoomInfo = cnChatRoomInfo;
                viewCount = cnChatRoomInfo.getTotalMemberCount();
            }

            @Override
            public void onError(int i, String s) {
            }
        });
        return viewCount;
    }

    public void disconnect() {
        ChatUtil.quitChatRoom(mRoomId, new IChat.OnOperationListener() {
            @Override
            public void onSuccess(int i, String s) {
            }

            @Override
            public void onError(int i, String s) {
                if (callback != null) callback.onChatState(ERROR_DISCONNECT);
            }
        });
        ChatUtil.removeReceiveMessageListener(this);
        ChatUtil.removeConnectStatusListener(this);
        callback = null;
        mContext = null;
    }

    @Override
    public void onConnectStatus(int i, String s) {
        switch (i) {
            case IChat.CONNECTED:
                if (callback != null) callback.onChatState(STATE_CONNECT);
                break;
            case IChat.CONNECTING:
                if (callback != null) callback.onChatState(STATE_CONNECTING);
                break;
            case IChat.DISCONNECTED:
            case IChat.NETWORK_UNAVAILABLE:
                if (callback != null) callback.onChatState(STATE_DISCONNECT);
                break;
        }

    }

    @Override
    public void processMessage(CNMessage cnMessage) {

        if (mRoomId.equals(cnMessage.getTargetId())) {
//            String from = cnMessage.getUserInfo() == null ? "" : cnMessage.getUserInfo().getUserName();
//            String body = cnMessage.getContent();
//            String userId = ChatUtil.getCurrentUserInfo().getUserId();
            if (callback != null) callback.onChatMessage(cnMessage);
        }

    }

    public void setCallback(ChatCallback callback) {
        this.callback = callback;
    }

    public interface ChatCallback {
        void onChatState(int what);

        void onChatMessage(CNMessage cnMessage);
    }
}