Web

更新时间:
复制 MD 格式

This topic describes how to use Real-Time Communication (RTC) SDK for Web to establish an interactive class between a teacher and students.

Procedure

  1. Create an instance on which RTS SDK for Web is used.
    var aliWebrtc = new AliRtcEngine();
  2. Configure the onUpdateRole callback to listen for role changes.
    Note In an interactive channel, the user role is successfully changed only when setClientRole returns true and onUpdateRole returns the value of the new role.
    aliWebrtc.on("onUpdateRole",(data) => {
        console.log(data);
    })
  3. Configure the user role.

    You can call this method before or after you join the channel. You must unpublish media tracks before you call this method.

    /**
     * @param  role The user role. 0 indicates the participant and 1 indicates the viewer.
     * @return true indicates that the call is successful and false indicates that the call failed.
     */
    aliWebrtc.setClientRole(role);
  4. Configure the channel mode.
    Note Set channelProfile to 1 to enable the interactive channel mode.
    /**
     * @param  channelProfile The channel mode. 0 indicates the communication mode, 1 indicates the interactive mode, and 2 indicates the low-latency interactive live streaming mode.
     * @return true indicates that the call is successful and false indicates that the call failed.
     */
    aliWebrtc.setChannelProfile(channelProfile)
  5. Check whether the browser you use supports RTC SDK for Web.
    aliWebrtc.isSupport().then(re => {
        console.log(re);
    }).catch(err => {
      console.log(err)
    })

    For more information, see isSupport.

  6. The user joins the channel.

    Only participants in the channel can publish media tracks.

    /**
     * @param authInfo The authentication information.
     * @param displayName The display name.
     */
    aliWebrtc.joinChannel(authInfo,displayName).then(()=>{
        // Operation succeeded.
    }).catch((err)=>{
      if(err.errCode === 33622275){
        // Operation failed. The channel mode is incorrect.
      }else{
        // Operation failed. An error other than incorrect channel mode occurred.
      }
    })
    Note authInfo contains the authentication information assigned from the application server. For more information about how to generate tokens and obtain authInfo, see Generate a token.

    For more information about methods, see AliRtcEngine class.