switchChannel method Null safety

Future<int> switchChannel(
  1. String? token,
  2. String channelName
)

Switch to a different RTC room.
In live streaming, audiences can switch from the current room to another room calling this method. After you switches to another room:

  • The app client first receives the message returned from [NERtcChannelEventCallback.onLeaveChannel]. The result parameter is [NERtcErrorCode.leaveChannelForSwitch]. Then, the client receives the message returned from [NERtcChannelEventCallback.onJoinChannel].
  • Remote users will receive messages returned by [NERtcChannelEventCallback.onUserLeave] and [NERtcChannelEventCallback.onUserJoined]. By default, after a user switches to another room, the user subscribes to all audio streams from other members in the new room. In this case, data usage is charged and billing is updated. To unsubscribe from all audio streams, call the [subscribeRemoteAudio] method by setting a false value to the subscribe parameter.


    The method applies to only the live streaming. The role is the audience in the RTC room. The room scenarios is set to live streaming by calling the setChannelProfile method, and the role of a room member is set to audience by calling the setClientRole method.

    token generated in the server and used for authentication. Valid values:

    • obtained token. Token is required in safe mode. By default, the token expires after 10 minutes. You can also request the token on a regular basis from YunXin server or a token that has a long lifetime. We recommend that you use the safe mode.
    • null。 You can specify null in a non-safe mode. This poses a security risk. We recommend that you contact your business manager to change to safe mode before your product is officially launched. [channelName] specifies the room name that a user want to switch to.


      0: success Other values: failure.

      • [NERtcErrorCode.switchChannelNotJoined]: Failed to switch to the room
      • [NERtcErrorCode.reserveNoPermission]: The role of the user is not audience.
      • [NERtcErrorCode.roomAlreadyJoined]: The user already joined the room

Implementation

Future<int> switchChannel(String? token, String channelName) async {
  IntValue reply = await _api.switchChannel(SwitchChannelRequest()
    ..token = token
    ..channelName = channelName);
  return reply.value ?? -1;
}