温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用C#如何实现调用腾讯即时通信功能

发布时间:2020-11-23 16:13:17 来源:亿速云 阅读:277 作者:Leah 栏目:开发技术

本篇文章为大家展示了使用C#如何实现调用腾讯即时通信功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

IM SDK API 

/// <summary>
    /// IM SDK 初始化。
    /// </summary>
    /// <param name="sdk_app_id"></param>
    /// <param name="json_sdk_config"></param>
    /// <returns></returns>
    [DllImport(@"imsdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public extern static int TIMInit(long sdk_app_id, string json_sdk_config);
/// <summary>
    /// 登录
    /// </summary>
    /// <param name="user_id">用户id</param>
    /// <param name="user_sig">签名</param>
    /// <param name="cb"></param>
    /// <returns></returns>
    [DllImport(@"imsdk.dll", CallingConvention = CallingConvention.Cdecl)]
    private extern static int TIMLogin(string user_id, string user_sig, TIMCommCallback cb);
/// <summary>
    /// 接收新消息回调
    /// </summary>
    /// <param name="cb"></param>
    [DllImport(@"imsdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    private extern static void TIMAddRecvNewMsgCallback(delegateTIMRecvNewMsgCallback cb);
public void ImInit()
    {
      int sdk_app_id = 1400000000;
      var data = new { sdk_config_log_file_path = AppDomain.CurrentDomain.BaseDirectory, sdk_config_config_file_path = AppDomain.CurrentDomain.BaseDirectory };
      TIMInit(sdk_app_id, JsonConvert.SerializeObject(data));
      TIMAddRecvNewMsgCallback(TIMRecvNewMsgCallback);
      var tLSSig = new TLSSigAPIv2(sdk_app_id, "163250645178b565ffc0b22618d14a88d8a91d86f0e76813313197a5xxxxxxx");
      TIMLogin("用户", tLSSig.GenSig("用户"), CommCallback);

    }
    private delegate int TIMCommCallback(int code, string desc, string json_params);
    
    private int CommCallback(int code, string desc, string json_params)
    {
      return code;
    }
    public delegate void delegateTIMRecvNewMsgCallback([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string json_msg_array, IntPtr user_data);;
    private void TIMRecvNewMsgCallback(string json_msg_array, IntPtr user_data) {
      var b = Encoding.Default.GetBytes(json_msg_array);
      richTextBox1.AppendText(Encoding.UTF8.GetString(b));
    }
    private void Form1_Load(object sender, EventArgs e)
    {
     ImInit();
    }
public class Utf8Marshaler : ICustomMarshaler
    {
        public void CleanUpManagedData(object managedObj)
        {
        }
 
        public void CleanUpNativeData(IntPtr pNativeData)
        {
            Marshal.FreeHGlobal(pNativeData);
        }
 
        public int GetNativeDataSize()
        {
            return -1;
        }
 
        public IntPtr MarshalManagedToNative(object managedObj)
        {
            if (object.ReferenceEquals(managedObj, null))
                return IntPtr.Zero;
            if (!(managedObj is string))
                throw new InvalidOperationException();
 
            byte[] utf8Bytes = Encoding.UTF8.GetBytes((string)managedObj);
            IntPtr ptr = Marshal.AllocHGlobal(utf8Bytes.Length + 1);
            Marshal.Copy(utf8Bytes, 0, ptr, utf8Bytes.Length);
            Marshal.WriteByte(ptr, utf8Bytes.Length, 0);
            return ptr;
        }
 
        public object MarshalNativeToManaged(IntPtr pNativeData)
        {
            if (pNativeData == IntPtr.Zero)
                return null;
 
            List<byte> bytes = new List<byte>();
            for (int offset = 0; ; offset++)
            {
                byte b = Marshal.ReadByte(pNativeData, offset);
                if (b == 0)
                    break;
                else
                    bytes.Add(b);
            }
            return Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count);
        }
 
        private static readonly Utf8Marshaler Instance = new Utf8Marshaler();
        public static ICustomMarshaler GetInstance(string cookie)
        {
            return Instance;
        }
    }

上述内容就是使用C#如何实现调用腾讯即时通信功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI