//消息监听类头文件
class ChatListener : public EMChatManagerListener {
public:
ChatListener()
{
}
virtual void onReceiveMessages(const EMMessageList &messages);
private:
EMCallbackObserverHandle m_coh;
void onTextMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType);
void onFileMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType);
void onImageMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType);
void onVoiceMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType);
void onVideoMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType);
void onLocationMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType);
string getJSHead(const EMMessagePtr msg, string sChatType, string JSFuncName);
string getJSTail(const EMMessageBodyPtr _body, string type);
void CallJSWithoutFilePath(string strJSHead, string strJSTail);
void CallJSWithFilePath(string strJSHead, string strJSTail, string strPath);
};
...
...
//消息监听类实现文件
void ChatListener::onReceiveMessages(const EMMessageList &messages)
{
HANDLE hObject[2];
hObject[0] = Utils::g_RosterDownloaded;
hObject[1] = Utils::g_GroupListDownloaded;
WaitForMultipleObjects(2, hObject, TRUE, INFINITE);
for (EMMessagePtr msg : messages)
{
EMMessage::EMChatType type = msg->chatType();
string sChatType = "chat";
if (type == EMMessage::GROUP)
{
sChatType = "groupchat";
}
else if (type == EMMessage::CHATROOM)
{
sChatType = "chatroom";
}
const vector<EMMessageBodyPtr> &bodies = msg->bodies();
const EMMessageBodyPtr _body = bodies[0];
switch (_body->type())
{
case EMMessageBody::TEXT:
{
onTextMessage(msg, _body, sChatType);
break;
}
case EMMessageBody::FILE:
{
onFileMessage(msg, _body, sChatType);
break;
}
case EMMessageBody::IMAGE:
{
onImageMessage(msg, _body, sChatType);
break;
}
case EMMessageBody::VOICE:
{
onVoiceMessage(msg, _body, sChatType);
break;
}
case EMMessageBody::COMMAND:
break;
case EMMessageBody::VIDEO:
{
onVideoMessage(msg, _body, sChatType);
break;
}
case EMMessageBody::LOCATION:
{
onLocationMessage(msg, _body, sChatType);
break;
}
}
}
}
void ChatListener::onTextMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType)
{
EMTextMessageBodyPtr body = std::dynamic_pointer_cast<EMTextMessageBody, EMMessageBody>(_body);
std::stringstream stream;
stream << "Demo.conn.onTextMessage('{id: \"";
stream << msg->msgId();
stream << "\",type : \"";
stream << sChatType;
stream << "\", from : \"";
stream << msg->from();
stream << "\",to : \"";
stream << msg->to();
stream << "\",data : \"";
stream << Utils::URLEncode(body->text());
stream << "\",ext : \"\"}');";
Utils::CallJS(stream);
}
void ChatListener::onFileMessage(const EMMessagePtr msg, const EMMessageBodyPtr _body, string sChatType)
{
EMFileMessageBodyPtr body = std::dynamic_pointer_cast<EMFileMessageBody, EMMessageBody>(_body);
string strJSHead = getJSHead(msg, sChatType,"onFileMessage");
string strJSTail = getJSTail(_body, "file");
CallJSWithoutFilePath(strJSHead, strJSTail);
EMCallbackPtr msgCallback(new EMCallback(m_coh,
[=](void)->bool
{
if (EMFileMessageBody::SUCCESSED == body->downloadStatus())
{
CallJSWithFilePath(strJSHead, strJSTail, GetPathForWebPage(body->localPath()));
}
return true;
},
[=](const easemob::EMErrorPtr)->bool
{
return false;
},
[](int){}));
msg->setCallback(msgCallback);
g_client->getChatManager().downloadMessageAttachments(msg);
}
...
...
//注册消息监听类
ChatListener *mChatListener;
mChatListener = new ChatListener();
g_client->getChatManager().addListener(mChatListener);