RTCConnection 类
RtcConnection
类,用于管理 SDK 与 RTC 频道之间的连接。连接完成之后,你可以通过 LocalUser
对象在 RTC 频道中发送和接收媒体流。连接频道是异步操作,你可以通过 IRtcConnectionObserver
对象监听连接状态变化和事件。RtcConnection
还可以监控频道中的远端用户是否加入或离开频道。
connect
Python
def connect(self, token: str, chan_id: str, user_id: str) -> int
方法成功时,连接状态由 1
变更为 2
。
根据连接是否成功,连接状态会变更为 3
或 5
。SDK 会触发 on_connected
或 on_disconnected
提醒连接状态变更。
参数
参数 | 描述 |
---|---|
token | RTC Token。详见 使用 Token 鉴权。 |
chan_id | 频道名。必须为字符串且长度不超过 64 字节。支持的字符类型包括:
|
user_id | 本地用户 ID。长度不超过 64 字节。如果你不设置用户 ID 或设为 None,SDK 在
|
返回值
0
: 方法调用成功。- <
0
: 方法调用失败。-2
: 参数无效。-8
: 当前连接状态不是1
。
disconnect
Python
def disconnect(self) -> int
1
。你还可以通过 on_disconnected
回调得知。
返回值
0
: 方法调用成功。- <
0
: 方法调用失败。
renew_token
Python
def renew_token(self, token: str) -> int
on_token_privilege_will_expire
回调时,你必须从服务端生成一个新的 Token,并使用该方法更新 Token。否则,SDK 会与频道断开连接。
参数
参数 | 描述 |
---|---|
token | 新 Token。 |
返回值
0
: 方法调用成功。- <
0
: 方法调用失败。
register_observer
Python
def register_observer(self, conn_observer: IRTCConnectionObserver) -> int
参数
参数 | 描述 |
---|---|
conn_observer | IRTCConnectionObserver 对象。 |
返回值
0
: 方法调用成功。- <
0
: 方法调用失败。
unregister_observer
Python
def unregister_observer(self) -> int
返回值
0
: 方法调用成功。- <
0
: 方法调用失败。
create_data_stream
Python
def create_data_stream(self, reliable: bool, ordered: bool) -> int
每个用户在每个频道中最多只能创建 5 个数据流。
注意
- 声网不支持你将
reliable
设为1
且ordered
设为0
。 - 当需要数据包立刻到达接收端时,不能将
ordered
参数设置为1
。
参数
参数 | 描述 |
---|---|
reliable | 该数据流是否可靠:
|
ordered | 该数据流是否有序:
|
返回值
stream_id
:方法调用成功,返回数据流 ID。None
:方法调用失败。
send_stream_message
Python
def send_stream_message(self, stream_id: int, data: str) -> int
该方法发送数据流消息到频道内所有用户。SDK 对该方法的实现进行了如下限制:
- 频道内每秒最多能发送 30 个包,且每个包最大为 1 KB。
- 每个客户端每秒最多能发送 6 KB 数据。
- 频道内每人最多能同时有 5 个数据通道。
成功调用该方法后,远端会触发 on_stream_message
回调,远端用户可以在该回调中获取接收到的流消息。
注意
- 请确保在调用该方法前,已调用
create_data_stream
创建了数据通道。 - 直播场景下,该方法仅适用于主播用户。
参数
参数 | 描述 |
---|---|
stream_id | 数据流 ID。可以通过 create_data_stream 获取。 |
data | 待发送的数据。 |
返回值
0
: 方法调用成功。- <
0
: 方法调用失败。
get_agora_parameter
Python
def get_agora_parameter(self)
AgoraParameter
对象。
返回值
AgoraParameter
对象:方法调用成功。None
:方法调用失败。
get_local_user
Python
def get_local_user(self)
LocalUser
对象。每个连接仅对应一个本地用户。
返回值
LocalUser
对象:方法调用成功。None
:方法调用失败。
release
Python
def release(self)