====== Windows SDK 集成说明 ====== ---- Windows SDK 是为在微软Windows操作系统中集成 IM 功能提供的 SDK。使用C++语言开发,编译器需支持C++11。用户可以用 SDK 实现 IM 功能。目前支持登录、注册、单聊、群聊、聊天室、文本消息、图片、语音、位置等消息以及透传消息,还可以实现好友管理、群组管理等功能。未实现语音通话和视频通话功能。 ===== 集成准备 ===== 从[[http://www.easemob.com/download/im|环信官方网站]]获取 IM Windows SDK,SDK中目录结构如下: |-include |-bin |-lib |-doc release 目录中包含SDK的头文件、lib文件和dll文件,其中easemob_d.lib和easemob_d.dll是Debug版,easemob.pdb是SDK的调试符号文件,libcurl.dll是libcurl库二进制文件。doc 目录包含对 sdk 的详细说明。 ===== 集成示例 ===== ==== 创建 EMClient ==== EMClient 是 IM 服务的入口,可以直接调用 EMClient 的接口,也可以通过 EMClient 获得相应 EMContactManager、EMChatManager 等进行相应的操作。 void CreateEMClient() { if (g_client == NULL) { CString strAppDir = GetAppDataPath() + L"\\EasemobDemo"; CefString sAppDir(strAppDir); CreateDirectory(strAppDir, NULL); easemob::EMChatConfigsPtr configs(new easemob::EMChatConfigs(sAppDir, sAppDir, "easemob-demo#chatdemoui")); configs->setOs(EMChatConfigs::OS_MSWIN); configs->setEnableConsoleLog(true); configs->setAutoAcceptGroup(false); configs->setClientResource("windows"); configs->setLogLevel(EMChatConfigs::DEBUG_LEVEL); EMClient *client = EMClient::create(configs); g_client = client; mChatListener = new ChatListener(); g_client->getChatManager().addListener(mChatListener); mContactListener = new ContactListener(); g_client->getContactManager().registerContactListener(mContactListener); mConnectionListener = new ConnectionListener(); g_client->addConnectionListener(mConnectionListener); mGroupManagerListener = new GroupManagerListener(); g_client->getGroupManager().addListener(mGroupManagerListener); } } ==== 注册、登录及登出 ==== 调用 chatclient 的 createAccount()、login()、logout() 接口可以实现注册、登录、登出功能。 注册的示例: easemob::EMErrorPtr result = chatClient->createAccount("zhangsan", "passw0rd"); if(result->mErrorCode == EMError::NO_ERROR) { //注册成功 } else { //注册失败提示错误描述 cout << result->mDescription << endl; } 登录的示例 : CreateEMClient(); ... ... easemob::EMErrorPtr result = chatClient->login("zhangsan", "passw0rd"); if(result->mErrorCode == EMError::NO_ERROR) { //登录成功 } else { //登录失败提示错误描述 cout << result->mDescription << endl; } 登出的示例: g_client->logout(); ... ... g_client->getChatManager().removeListener(mChatListener); g_client->getContactManager().removeContactListener(mContactListener); g_client->removeConnectionListener(mConnectionListener); g_client->getGroupManager().removeListener(mGroupManagerListener); g_client->logout(); delete mConnectionListener; mConnectionListener = NULL; delete mContactListener; mContactListener = NULL; delete mChatListener; mChatListener = NULL; delete mGroupManagerListener; mGroupManagerListener = NULL; delete g_client; g_client = NULL; **注意:**createAccount(), login() 是需要与后台服务器通讯的操作,可能需要一定时间,如果程序想同时显示 UI 的话,需要放在单独线程中处理。 ===== 注册连接状态监听 ===== 当掉线时,SDK 会自动重连,无需进行任何操作,通过注册连接监听来知道连接状态。 * 在聊天过程中难免会遇到网络问题,在此 SDK 为您提供了网络监听接口,实时监听。 * 可以根据 disconnect 返回的 error 判断原因。若服务器返回的参数值为''EMError.USER_LOGIN_ANOTHER_DEVICE'',则认为是有同一个账号异地登录;若服务器返回的参数值为''EMError.USER_REMOVED'',则是账号在后台被删除。 class ConnectionListener : public EMConnectionListener { public: ConnectionListener() { } virtual void onDisconnect(EMErrorPtr error); virtual void onConnect(); }; ... ... mConnectionListener = new ConnectionListener(); g_client->addConnectionListener(mConnectionListener); ---- 上一章:[[im:applet:intro|微信小程序集成]] 下一页:[[im:windows:basics:message|消息]]