Integrated micro-channel Tencent applet cloud IM SDK

Integrated micro-channel Tencent applet cloud IM SDK

1. Background

  Due to business functional requirements need to access the IM (instant messaging) function, the beginning of thought is to use WebSocket to achieve this function, and then make fun of God (ha ha) server version is too low does not support wx protocol (also not support WebSocket a) had Looking for a third-party service, because the client is currently using the program to develop small micro-channel, that thought must use their own IM is a function. So far there of this article.

2, the document addresses

         1) Tencent cloud Live Chat: https://cloud.tencent.com/document/product/269

         2) micro-channel applet Demo: https://github.com/tencentyun/TIMSDK/tree/master/WXMini (official uses MPVUE to develop) This has made recommendations (own small program actually uses the framework of other stores Haha feeling a bit on her face, I did not have to say MPVUE entry Haha, it seems that this framework have to learn next, and Tencent engineers use it, and have to say it is an excellent framework for the applet)

   3)IM SDK :https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html?_ga=1.163142140.1311859679.1566291063

3, IM SDK integration

  3.1 Import SDK

    Import SDK, which is the introduction of their js files, download address: https://github.com/tencentyun/TIMSDK/tree/master/WXMini/sdk , or use the tool to import npm. Not here in a long-winded, we will default oh.

    (For everyone to see clearly, the other packages do not erase)

 

 

  3.2, initialization 

Copy the code
from the TIM Import 'Tim-WX-SDK' ;
 // send pictures, documents and other information required for the SDK COS 
Import from COS "COS-WX-V5-SDK" ; // If the chat text is sent, where necessary introduced 

Options the let = { 
  SDKAppID: 0 // need to replace the access to your SDKAppID 0 instant messaging application 
};
 // Create SDK instance, TIM.create () method returns only one instance of the same with respect to a SDKAppID 
let tim TIM.create = (Options); // the SDK examples usually indicates tim 

@ Register COS SDK widget 
tim.registerPlugin ({ 'cos-wx- sdk': COS}); // If the chat text is sent, where registration is not required
Copy the code

    Set the log level:

tim.setLogLevel(1);

  3.3 log

      Before logging in, you need to know UserSig and generate UserSig on the server side,

      UserSing: UserSig instant messaging IM is user login password, by its very nature is UserID and other information obtained by encrypting the ciphertext, this article will guide you how to generate UserSig.

      The server generates UserSig :( Our server uses C #, other language please see the official website: https://cloud.tencent.com/document/product/269/32688 )

      Because our server uses C #, so just to give C # server code generated UserSig

      

     NuGet command line integration    

PM> Install-Package tls-sig-api-v2

      use

using tencentyun;

TLSSigAPIv2 api = new TLSSigAPIv2(1400000000, "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e");
string sig = api.GenSig("userId");//userId:应该是您的 userId
System.Console.WriteLine(sig);

    tim Login

Copy the code
tim.login ({ 
            userID: the userId, 
            USERSIG: USERSIG // obtained by the server 
          }). the then ((imResponse) => { 
            the console.log (imResponse.data); // successful login 
            app.globalData.isImLogin = to true 
          } .) the catch ((imError) => { 
            console.warn ( 'Login error:', imError); // information failed login 
          })
Copy the code

  3.4, the messaging

      Hair (in plain text, for example):

Copy the code
// send text messaging, Web terminal end of the same applet 
// 1. Create message instance, examples of the interface on the screen may return to 
the let Message = tim.createTextMessage ({ 
  to: 'user1' , 
  conversationType: TIM.TYPES.CONV_C2C 
  payload : { 
    text: 'the Hello World!' 
  } 
}); 
// 2. transmitted message 
the let Promise = tim.sendMessage (message); 
promise.then ( function (imResponse) {
   // transmission success 
  the console.log (imResponse); 
} .) the catch ( function (imError) {
   // transmission failure 
  console.warn ( 'the sendMessage error:' , imError); 
});
Copy the code

    Income:

= OnMessageReceived the let function (Event) {
   // the event.data - array to store the Message object - [Message] 
}; 
tim.on (TIM.EVENT.MESSAGE_RECEIVED, OnMessageReceived);

    *note*:

        Here tim.on ( TIM.EVENT.MESSAGE_RECEIVED, function (Event) {}) This interface is global, no matter where you are writing, when you first start the applet will call it, whenever there is a new message to this interface We will be listening to.

     After entering the chat interface, call Tim. GetMessageList () interface to get chat history.

   Recommendation: After entering the chat interface, please report this conversation as read, (small programs mentioned above only after this first launch is set listening tim.on (TIM.EVENT.MESSAGE_RECEIVED, function (event) { } ) will not receive the message has been read  )

    Read report:  

// all unread messages read at a session reported 
tim.setMessageRead ({conversationID: 'session ID'});

  3.5 Exit

tim.logout()

4, complete

 

Description:

    1: If in doubt, you can get in touch with me

    2: an official document has been based, it is likely to have some time after the documentation and SDK will change

    3: The official documents have been given above

    4: No. article first appeared in public

    5: Small package server using the SDK Sheng faction ( https://weixin.senparc.com )

Integrated micro-channel Tencent applet cloud IM SDK

1. Background

  Due to business functional requirements need to access the IM (instant messaging) function, the beginning of thought is to use WebSocket to achieve this function, and then make fun of God (ha ha) server version is too low does not support wx protocol (also not support WebSocket a) had Looking for a third-party service, because the client is currently using the program to develop small micro-channel, that thought must use their own IM is a function. So far there of this article.

2, the document addresses

         1) Tencent cloud Live Chat: https://cloud.tencent.com/document/product/269

         2) micro-channel applet Demo: https://github.com/tencentyun/TIMSDK/tree/master/WXMini (official uses MPVUE to develop) This has made recommendations (own small program actually uses the framework of other stores Haha feeling a bit on her face, I did not have to say MPVUE entry Haha, it seems that this framework have to learn next, and Tencent engineers use it, and have to say it is an excellent framework for the applet)

   3)IM SDK :https://imsdk-1252463788.file.myqcloud.com/IM_DOC/Web/Message.html?_ga=1.163142140.1311859679.1566291063

