http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2842|回复: 4

[vue.js] C#WebIM融云聊天

[复制链接]
发表于 2018-12-28 16:47:34 | 显示全部楼层 |阅读模式
本帖最后由 惜 于 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>








1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-28 16:48:59 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-28 17:29:33 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-29 08:25:19 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-29 08:29:18 | 显示全部楼层
真是难得给力的帖子啊。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-29 19:44

© 2014-2021

快速回复 返回顶部 返回列表