# 加入多频道 (/zh-CN/realtime-media/rtc/build/advanced-channel/multiple-channel/react-native)
> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).
声网 SDK 支持用户同时加入多个频道,同一时间可以在多个频道中接收和发布音视频流。
## 技术原理 [#技术原理-6]
SDK 提供 `RtcConnection` 对象和 `IRtcEngineEx` 类实现多频道功能。`IRtcEngineEx` 提供适用于指定 `RtcConnection` 对象的方法。
`RtcConnection` 对象包含用于识别连接的如下信息:
- 频道名称
- 本地用户的用户 ID
你可以创建多个 `RtcConnection` 对象,并为每个对象设置不同的频道名称和用户 ID。
如果要加入多个频道,多次调用 `IRtcEngineEx` 类中的 `joinChannelEx` 并设置不同的 `RtcConnection` 对象。加入多频道时请注意:
- 每个 `RtcConnection` 对象的用户 ID 是唯一的,且不为 `0`。
- 可以为 `joinChannelEx` 中的 `RtcConnection` 对象配置发布和订阅选项。
- 每个 `RtcConnection` 可以独立发布多路音频流和一路视频流。
- 你可以通过每个具体回调中的 `connection` 参数得知该回调对应哪个频道的事件。
## 前提条件 [#前提条件-6]
在进行操作之前,请确保你已经在项目中实现了基本的实时音视频功能。详见[实现音视频互动](/zh-CN/realtime-media/rtc/get-started/quick-start)。
## 实现方法 [#实现方法-6]
为实现多频道功能,你需要对每个频道进行如下设置:
1. 调用 `joinChannelEx`,定义 `RtcConnection` 并使用随机的用户 ID 加入频道。
```javascript
let token2 = "token2";
const channelId2 = "channel2";
let uid2 = 321;
// 加入多频道
rtcEngine.joinChannelEx(
token2,
// RtcConnection
{
channelId: channelId2,
localUid: uid2,
},
// ChannelMediaOptions
{
clientRoleType: ClientRoleType.ClientRoleBroadcaster,
publishMicrophoneTrack: false,
publishCameraTrack: false,
autoSubscribeAudio: false,
autoSubscribeVideo: false,
publishSecondaryCameraTrack: true,
}
);
```
2. 监听 `onUserJoined` 回调并设置远端视频。
```jsx
// 注册远端用户加入当前频道回调
agoraEngine.registerEventHandler(
{
onUserJoined: (_connection: RtcConnection, uid: number) => {
showMessage("远端用户 " + uid + " 已加入");
setRemoteUid(uid);
},
}
);
// 使用 RtcSurfaceView 创建远端视图
;
```
## 参考信息 [#参考信息-6]
### API 参考 [#api-参考-6]
- [`joinChannelEx`](/zh-CN/api-reference/rtc/react-native/channel#api_irtcengineex_joinchannelex)
- `setupRemoteVideoEx`
- [`onUserJoined`](/zh-CN/api-reference/rtc/react-native/channel#callback_irtcengineeventhandler_onuserjoined)
### 示例项目 [#示例项目-5]
声网提供了加入多频道的示例项目供你参考。你可以前往下载或查看其中的源代码。
JoinMultiChannel} href="https://gitee.com/agoraio-community/react-native-agora/tree/main/example/src/examples/advanced/JoinMultipleChannel/JoinMultipleChannel.tsx" />
JoinMultiChannel} href="https://github.com/AgoraIO-Extensions/react-native-agora/blob/main/examples/legacy/src/examples/advanced/JoinMultipleChannel/JoinMultipleChannel.tsx" />