为适应不同国家或地区的法律法规,声网支持限定访问区域功能,实现 SDK 只访问指定区域内的声网服务器,将音视频和消息数据传输限定在某一区域范围内。
为适应不同国家或地区的法律法规,声网支持限定访问区域功能。开启限定访问区域功能后,不论用户在哪个区域使用你的 app,SDK 都只会访问指定区域的声网服务器。
例如你指定了北美为访问区域,假设有两位用户分别从北美和中国连接声网服务器:
指定的访问区域 | App 用户所在区域 | SDK 实际访问的区域 | 连接后的用户体验 1 |
---|---|---|---|
北美 | 北美 | 北美 | 正常 |
中国 | 可能受到较大影响 2 |
在进行操作之前,请确保你已经在项目中实现了基本的实时音视频功能。 有关详细信息,请参考实现视频通话或实现视频直播。
你需要在调用 initialize
方法初始化 IRtcEngine
实例时,通过设置 RtcEngineContext
中的 AreaCode
参数来指定访问区域。
AREA_CODE_GLOB
:(默认)全球。AREA_CODE_CN
:中国大陆。AREA_CODE_NA
:北美。AREA_CODE_EU
:欧洲。AREA_CODE_AS
:除中国大陆以外的亚洲区域。AREA_CODE_JP
:日本。AREA_CODE_IN
:印度。如果要指定仅访问一个区域的服务器(如北美),在调用 initialize
方法之前添加 context.areaCode = AREA_CODE_NA;
。
// 初始化声网 SDK
bool CAgoraRegionConnDlg::InitAgora()
{
// 创建 AgoraRtcEngine
m_rtcEngine = createAgoraRtcEngine();
if (!m_rtcEngine) {
m_lstInfo.InsertString(m_lstInfo.GetCount() - 1, _T("createAgoraRtcEngine failed"));
return false;
}
// 设置接收通知消息的窗口
m_eventHandler.SetMsgReceiver(m_hWnd);
RtcEngineContext context;
std::string strAppID = GET_APP_ID;
context.appId = strAppID.c_str();
CString area_code;
m_cmbAreaCode.GetWindowText(area_code);
// 指定仅访问北美的服务器。
context.areaCode = AREA_CODE_NA;
// 初始化 RtcEngineContext
int ret = m_rtcEngine->initialize(context);
if (ret != 0) {
m_initialize = false;
CString strInfo;
strInfo.Format(_T("initialize failed: %d"), ret);
m_lstInfo.InsertString(m_lstInfo.GetCount(), strInfo);
return false;
}
else
m_initialize = true;
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("initialize success"));
}
如果要排除一个区域的服务器(如中国大陆),在调用 initialize
方法之前添加 context.areaCode = AREA_CODE_GLOB ^ AREA_CODE_CN;
。
// 初始化声网 SDK
bool CAgoraRegionConnDlg::InitAgora()
{
// 创建 AgoraRtcEngine
m_rtcEngine = createAgoraRtcEngine();
if (!m_rtcEngine) {
m_lstInfo.InsertString(m_lstInfo.GetCount() - 1, _T("createAgoraRtcEngine failed"));
return false;
}
// 设置接收通知消息的窗口
m_eventHandler.SetMsgReceiver(m_hWnd);
RtcEngineContext context;
std::string strAppID = GET_APP_ID;
context.appId = strAppID.c_str();
CString area_code;
m_cmbAreaCode.GetWindowText(area_code);
// 在访问区域中排除中国大陆。
context.areaCode = AREA_CODE_GLOB ^ AREA_CODE_CN;
// 初始化 RtcEngineContext
int ret = m_rtcEngine->initialize(context);
if (ret != 0) {
m_initialize = false;
CString strInfo;
strInfo.Format(_T("initialize failed: %d"), ret);
m_lstInfo.InsertString(m_lstInfo.GetCount(), strInfo);
return false;
}
else
m_initialize = true;
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("initialize success"));
}
本节介绍本文中相关页面的链接。
声网在 GitHub 上提供了一个开源的示例项目 RegionConn,你可以前往下载,或查看其中的源代码。