3, IM SDK integration

  3.1 Import SDK

    Import SDK, which is the introduction of their js files, download address: https://github.com/tencentyun/TIMSDK/tree/master/WXMini/sdk , or use the tool to import npm. Not here in a long-winded, we will default oh.

    (For everyone to see clearly, the other packages do not erase)

 

 

  3.2, initialization 

Copy the code
from the TIM Import 'Tim-WX-SDK' ;
 // send pictures, documents and other information required for the SDK COS 
Import from COS "COS-WX-V5-SDK" ; // If the chat text is sent, where necessary introduced 

Options the let = { 
  SDKAppID: 0 // need to replace the access to your SDKAppID 0 instant messaging application 
};
 // Create SDK instance, TIM.create () method returns only one instance of the same with respect to a SDKAppID 
let tim TIM.create = (Options); // the SDK examples usually indicates tim 

@ Register COS SDK widget 
tim.registerPlugin ({ 'cos-wx- sdk': COS}); // If the chat text is sent, where registration is not required
Copy the code

    Set the log level:

tim.setLogLevel(1);

  3.3 log

      Before logging in, you need to know UserSig and generate UserSig on the server side,

      UserSing: UserSig instant messaging IM is user login password, by its very nature is UserID and other information obtained by encrypting the ciphertext, this article will guide you how to generate UserSig.

      The server generates UserSig :( Our server uses C #, other language please see the official website: https://cloud.tencent.com/document/product/269/32688 )

      Because our server uses C #, so just to give C # server code generated UserSig

      

     NuGet command line integration    

PM> Install-Package tls-sig-api-v2

      use

using tencentyun;

TLSSigAPIv2 api = new TLSSigAPIv2(1400000000, "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e");
string sig = api.GenSig("userId");//userId:应该是您的 userId
System.Console.WriteLine(sig);

    tim Login

Copy the code
tim.login ({ 
            userID: the userId, 
            USERSIG: USERSIG // obtained by the server 
          }). the then ((imResponse) => { 
            the console.log (imResponse.data); // successful login 
            app.globalData.isImLogin = to true 
          } .) the catch ((imError) => { 
            console.warn ( 'Login error:', imError); // information failed login 
          })
Copy the code

  3.4, the messaging

      Hair (in plain text, for example):

Copy the code
// send text messaging, Web terminal end of the same applet 
// 1. Create message instance, examples of the interface on the screen may return to 
the let Message = tim.createTextMessage ({ 
  to: 'user1' , 
  conversationType: TIM.TYPES.CONV_C2C 
  payload : { 
    text: 'the Hello World!' 
  } 
}); 
// 2. transmitted message 
the let Promise = tim.sendMessage (message); 
promise.then ( function (imResponse) {
   // transmission success 
  the console.log (imResponse); 
} .) the catch ( function (imError) {
   // transmission failure 
  console.warn ( 'the sendMessage error:' , imError); 
});
Copy the code

    Income:

= OnMessageReceived the let function (Event) {
   // the event.data - array to store the Message object - [Message] 
}; 
tim.on (TIM.EVENT.MESSAGE_RECEIVED, OnMessageReceived);

    *note*:

        Here tim.on ( TIM.EVENT.MESSAGE_RECEIVED, function (Event) {}) This interface is global, no matter where you are writing, when you first start the applet will call it, whenever there is a new message to this interface We will be listening to.

     After entering the chat interface, call Tim. GetMessageList () interface to get chat history.

   Recommendation: After entering the chat interface, please report this conversation as read, (small programs mentioned above only after this first launch is set listening tim.on (TIM.EVENT.MESSAGE_RECEIVED, function (event) { } ) will not receive the message has been read  )

    Read report:  

// all unread messages read at a session reported 
tim.setMessageRead ({conversationID: 'session ID'});

  3.5 Exit

tim.logout()

4, complete

 

Description:

    1: If in doubt, you can get in touch with me

    2: an official document has been based, it is likely to have some time after the documentation and SDK will change

    3: The official documents have been given above

    4: No. article first appeared in public

    5: Small package server using the SDK Sheng faction ( https://weixin.senparc.com )

Guess you like

Origin www.cnblogs.com/wyt007/p/11718432.html
Recommended