• C#关于HttpClient的应用(二):融云IM集成


        public class RcHttpClient:BaseHttpClient
        {
            private String appKey;
            private String appSecret;
    
            public RcHttpClient()
            {
                this.appKey = BaseHelper.GetValue("RongCloudAppKey");
                this.appSecret = BaseHelper.GetValue("RongCloudSecret");
                this.contentType = "application/x-www-form-urlencoded";
            }
    
            public RcHttpClient(String appKey, String masterSecret)
            {
                this.appKey = appKey;
                this.appSecret = masterSecret;
                this.contentType = "application/x-www-form-urlencoded";
            }
    
            /// <summary>
            /// HTTP 验证
            /// </summary>
            /// <returns></returns>
            public override Dictionary<string,string> Authorization()
            {
                Random rd = new Random();
                String nonce = Convert.ToString(rd.Next());
                string timestamp = Convert.ToString(BaseHelper.ConvertDateTimeInt(DateTime.Now));
                string signature = Sha1.GetHash(this.appSecret + nonce + timestamp);
    
                return new Dictionary<string, string>
                {
                    {"App-Key", this.appKey},
                    {"Nonce", nonce},
                    {"Timestamp", timestamp},
                    {"Signature", signature}
                };
            }

    Server层:

        public class RcClientServer
        {
            #region 请求地址
            private const String HOST_PATH = "http://api.cn.ronghub.com";
            private const String GET_TOKEN_PATH = HOST_PATH + "/user/getToken.json";
            private const String CREATE_GROUP_PATH = HOST_PATH + "/group/create.json";
            private const String JOIN_GROUP_PATH = HOST_PATH + "/group/join.json";
            private const String QUIT_GROUP_PATH = HOST_PATH + "/group/quit.json";
            private const String DISMISS_PATH = HOST_PATH + "/group/dismiss.json";
            private const String SYNC_GROUP_PATH = HOST_PATH + "/group/sync.json";
            private const String SEND_MSG_PATH = HOST_PATH + "/message/publish.json";
            private const String BROADCAST_PATH = HOST_PATH + "/message/broadcast.json";
            private const String CREATE_CHATROOM_PATH = HOST_PATH + "/chatroom/create.json";
            private const String DESTORY_CHATROOM_PATH = HOST_PATH + "/chatroom/destroy.json";
            private const String QUERY_CHATROOM_PATH = HOST_PATH + "/chatroom/query.json";
            #endregion
    
            /// <summary>
            /// 获取Token
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="name"></param>
            /// <param name="portraitUri"></param>
            /// <returns></returns>
            public static ResultDTO GetToken(String userId, String name, String portraitUri)
            {
                var reqParams = new Dictionary<String, String>
                {
                    {"userId", userId},
                    {"name", name},
                    {"portraitUri", portraitUri}
                };
                RcHttpClient client=new RcHttpClient();
                var result = client.SendRequest(Method.Post, GET_TOKEN_PATH, client.BuildQueryStr(reqParams));
                if (result.status)
                {
                    return WebApi.Success(JsonHelper.ToObject(result.info.ToString())["token"].ToString());
                }
                return WebApi.Error(result.info);
            }
    
            /// <summary>
            /// 创建群组
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="groupId"></param>
            /// <param name="groupName"></param>
            /// <remarks>同一用户最多可加入 3000 个群,每个群最大至 3000 人</remarks>
            /// <returns></returns>
            public static bool CreateGroup(String userId, String groupId, String groupName)
            {
                var reqParams = new Dictionary<String, String>
                {
                    {"userId", userId},
                    {"groupId", groupId},
                    {"groupName", groupName}
                };
                RcHttpClient client = new RcHttpClient();
                var result = client.SendRequest(Method.Post, CREATE_GROUP_PATH, client.BuildQueryStr(reqParams));
                return result.status;
            }
    
            /// <summary>
            /// 加入群组
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="groupId"></param>
            /// <param name="groupName"></param>
            /// <remarks>同一用户最多可加入 3000 个群,每个群最大至 3000 人</remarks>
            /// <returns></returns>
            public static bool JoinGroup(String userId, String groupId, String groupName)
            {
                var reqParams = new Dictionary<String, String>
                {
                    {"userId", userId},
                    {"groupId", groupId},
                    {"groupName", groupName}
                };
                RcHttpClient client = new RcHttpClient();
                var result = client.SendRequest(Method.Post, JOIN_GROUP_PATH, client.BuildQueryStr(reqParams));
                return result.status;
            }
    
            /// <summary>
            /// 退出群组
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="groupId"></param>
            /// <returns></returns>
            public static bool QuitGroup(String userId, String groupId)
            {
                var reqParams = new Dictionary<String, String>
                {
                    {"userId", userId},
                    {"groupId", groupId}
                };
    
                RcHttpClient client = new RcHttpClient();
                var result = client.SendRequest(Method.Post, QUIT_GROUP_PATH, client.BuildQueryStr(reqParams));
                return result.status;
            }
    
            /// <summary>
            /// 解散群组
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="groupId"></param>
            /// <returns></returns>
            public static bool DismissGroup(String userId, String groupId)
            {
                var reqParams = new Dictionary<String, String>
                {
                    {"userId", userId},
                    {"groupId", groupId}
                };
    
                RcHttpClient client = new RcHttpClient();
                var result = client.SendRequest(Method.Post, DISMISS_PATH, client.BuildQueryStr(reqParams));
                return result.status;
            }
    
            /// <summary>
            /// 同步群组
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="groupId"></param>
            /// <param name="groupName"></param>
            /// <returns></returns>
            public static bool SyncGroup(String userId, String[] groupId, String[] groupName)
            {
                String postStr = "userId=" + userId + "&";
                for (int i = 0; i < groupId.Length; i++)
                {
                    var id=HttpUtility.UrlEncode(groupId[i], Encoding.UTF8);
                    var name = HttpUtility.UrlEncode(groupName[i], Encoding.UTF8);
                    postStr += "group[" + id + "]=" + name + "&";
                }
                postStr = postStr.Substring(0, postStr.LastIndexOf('&'));
    
                RcHttpClient client = new RcHttpClient();
                var result = client.SendRequest(Method.Post, SYNC_GROUP_PATH, postStr);
                return result.status;
            }
    
            /// <summary>
            /// 发送文本消息
            /// </summary>
            /// <param name="fromUserId"></param>
            /// <param name="toUserId"></param>
            /// <param name="content"></param>
            /// <returns></returns>
            public static bool SendTextMessage(string fromUserId,string toUserId,string content)
            {
                var reqParams  = new Dictionary<String, String>
                {
                    {"fromUserId", fromUserId},
                    {"toUserId", toUserId},
                    {"objectName", "RC:TxtMsg"},
                    {"content", content}
                };
                RcHttpClient client = new RcHttpClient();
                var result = client.SendRequest(Method.Post, SYNC_GROUP_PATH, client.BuildQueryStr(reqParams));
                return result.status;
            }
        }
  • 相关阅读:
    hive 三种启动方式及用途
    Nodejs根据字符串调用对象方法
    Hive原理与不足
    [置顶] 面向领域概念:流的思考
    curl的使用
    mysql知识点总结
    中文字符串反转
    《c陷阱与缺陷》之贪心法
    静态数据成员和静态成员函数
    常成员函数 int fun() const;
  • 原文地址:https://www.cnblogs.com/xuhang/p/5204968.html
Copyright © 2020-2023  润新知