• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
    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;
        }
    }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#试题1发布时间:2022-07-10
下一篇:
C#extendedtoolkitpropertygrid隐藏部分属性发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap