请问有没有C#的转换成channelKey的相关代码


 我大概按照java和nodejs的模仿写了c#的,但不能用
mRtcEngine.joinChannel 返回值是0 但是不能正常视频,也不知道错在哪里了
请提供相关原理或方法
 
已邀请:

石敏

赞同来自:

您好,暂时我们这边没有c#生成channelkey的相关示例,你可以onerror查看一下回调里的报错,看看是什么问题,我们再分析一下。

kang

赞同来自:

后来仔细分析了nodejs的相关代码,按照那个逻辑已经把C#代码调整好了,现在可以使用了
        public static string GetHMACToken(string appCertificate, string serviceType, string appID, string unixTs, string salt, string channelName, string uid, string expiredTs, string extra)
        {
            byte[] keyByte = UnHex(appCertificate);
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(false);
            byte[] appIDByte = UnHex(appID);
            //wtest("appIDByte", appIDByte);
            MemoryStream messageStream = new MemoryStream();
            MemoryStream contentStream = new MemoryStream();
            string[] arr1 = new string[] { serviceType, appID, unixTs, salt, channelName, uid, expiredTs, extra };
            try
            {
                for (int i = 0; i < arr1.Length; i++)
                {
                    byte[] tBytes;
                    if (i == 1)
                    {//appID
                        byte[] bt1 = BitConverter.GetBytes(appIDByte.Length);
                        messageStream.Write(bt1, 0, 2);
                        messageStream.Write(appIDByte, 0, appIDByte.Length);
                    }
                    else if (!string.IsNullOrEmpty(arr1[i]))
                    {//BitConverter 
                        if (i == 0)
                        {//serviceType
                            tBytes = BitConverter.GetBytes(int.Parse(arr1[i]));
                            messageStream.Write(tBytes, 0, 2);
                        }
                        else if (i == 4)
                        {//channelName
                            tBytes = encoding.GetBytes(arr1[i]);
                            byte[] bt2 = BitConverter.GetBytes(tBytes.Length);
                            messageStream.Write(bt2, 0, 2);
                            messageStream.Write(tBytes, 0, tBytes.Length);
                        }
                        else if (i == 5)
                        {//uid
                            tBytes = BitConverter.GetBytes(Convert.ToInt64(arr1[i]));
                            //byte[] tBytes = UnHex(arr1[i]);// new arr1[i].Length < 2 ? arr1[i] + "0" : arr1[i];
                            messageStream.Write(tBytes, 0, 4);
                        }
                        else
                        {
                            tBytes = BitConverter.GetBytes(Convert.ToInt32(arr1[i]));
                            //byte[] tBytes = UnHex(arr1[i]);// new arr1[i].Length < 2 ? arr1[i] + "0" : arr1[i];
                            messageStream.Write(tBytes, 0, tBytes.Length);
                        }
                    }
                    else
                    {
                        tBytes = BitConverter.GetBytes(0);
                        messageStream.Write(tBytes, 0, 2);
                    }
                }
                //wtest("messageStream", messageStream.ToArray());
                string strmessage = "";
                byte[] hashmessage;
                using (HMACSHA1 hmacsha1 = new HMACSHA1(keyByte))
                {
                    hashmessage = hmacsha1.ComputeHash(messageStream.ToArray());
                    for (int i = 0; i < hashmessage.Length; i++)
                    {
                        string t1 = string.Format("{0:X}", hashmessage[i]);
                        strmessage += t1.Length < 2 ? "0" + t1 : t1;
                    }
                }
                //serviceType + strmessage + appID + unixTs + salt + expiredTs + extra
                string[] arr2 = new string[] { serviceType, strmessage, appID, unixTs, salt, expiredTs, extra };
                for (int i = 0; i < arr2.Length; i++)
                {
                    byte[] tBytes;
                    if (i == 2)
                    {
                        byte[] bt1 = BitConverter.GetBytes(appIDByte.Length);
                        contentStream.Write(bt1, 0, 2);
                        contentStream.Write(appIDByte, 0, appIDByte.Length);
                    }
                    else if (!string.IsNullOrEmpty(arr2[i]))
                    {//BitConverter 
                        if (i == 0)
                        {
                            tBytes = BitConverter.GetBytes(int.Parse(arr1[i]));
                            contentStream.Write(tBytes, 0, 2);
                        }
                        else if (i == 1)
                        {//channelName
                            tBytes = encoding.GetBytes(arr2[i]);
                            byte[] bt2 = BitConverter.GetBytes(tBytes.Length);
                            contentStream.Write(bt2, 0, 2);
                            contentStream.Write(tBytes, 0, tBytes.Length);
                        }
                        else
                        {
                            tBytes = BitConverter.GetBytes(Convert.ToInt32(arr2[i]));
                            //byte[] tBytes = UnHex(arr1[i]);// new arr1[i].Length < 2 ? arr1[i] + "0" : arr1[i];
                            contentStream.Write(tBytes, 0, tBytes.Length);
                        }
                    }
                    else
                    {
                        tBytes = BitConverter.GetBytes(0);
                        contentStream.Write(tBytes, 0, 2);
                    }

                }
                //wtest("contentStream", contentStream.ToArray()); 
                //byte[] btend = contentStream.ToArray();
                return Convert.ToBase64String(contentStream.ToArray());
            }
            catch (Exception ex)
            {
                
            }
            finally
            {
                messageStream.Close();
                contentStream.Close();
            }
            return "";
        }
        public static byte[] UnHex(string hex)
        {
            if (hex == null)
                throw new ArgumentNullException("hex");
            hex = hex.Replace(",", "");
            hex = hex.Replace("\n", "");
            hex = hex.Replace("\\", "");
            hex = hex.Replace(" ", "");
            if (hex.Length % 2 != 0)
            {
                hex += "20";//空格
            }
            // 需要将 hex 转换成 byte 数组。
            byte[] bytes = new byte[hex.Length / 2];
            for (int i = 0; i < bytes.Length; i++)
            {
                try
                {
                    // 每两个字符是一个 byte。
                    bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
                    System.Globalization.NumberStyles.HexNumber);
                }
                catch
                {
                    // Rethrow an exception with custom message.
                    throw new ArgumentException("hex is not a valid hex number!", "hex");
                }
            }
            return bytes;
        }

要回复问题请先登录注册