目录

消息


发送消息

发送文本、语音、图片、位置等消息(单聊/群聊/聊天室通用,只是一个参数的差别)。

发送文本消息

//创建消息体
	EMMessageBodyPtr body = EMTextMessageBodyPtr(new EMTextMessageBody(content.c_str()));

	CefStringUTF8 utf8(content);
	//聊天类型:单聊
	EMMessage::EMChatType type = EMMessage::SINGLE;
	if (isGoupChat.compare("groupchat") == 0)
	{
		if (roomType.compare("true") == 0)
		{
			type = EMMessage::CHATROOM;//聊天室
		}
		else
		{
			type = EMMessage::GROUP;//群聊
		}
	}
	//创建待发消息
	EMMessagePtr msg = EMMessage::createSendMessage(g_client->getLoginInfo().loginUser(), to,
		body, type);
	EMCallbackPtr msgCallback(new EMCallback(m_coh,
		[=](void)->bool
	{
		string enc = URLEncode(utf8);
		callback->Success(enc.c_str());
		return true;
	},
		[=](const easemob::EMErrorPtr)->bool
	{
		return false;
	},
		[](int){}));
	//设置消息回调
	msg->setCallback(msgCallback);
	//发送消息
	g_client->getChatManager().sendMessage(msg);

发送文件类消息

OPENFILENAME ofn;       // common dialog box structure
	TCHAR szFile[260];       // buffer for file name
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = ::GetDesktopWindow();
	ofn.lpstrFile = szFile;
	ofn.lpstrFile[0] = '\0';
	ofn.nMaxFile = sizeof(szFile);
	ofn.lpstrFilter = L"所有文件(*.*)\0*.*\0\0";
	if (type.compare("img") == 0)
	{
		ofn.lpstrFilter = L"图像文件(*.bmp;*.jpg;*.png;*.gif)\0*.bmp;*.jpg;*.png;*.gif\0\0";
	}
	else if (type.compare("aud") == 0)
	{
		ofn.lpstrFilter = L"音频文件(*.mp3;*.amr;*.wmv)\0*.mp3;*.amr;*.wmv\0\0";
	}
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = NULL;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
	//打开系统文件对话框
	if (GetOpenFileName(&ofn) == TRUE)
	{
		//获取文件路径
		std::wstring sAttach = ofn.lpstrFile;
		CefStringUTF8 utf8(sAttach);
		//根据消息类型创建不同类型的消息体
		EMMessageBodyPtr body;
		if (type.compare("aud") == 0)
		{
			body = EMFileMessageBodyPtr(new EMFileMessageBody(utf8.ToString().c_str()));
		}
		else if (type.compare("img") == 0)
		{
			body = EMImageMessageBodyPtr(new EMImageMessageBody(utf8.ToString().c_str(), ""));
		}
		else if (type.compare("file") == 0)
		{
			body = EMFileMessageBodyPtr(new EMFileMessageBody(utf8.ToString().c_str()));
		}
		//聊天类型
		EMMessage::EMChatType chatType = EMMessage::SINGLE;
		if (isGoupChat.compare("groupchat") == 0)
		{
			if (roomType.compare("true") == 0)
			{
				chatType = EMMessage::CHATROOM;
			}
			else
			{
				chatType = EMMessage::GROUP;
			}
		}
		//创建消息
		EMMessagePtr msg = EMMessage::createSendMessage(g_client->getLoginInfo().loginUser(), to,
			body, chatType);
		EMCallbackPtr msgCallback(new EMCallback(m_coh,
			[=](void)->bool
		{
			string enc = URLEncode(utf8);
			callback->Success(enc.c_str());
			return true;
		},
			[=](const easemob::EMErrorPtr)->bool
		{
			return false;
		},
			[](int){}));
		//设置消息回调
		msg->setCallback(msgCallback);
		//发送消息
		g_client->getChatManager().sendMessage(msg);
	}

接收消息

//消息监听类头文件
 
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);


下一页:好友管理