• 目录
  • C#应用配置修改
  • 用户初始化
  • 安全认证
    • 参考 详细文档安全认证
  • 登录
  • 在线状态变化回调
  • 发送单聊消息
  • 发送群聊消息
  • 接收消息回调
  • 注销

    目录

    • C#应用配置修改
    • 用户初始化
    • 安全认证
    • 登录
    • 在线状态变化回调
    • 发送单聊消息
    • 接收消息回调
    • 注销
    • 更新日志

    C#应用配置修改

    1. 1、可先下载demo,并更新demo项目依赖路径到 sdk/0.0.X,建议采用最新版本,然后运行demo
    2. 2、在项目中添加sdk目录中最新的dll包:
    3. |-- mimc-csharp-sdk.dll
    4. |--Newtonsoft.Json.dll
    5. |--protobuf-net.dll
    6. |--StreamJsonRpc.dll
    7. |--log4net.dll
    8. 3、项目采用Visual Studio 2017开发;
    9. 4、日志采用log4net组件,项目中已经做了简单配置,可以按照需要自行修改;

    用户初始化

    1. /**
    2. * @param[appId]: 开发者在小米开放平台申请的appId
    3. * @param[appAccount]: 用户在APP帐号系统内的唯一帐号ID
    4. **/
    5. User user = new User(appId, appAccount);

    安全认证

    参考 详细文档安全认证

    1. user.RegisterTokenFetcher(IMIMCTokenFetcher fetcher);

    实现接口:

    1. interface IMIMCTokenFetcher {
    2. /// <summary>
    3. /// MIMCUser如果采用Login()方式登录 则必须实现该接口
    4. /// FetchToken()访问APP应用方自行实现的AppProxyService服务,
    5. /// 该服务实现以下功能:
    6. ///1. 存储appId/appKey/appSecret(appKey/appSecret不可存储在APP客户端,以防泄漏)
    7. ///2. 用户在APP系统内的合法鉴权
    8. ///3. 调用小米TokenService服务,并将小米TokenService服务返回结果通过fetchToken()原样返回
    9. /// </summary>
    10. /// <returns>小米TokenService服务下发的原始数据</returns>
    11. string FetchToken();
    12. /// <summary>
    13. /// MIMCUser如果采用LoginAsync()方式登录 则必须实现该接口
    14. /// </summary>
    15. /// <returns>小米TokenService服务下发的原始数据</returns>
    16. Task<string> FetchTokenAsync();
    17. }

    登录

    1. /**
    2. * @note: 用户登录接口,除在APP初始化时调用,APP从后台切换到前台时也建议调用一次
    3. */
    4. user.Login();
    5. user.LoginAsync();

    在线状态变化回调

    1. ///添加事件
    2. user.StateChangeEvent += HandleStatusChange;
    3. public void HandleStatusChange(object source, StateChangeEventArgs e)
    4. {
    5. logger.InfoFormat("{0} OnlineStatusHandler status:{1},errType:{2},errReason:{3},errDescription:{4}!",e.User.AppAccount(), e.IsOnline, e.ErrType, e.ErrReason, e.ErrDescription);
    6. }

    发送单聊消息

    1. /**<summary>
    2. * 发送单聊消息
    3. * </summary>
    4. * <param name="toAppAccount">消息接收者在APP帐号系统内的唯一帐号ID</param>
    5. * <param name="msg">开发者自定义消息体,二级制数组格式</param>
    6. * <returns>packetId客户端生成的消息ID</returns>
    7. **/
    8. String packetId = user.SendMessage(string toAppAccount, byte[] msg)
    9. String packetId = user.SendMessageAsync(string toAppAccount, byte[] msg)
    10. /** <summary>
    11. * 发送单聊消息
    12. * </summary>
    13. * <param name="toAppAccount">消息接收者在APP帐号系统内的唯一帐号ID</param>
    14. * <param name="msg">开发者自定义消息体,二级制数组格式</param>
    15. * <param name="isStore">是否保存历史记录,true:保存,false:不存</param>
    16. * <returns>packetId客户端生成的消息ID</returns>
    17. **/
    18. String packetId = user.SendMessage(string toAppAccount, byte[] msg, bool isStore)
    19. String packetId = user.SendMessageAsync(string toAppAccount, byte[] msg, bool isStore)

    发送群聊消息

    1. /// <summary>
    2. /// 发送群聊消息
    3. /// </summary>
    4. /// <param name="topicId">群ID</param>
    5. /// <param name="msg">开发者自定义消息体,二级制数组格式</param>
    6. /// <returns>packetId客户端生成的消息ID</returns>
    7. String packetId = user.SendGroupMessage(long topicId, byte[] msg)
    8. String packetId = user.SendGroupMessageAsync(long topicId, byte[] msg)
    9. /// <summary>
    10. /// 发送群聊消息
    11. /// </summary>
    12. /// <param name="topicId">群ID</param>
    13. /// <param name="msg">开发者自定义消息体,二级制数组格式</param>
    14. /// <param name="isStore">是否保存历史记录,true:保存,false:不存</param>
    15. /// <returns>packetId客户端生成的消息ID</returns>
    16. String packetId = user.SendGroupMessage(long topicId, byte[] msg, bool isStore)
    17. String packetId = user.SendGroupMessageAsync(long topicId, byte[] msg, bool isStore)

    接收消息回调

    1. ///添加事件
    2. user.MessageEvent += HandleMessage;
    3. user.MessageTimeOutEvent += HandleMessageTimeout;
    4. user.GroupMessageEvent += HandleGroupMessage;
    5. user.GroupMessageTimeoutEvent += HandleGroupMessageTimeout;
    6. user.ServerACKEvent += HandleServerACK;
    7. public void HandleMessageTimeout(object source, SendMessageTimeoutEventArgs e)
    8. {
    9. P2PMessage msg = e.P2PMessage;
    10. logger.InfoFormat("MIMCMessageHandler HandleSendMessageTimeout, to:{0}, packetId:{1}, sequence:{2}, ts:{3}, payload:{4}",
    11. e.User.AppAccount(), msg.PacketId, msg.Sequence, msg.Timestamp,
    12. Encoding.UTF8.GetString(msg.Payload));
    13. }
    14. public void HandleGroupMessage(object source, GroupMessageEventArgs e)
    15. {
    16. List<P2TMessage> packets = e.Packets;
    17. logger.InfoFormat("MIMCMessageHandler HandleGroupdMessage, to:{0}, packetCount:{1}", e.User.AppAccount(), packets.Count);
    18. if (packets.Count == 0)
    19. {
    20. logger.WarnFormat("HandleGroupdMessage packets.Count==0");
    21. return;
    22. }
    23. foreach (P2TMessage msg in packets)
    24. {
    25. logger.InfoFormat("MIMCMessageHandler HandleGroupdMessage, to:{0}, packetId:{1}, sequence:{2}, ts:{3}, payload:{4}",
    26. e.User.AppAccount(), msg.PacketId, msg.Sequence, msg.Timestamp,
    27. Encoding.UTF8.GetString(msg.Payload));
    28. }
    29. }
    30. public void HandleGroupMessageTimeout(object source, SendGroupMessageTimeoutEventArgs e)
    31. {
    32. P2TMessage msg = e.P2tMessage;
    33. logger.InfoFormat("MIMCMessageHandler HandleSendGroupdMessageTimeout, to:{0}, packetId:{1}, sequence:{2}, ts:{3}, payload:{4}",
    34. e.User.AppAccount(), msg.PacketId, msg.Sequence, msg.Timestamp,
    35. Encoding.UTF8.GetString(msg.Payload));
    36. }
    37. public void HandleServerACK(object source, ServerACKEventArgs e)
    38. {
    39. ServerAck serverAck = e.ServerAck;
    40. logger.InfoFormat("{0} MIMCMessageHandler HandleServerACK, appAccount:{0}, packetId:{1}, sequence:{2}, ts:{3}",
    41. e.User.AppAccount(), serverAck.PacketId, serverAck.Sequence, serverAck.Timestamp);
    42. }

    注销

    1. user.Logout();

    回到顶部