目录

编译

编译工具

编译工具使用bazel

下载源码

git clone git@github.com:Xiaomi-mimc/mimc-cpp-sdk.git

编译库

cd mimc-cpp-sdk
bazel build :mimc_cpp_sdk

编译示例代码

即时消息

cd mimc-cpp-sdk
bazel build :mimc_cpp_demo

实时流

cd mimc-cpp-sdk
bazel build :rts_cpp_demo

编译测试用例

即时消息

cd mimc-cpp-sdk
bazel build :mimc_cpp_test

实时流

cd mimc-cpp-sdk
bazel build :rts_cpp_test

用户创建及初始化

注意:此流程务必全部走完,否则无法登录!

创建用户

/**
 * @param[appAccount]: 用户在APP帐号系统内的唯一帐号ID
 * @param[resource]: 用户设备的标识
 *    如不指定resource,SDK自动生成并支持多设备登录
 *    如开发者指定resource,则需支持多设备登录:
 *    同一台设备,resource要保持不变
 *    不同设备,resource要相互不同
 */
 User(std::string appAccount, std::string resource = "");

 #include <mimc/user.h>
 string appAccount1 = "LeiJun";
 User* user = new User(appAccount1);

注册回调函数

安全认证

参考安全认证文档实现:

class MIMCTokenFetcher {
public:
   /**
    * 同步访问代理认证服务(appProxyService),
    * 从代理认证服务返回结果中获取[小米认证服务下发的原始数据]并返回
    */
    virtual std::string fetchToken() = 0;
};

#include <mimc/tokenfetcher.h>
#include <curl/curl.h>
···
class TestTokenFetcher : public MIMCTokenFetcher {
public:
    string fetchToken() {
        curl_global_init(CURL_GLOBAL_ALL);
        CURL *curl = curl_easy_init();
        CURLcode res;
       /**
        * 当前示例代码直接访问小米认证服务(TokenService)
        * 实际生产环境上,应该访问代理认证服务(appProxyService)
        */
        const string url = "https://mimc.chat.xiaomi.net/api/account/token";
        const string body = "{\"appId\":\"" + this->appId + "\",\"appKey\":\"" + this->appKey + "\",\"appSecret\":\"" + this->appSecret + "\",\"appAccount\":\"" + this->appAccount + "\"}";
        string result;
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_POST, 1);
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            ···
            res = curl_easy_perform(curl);
            ···
            curl_easy_cleanup(curl);
        }

        curl_global_cleanup();

        return result;
    }
    TestTokenFetcher(string appId, string appKey, string appSecret, string appAccount)
    {
        this->appId = appId;
        this->appKey = appKey;
        this->appSecret = appSecret;
        this->appAccount = appAccount;
    }
private:
    string appId;
    string appKey;
    string appSecret;
    string appAccount;
};

user->registerTokenFetcher(new TestTokenFetcher(appId, appKey, appSecret, appAccount1));
用户在线状态变化通知
class OnlineStatusHandler {
public:
   /**
    * @param[status]: 登录状态,Online 在线,Offline 离线。
    * @param[errType]: 状态改变错误码
    * @param[errReason]: 状态改变错误原因
    * @param[errDescription]: 状态改变错误描述
    */
    virtual void statusChange(OnlineStatus status, std::string errType, std::string errReason, std::string errDescription) = 0;
};

#include <mimc/onlinestatus_handler.h>
#include <XMDLoggerWrapper.h>

class TestOnlineStatusHandler : public OnlineStatusHandler {
public:
    void statusChange(OnlineStatus status, string errType, string errReason, string errDescription) {
        XMDLoggerWrapper::instance()->info("In statusChange, status is %d, errType is %s, errReason is %s, errDescription is %s", status, errType.c_str(), errReason.c_str(), errDescription.c_str());
    }
};

user->registerOnlineStatusHandler(new TestOnlineStatusHandler());
消息通知
class MessageHandler {
public:
    //接收消息通知
    virtual void handleMessage(std::vector<MIMCMessage> packets) = 0;
    //发送消息成功或失败通知
    virtual void handleServerAck(std::string packetId, long sequence, long timestamp, std::string errorMsg) = 0;
    //发送消息超时通知
    virtual void handleSendMsgTimeout(MIMCMessage message) = 0;
};

#include <mimc/message_handler.h>
#include <mimc/threadsafe_queue.h>

class TestMessageHandler : public MessageHandler {
public:
    void handleMessage(std::vector<MIMCMessage> packets) {
        std::vector<MIMCMessage>::iterator it = packets.begin();
        for (; it != packets.end(); ++it) {
            messages.push(*it);
        }
    }
    void handleServerAck(std::string packetId, long sequence, long timestamp, std::string errorMsg) {
        packetIds.push(packetId);
    }
    void handleSendMsgTimeout(MIMCMessage message) {
        ···
    }
    MIMCMessage* pollMessage() {
        MIMCMessage *messagePtr;
        messages.pop(&messagePtr);
        return messagePtr;
    }
    std::string* pollServerAck() {
        std::string *packetIdPtr;
        packetIds.pop(&packetIdPtr);
        return packetIdPtr;
    }
private:
    ThreadSafeQueue<MIMCMessage> messages;
    ThreadSafeQueue<std::string> packetIds;
};

TestMessageHandler* messageHandler = new TestMessageHandler();
user->registerMessageHandler(messageHandler);

登录

user->login();

即时消息

发送消息

/**
 * @param[toAppAccount]: 消息接收者在APP帐号系统内的唯一帐号ID
 * @param[msg]: 开发者自定义消息体
 * @param[isStore]: 消息是否存储在mimc服务端,true 存储, false 不存储, 默认存储。
 * @return: 客户端生成的消息ID
 *    返回空串:消息未进入发送队列
 */
 std::string sendMessage(const std::string& toAppAccount, const std::string& msg, const bool isStore = true);

 string msg1 = "With MIMC, We can communicate with each other much easier!";
 string packetId_sent1 = user->sendMessage(toAppAccount, msg1);

接收消息

MIMCMessage *message;
while (message = messageHandler->pollMessage())
{
    LOG4CPLUS_INFO(LOGGER, "message from " << message->getFromAccount() << " to " << message->getToAccount() << " is " << message->getPayload());
}

实时流

发起会话

/**
 * @param[toAppAccount]: 接收方账号
 * @param[appContent]: 给接收方携带的业务自定义数据
 * @param[toResource]: 用户设备的标识
 * @return: 会话ID,唯一标识当前流会话
 */
 long dialCall(const std::string& toAppAccount, const std::string& appContent = "", const std::string& toResource = "");

发送数据

/**
 * @param[chatId]: 会话ID
 * @param[data]: 流数据
 * @param[dataType]: 数据类型,音频:AUDIO,视频:VIDEO
 * @param[channelType]: 通道类型,RELAY、P2P_INTERNET、P2P_INTRANET
 */
 bool sendRtsData(long chatId, const std::string& data, RtsDataType dataType, RtsChannelType channelType = RELAY);

关闭会话

/**
 * @param[chatId]: 会话ID
 * @param[byeReason]: 描述信息
 */
 void closeCall(long chatId, std::string byeReason = "");

实时流回调

class RTSCallEventHandler {
public:
   /**
    * 新会话接入回调
    * @param[chatId]: 会话ID
    * @param[fromAccount]: 发起方账号
    * @param[appContent]: 发起方携带的业务自定义数据
    * @param[fromResource]: 用户设备的标识
    * @return: LaunchedResponse,成员accepted表示是否接受会话,成员errmsg携带描述信息
    */
    virtual LaunchedResponse onLaunched(long chatId, const std::string& fromAccount, const std::string& appContent, const std::string& fromResource) = 0;

   /**
    * 会话被接通回调
    * @param[chatId]: 会话ID
    * @param[accepted]: 接收方同意ture,拒绝false
    * @param[errmsg]: 描述信息
    */
    virtual void onAnswered(long chatId, bool accepted, const std::string& errmsg) = 0;

   /**
    * 会话被关闭回调
    * @param[chatId]: 会话ID
    * @param[errmsg]: 描述信息
    */
    virtual void onClosed(long chatId, const std::string& errmsg) = 0;

   /**
    * 接收数据的回调
    * @param[chatId]: 会话ID
    * @param[data]: 流数据
    * @param[dataType]: 数据类型,AUDIO,VIDEO
    * @param[channelType]: 通道类型,RELAY、P2P_INTERNET、P2P_INTRANET
    */
    virtual void handleData(long chatId, const std::string& data, RtsDataType dataType, RtsChannelType channelType) = 0;
};

注销

user->logout();

示例代码

点击查看

更多功能,敬请期待!

results matching ""

    No results matching ""