消息

发送消息

Unity SDK支持发送文本消息和文件消息两种格式的消息(单聊和群聊通用,只是一个参数的差别)。

发送文本

sendTxtMessageBtn.onClick.AddListener (delegate() {
    EMBaseCallback cb = new EMBaseCallback();
    cb.onSuccessCallback = () => {
        logText.text = "send message success";
    };
    cb.onProgressCallback = (progress,status) => {};
    cb.onErrorCallback = (code,message) => {};
    EMClient.Instance.SendTextMessage(txtContent.text, toUser.text, ChatType.Chat, cb);
});

发送文件消息

sendFileMessageBtn.onClick.AddListener (delegate() {
    if(filePath.text.Length > 0){
        EMBaseCallback cb = new EMBaseCallback();
        cb.onSuccessCallback = () => {
            logText.text = "send file success";
        };
        cb.onProgressCallback = (progress,status) => {};
        cb.onErrorCallback = (code,msg) => {};
        EMClient.Instance.SendFileMessage(filePath.text,toUser.text,ChatType.Chat,cb);
    }
});

接收消息

Unity SDK通过继承实现EMMessageListenerCallback类来完成消息的接收处理。

private void setMessageRecvListener()
{
    EMMessageListenerCallback receiveMessageCallback = new EMMessageListenerCallback();
    receiveMessageCallback.onMessageReceivedCallback = (msgs) => {
        logText.text = "from ";
        foreach(EMMessage msg in msgs) {
            logText.text += msg.mFrom;
            if(msg.mType == MessageType.TXT) {
                logText.text += ",content="+msg.mTxt+",ext="+msg.ext;
            }

            if(msg.mType == MessageType.IMAGE) {
                rawImage.gameObject.SetActive(true);
                rawImage.GetComponent<ShowPicture>().loadPic(msg.mRemotePath);
            }else{
                rawImage.gameObject.SetActive(false);
            }

            if(msg.mType == MessageType.VOICE || msg.mType == MessageType.FILE) {
                logText.text += msg.mRemotePath;
                EMBaseCallback cb = new EMBaseCallback();
                cb.onSuccessCallback = () => {
                    logText.text = "recv file success:" + msg.mLocalPath;
                };
                cb.onProgressCallback = (progress,status) => {};
                cb.onErrorCallback = (code,msg1) => {
                    logText.text = "recv file failure:" + msg.mLocalPath;
                };
                EMClient.Instance.DownloadAttachment(msg.mFrom, msg.mMsgId, cb);
            }
        }
    };
    receiveMessageCallback.onMessageReadAckReceivedCallback = (msgs) => {};
    receiveMessageCallback.onMessageDeliveryAckReceivedCallback = (msgs) => {};
    receiveMessageCallback.onMessageChangedCallback = (msgs) => {};
    receiveMessageCallback.onCmdMessageReceivedCallback = (msgs) => {};
    EMClient.Instance.receiveMessageCallback = receiveMessageCallback;
}

上一章节:Unity SDK基础功能

下一页:群组管理