欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 调用腾讯即时通信 IM

程序员文章站 2023-11-14 09:38:46
IM SDK API 概述 https://cloud.tencent.com/document/product/269/33543 ......

im sdk api 概述 https://cloud.tencent.com/document/product/269/33543

        /// <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;
        }
        private delegate void delegatetimrecvnewmsgcallback(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();
        }