C# 调用腾讯即时通信 IM的示例
IMSDKAPI概述https://cloud.tencent.com/document/product/269/33543
//////IMSDK初始化。 /// ////// /// [DllImport(@"imsdk.dll",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)] publicexternstaticintTIMInit(longsdk_app_id,stringjson_sdk_config);
//////登录 /// ///用户id /// 签名 /// /// [DllImport(@"imsdk.dll",CallingConvention=CallingConvention.Cdecl)] privateexternstaticintTIMLogin(stringuser_id,stringuser_sig,TIMCommCallbackcb);
//////接收新消息回调 /// ///[DllImport(@"imsdk.dll",CallingConvention=CallingConvention.Cdecl,CharSet=CharSet.Ansi)] privateexternstaticvoidTIMAddRecvNewMsgCallback(delegateTIMRecvNewMsgCallbackcb);
publicvoidImInit()
{
intsdk_app_id=1400000000;
vardata=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);
vartLSSig=newTLSSigAPIv2(sdk_app_id,"163250645178b565ffc0b22618d14a88d8a91d86f0e76813313197a5xxxxxxx");
TIMLogin("用户",tLSSig.GenSig("用户"),CommCallback);
}
privatedelegateintTIMCommCallback(intcode,stringdesc,stringjson_params);
privateintCommCallback(intcode,stringdesc,stringjson_params)
{
returncode;
}
publicdelegatevoiddelegateTIMRecvNewMsgCallback([MarshalAs(UnmanagedType.CustomMarshaler,MarshalTypeRef=typeof(Utf8Marshaler))]stringjson_msg_array,IntPtruser_data);;
privatevoidTIMRecvNewMsgCallback(stringjson_msg_array,IntPtruser_data){
varb=Encoding.Default.GetBytes(json_msg_array);
richTextBox1.AppendText(Encoding.UTF8.GetString(b));
}
privatevoidForm1_Load(objectsender,EventArgse)
{
ImInit();
}
publicclassUtf8Marshaler:ICustomMarshaler
{
publicvoidCleanUpManagedData(objectmanagedObj)
{
}
publicvoidCleanUpNativeData(IntPtrpNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
publicintGetNativeDataSize()
{
return-1;
}
publicIntPtrMarshalManagedToNative(objectmanagedObj)
{
if(object.ReferenceEquals(managedObj,null))
returnIntPtr.Zero;
if(!(managedObjisstring))
thrownewInvalidOperationException();
byte[]utf8Bytes=Encoding.UTF8.GetBytes((string)managedObj);
IntPtrptr=Marshal.AllocHGlobal(utf8Bytes.Length+1);
Marshal.Copy(utf8Bytes,0,ptr,utf8Bytes.Length);
Marshal.WriteByte(ptr,utf8Bytes.Length,0);
returnptr;
}
publicobjectMarshalNativeToManaged(IntPtrpNativeData)
{
if(pNativeData==IntPtr.Zero)
returnnull;
Listbytes=newList();
for(intoffset=0;;offset++)
{
byteb=Marshal.ReadByte(pNativeData,offset);
if(b==0)
break;
else
bytes.Add(b);
}
returnEncoding.UTF8.GetString(bytes.ToArray(),0,bytes.Count);
}
privatestaticreadonlyUtf8MarshalerInstance=newUtf8Marshaler();
publicstaticICustomMarshalerGetInstance(stringcookie)
{
returnInstance;
}
}
以上就是C#调用腾讯即时通信IM的示例的详细内容,更多关于c#调用腾讯im的资料请关注毛票票其它相关文章!