mode: TRTC mode, which should be set to livesdkAppId: the sdkAppId you obtain from Tencent ClouduserId: user IDuserSig: user signatureconst client = TRTC.createClient({mode: 'live',sdkAppId,userId,userSig});
roomId: room IDrole: roleanchor (default): users in the role of “anchor” can publish local streams and play remote streams.audience. Users in the role of “audience” can play remote streams but cannot publish local streams. To co-anchor and publish local streams, audience must switch the role to anchor using Client.switchRole().// Enter a room as audienceclient.join({ roomId, role: 'audience' }).then(() => {console.log('Entered room successfully');}).catch(error => {console.error('Failed to enter room' + error);});
client.on('stream-added'), which is used to listen for remote streams, call Client.subscribe() to subscribe to the remote stream.client.on('stream-added') callback before you call Client.join() to enter the room.client.on('stream-added', event => {const remoteStream = event.stream;console.log('New remote stream:' + remoteStream.getId());// Subscribe to the remote streamclient.subscribe(remoteStream);});client.on('stream-subscribed', event => {const remoteStream = event.stream;console.log('Subscribed to remote stream successfully:' + remoteStream.getId());// Play the remote streamremoteStream.play('remote_stream-' + remoteStream.getId());});
play method allows a parameter that is a div element ID. The SDK will create an audio/video tag in the div element and play the stream on it.client.on('stream-subscribed', event => {const remoteStream = event.stream;console.log('Subscribed to remote stream successfully:' + remoteStream.getId());// Play the remote streamremoteStream.play('remote_stream-' + remoteStream.getId());});
client.switchRole('anchor').then(() => {// Role switched to “anchor” successfully}).catch(error => {console.error('Failed to switch role' + error);});
userId: ID of the user to whom the local stream belongsaudio: whether to enable audiovideo: whether to enable videoconst localStream = TRTC.createStream({ userId, audio: true, video: true });
localStream.initialize().then(() => {console.log('Local stream initialized successfully');}).catch(error => {console.error('Failed to initialize local stream' + error);});
localStream.initialize().then(() => {console.log('Local stream initialized successfully');localStream.play('local_stream');}).catch(error => {console.error('Failed to initialize local stream' + error);});
client.publish(localStream).then(() => {console.log('Local stream published successfully');}).catch(error => {console.error('Failed to publish local stream' + error);});
client.leave().then(() => {// Exited room successfully}).catch(error => {console.error('Failed to leave room' + error);});
appScene must be the same on each client. Inconsistent appScene may cause unexpected problems.Feedback