当前位置: 萬仟网 > IT编程>开发语言>.net > C# 调用腾讯即时通信 IM的示例

C# 调用腾讯即时通信 IM的示例

2020年11月21日  | 萬仟网IT编程  | 我要评论
im sdk api 概述 /// <summary> /// im sdk 初始化。 /// </summary> /// <param name="

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# 调用腾讯即时通信 im的示例的详细内容,更多关于c# 调用腾讯 im的资料请关注萬仟网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
Copyright © 2017-2020  萬仟网 保留所有权利. 粤ICP备17035492号-1