苏飞论坛

标题: C#WebIM融云聊天 [打印本页]

作者: 惜    时间: 2018-12-28 16:47
标题: C#WebIM融云聊天
本帖最后由 惜 于 2018-12-28 16:47 编辑

本代码借助 融云 websdk-demo-master vue - im
[C#] 纯文本查看 复制代码
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using Job_Util.Helper;

namespace vue.im
{
    public partial class VueImIndex : System.Web.UI.Page
    {
        protected string Resultarray = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = Request.QueryString["userId"];

            if (string.IsNullOrEmpty(userId))
            {
                userId = "111111111";
            }

            string token = Session[userId] == null ? "" : Session[userId].ToString();

            if (string.IsNullOrEmpty(token))
            {
                string appkey = System.Configuration.ConfigurationManager.AppSettings["newrongappkey"];//appkey
                string appSecret = System.Configuration.ConfigurationManager.AppSettings["newrongappSecret"];

                string methodUrl = "http://api-cn.ronghub.com/user/getToken.json";

                string postStr = string.Format("userId={0}&name={1}&portraitUri={2}", userId, "我是对面的小朋友", "");

                byte[] responseArray = null;
                Random rd = new Random();
                int rd_i = rd.Next();
                string nonce = rd_i.ToString();//随机数
                string timestamp = ConvertDateTimeInt(DateTime.Now).ToString();//时间戳
                string signature = GetHash(appSecret + nonce + timestamp);//数据签名

                //解决服务器证书验证问题
                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                WebClient myWebClient = new WebClient();
                myWebClient.Headers.Add("App-Key", appkey);
                myWebClient.Headers.Add("Nonce", nonce);
                myWebClient.Headers.Add("Timestamp", timestamp);
                myWebClient.Headers.Add("Signature", signature);
                myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                byte[] byteArray = Encoding.UTF8.GetBytes(postStr);
                try
                {
                    responseArray = myWebClient.UploadData(methodUrl, "POST", byteArray);
                }
                catch
                {

                }

                if (responseArray != null)
                {
                    Resultarray = Encoding.UTF8.GetString(responseArray);
                }

                RongYunTokenInfo rongYun = JsonHelper.jsonDes<RongYunTokenInfo>(Resultarray);

                Session[userId] = rongYun.token;
            }
            Resultarray = token;
        }

        /// <summary>
        /// 融云返回数据实体
        /// </summary>
        public class RongYunTokenInfo
        {
            public string code { get; set; }
            public string userId { get; set; }
            public string token { get; set; }
        }

        /// <summary>
        /// 签名字符串
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int ConvertDateTimeInt(DateTime time)
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        
        /// <summary>
        ///
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static String GetHash(String input)
        {
            //建立SHA1对象
            SHA1 sha = new SHA1CryptoServiceProvider();

            //将mystr转换成byte[]
            UTF8Encoding enc = new UTF8Encoding();
            byte[] dataToHash = enc.GetBytes(input);

            //Hash运算
            byte[] dataHashed = sha.ComputeHash(dataToHash);

            //将运算结果转换成string
            string hash = BitConverter.ToString(dataHashed).Replace("-", "");

            return hash;
        }
    }
}

[HTML] 纯文本查看 复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="vue.im.VueImIndex" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Rong-IM-Vue</title>
    <link rel="icon" type="image/png" href="css/images/logo.png">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/main.css">

    <script src="js/service.js"></script>
    <script src="//cdn.ronghub.com/RongIMLib-2.3.0.js"></script>

    <script src="lib/vue-2.1.4.js"></script>
    <script src="lib/vue-router-2.1.1.js"></script>
        
    <script>
        var RongIM = {
            config: {
                appKey: "11111111",
                token: "<%=Resultarray %>"
            },
            components: {}
        };
    </script>
        
    <script src="conversation-list.js"></script>
    <script src="message-list.js"></script>

    <script src="routes.js"></script>

    <script src="im.js"></script>

</head>
<body>
<div class="rong-im" id="rong-im">
    <!-- 会话列表 -->
    <router-view name="list"></router-view>
    <!-- 会话窗口消息列表 -->
    <router-view name="chat"></router-view>
</div>
</body>
</html>







作者: Amy    时间: 2018-12-28 16:48
我只是路过打酱油的。
作者: 竹林风    时间: 2018-12-28 17:29
强烈支持楼主ing……
作者: 站长苏飞    时间: 2018-12-29 08:25
我只是路过打酱油的。
作者: 范范    时间: 2018-12-29 08:29
真是难得给力的帖子啊。




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4