跳到主要内容

WebSDK初始化

更新时间:2026-07-08 18:38:55

SDK初始化


/**
* 初始化SDK
* @param config - 初始配置项
* @returns
* @public
*/
declare function init(config: InitConfig): Promise<void>;


/**
* 设置语言
* @param locale - 语言
* @returns
* @public
*/
declare function setLocale(locale: LocaleEnum): Promise<void>;

/**
* 获取语言
* @returns
* @public
*/
declare function getLocale(): String;

/**
* 获取用户所在国家或区域
* @returns
* @public
*/
declare function getCountry(): Country | undefined;

/**
* 设置用户所在国家或区域
* @param country - 国家或区域
* @returns
* @public
*/
declare function setCountry(country: Country): Promise<void>;

/**
* 获取当前数据中心ID
* @returns
* @public
*/
declare function getRegionId(): number;

/**
* 获取接入区域(国内或海外)
* @returns
* @public
*/
declare function getRegionType(): RegionTypeEnum;
/**
* SDK实始化配置选择
* @public
*/
declare class InitConfig {
/**
* 是否开启日志调试
*/
debug: boolean;
/**
* 通用版App配置项
*/
appKeyConfig?: AppKeyConfig;
}


export declare namespace RTCXSDKInit {
export {
init,
uninit,
getRegionId,
getRegionType,
getCountry,
setLocale,
setCountry
}
}

设置接入区域

AIRTC平台的云端服务为多区域部署,根据您使用RTCXSDKInitTypes.InitConfig.regionType参数的取值区分中国大陆或海外

注意

中国大陆与海外都要接入时需要在控制台【中国数据中心】与【海外数据中心】分别创建两个不同的应用

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'



const initConfig = new RTCXSDKInitTypes.InitConfig()

initConfig.regionType = RTCXCommonTypes.RegionTypeEnum.REGION_ALL

区域类型

/**
* SDK接入区域
* @public
*/
export enum RegionTypeEnum {
/**
* 中国大陆
*/
REGION_CHINA_ONLY,
/**
* 海外
*/
REGION_ALL,
}

初始化

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'

// 初始换SDK,appKey和appSecret是平台分配给PC SDK,在系统中的唯一标识及对应的密钥
const appKeyConfig = new RTCXSDKInitTypes.AppKeyConfig()
appKeyConfig.appKey = 'appKey'
appKeyConfig.appSecret = 'appSecret'
const initConfig = new RTCXSDKInitTypes.InitConfig()
initConfig.appKeyConfig = appKeyConfig
initConfig.debug = false //需要时开启
initConfig.productEnv = RTCXCommonTypes.PlatformEnvEnum.PROD

console.info('checkInitSdk init', initConfig)
await RTCXSDKInit.init(initConfig)



示例

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'

// 初始换SDK,appKey和appSecret是平台分配给PC SDK,在系统中的唯一标识及对应的密钥
const appKeyConfig = new RTCXSDKInitTypes.AppKeyConfig()
appKeyConfig.appKey = 'appKey'
appKeyConfig.appSecret = 'appSecret'

//定制环境初始化 开始
appKeyConfig.dataCenterScheme = RTCXCommonTypes.ServerSchemeEnum.HTTPS
appKeyConfig.dataCenterDomain = '定制环境域名'
//如果没有域名分配模块。直接是网关,设置为:DataCenterDomainTypeEnum.GATEWAY
appKeyConfig.dataCenterDomainType = RTCXCommonTypes.DataCenterDomainTypeEnum.GATEWAY
appKeyConfig.domainAutoWidthAK = false
appKeyConfig.signalingScheme = RTCXCommonTypes.ServerSchemeEnum.WSS
appKeyConfig.signalingDomain = '定制环境信令域名'
//定制环境初始化 结束

const initConfig = new RTCXSDKInitTypes.InitConfig()
initConfig.appKeyConfig = appKeyConfig
initConfig.debug = false //需要时开启
initConfig.productEnv = RTCXCommonTypes.PlatformEnvEnum.PROD

// 默认国内, 海外设置为RTCXCommonTypes.RegionTypeEnum.REGION_ALL
initConfig.regionType = RTCXCommonTypes.RegionTypeEnum.REGION_CHINA_ONLY

//强制设置语言 [可选]
if(initConfig.regionType === RTCXCommonTypes.RegionTypeEnum.REGION_ALL){
//RTCXSDKInit.setLocale(RTCXCommonTypes.LocaleEnum.en_US)
} else {
//RTCXSDKInit.setLocale(RTCXCommonTypes.LocaleEnum.zh_CN)
}

console.info('checkInitSdk init', initConfig)
await RTCXSDKInit.init(initConfig)
//设置为海外时,需要设置国家 SDK初始化成功后,设置完国家/地区和RegionId(RegionId可选)后,方可注册/登录
if(initConfig.regionType === RTCXCommonTypes.RegionTypeEnum.REGION_ALL){
const country = new RTCXCommonTypes.Country('1', 'MeiGuo', 'USA', '美国', 'US')
RTCXSDKInit.setCountry(country)
}

/**
* app配置选项
* @public
*/
declare class AppKeyConfig {
/**
* 数据中心服务协议
*/
dataCenterScheme?: ServerSchemeEnum;
/**
* 数据中心服务域名
*/
dataCenterDomain?: string;
/**
* 数据中心服务域名类型
*/
dataCenterDomainType?: DataCenterDomainTypeEnum;
/**
* 自动加上AK
*/
domainAutoWidthAK?: boolean;
/**
* 信令协议
*/
signalingScheme?: ServerSchemeEnum;
/**
* 信令域名
*/
signalingDomain?: string;
/**
* STUN服务地址
*/
stunAddress?: string;
/**
* 控制台创建的应用key
*/
appKey: string;
/**
* 控制台创建的应用Secret
*/
appSecret: string;
}

反初始化

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'
RTCXSDKInit.uninit()

设置国际化语言

AIRTC平台目前支持中文(zh_CN)、 英文(en_US)、法文(fr_FR)、德文(de_DE)、日文(ja_JP)、韩文(ko_KR)、西班牙文(es_ES)、俄文(ru_RU)、意大利文(it_IT),共9种语言。

  • 设置语言

统一切换API网关、用户账号、推送、插件等SDK的语言环境。

如果未设置,默认是浏览器设置的网站使用首选语言

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'

RTCXSDKInit.setLocale(RTCXCommonTypes.LocaleEnum.en_US)
  • 获取当前语言

    获取API网关、用户账号、推送、插件等SDK的语言环境。

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'

// 查看SDK当前设置的语言,如果您没有设置过语言,此处会返回当前浏览器设置的网站使用首选语言
RTCXSDKInit.getLocale()

设置国家地区

注意

接入区域配置为中国大陆

  • 默认中国,可不需要设置国家地区

接入区域配置为海外

  • 针对内置账号体系:App需要调用AppSDK提供的获取国家地区列表供用户选择
  • 针对自有账号体系:App需要在用户注册/登录前显示国家地区列表供用户选择,并在自有云平台存储
  • SDK初始化成功后,设置完国家/地区和RegionId(RegionId可选)后,方可注册/登录。参考:推荐对接方案

通过用户选择的国家地区,帮助您选择业务服务器最快连接的区域。

1.设置国家地区

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'


//设置国家或地区
RTCXSDKInit.setCountry(country: RTCXCommonTypes.Country)

//设置国家和地区及RegionId(指定用户所在数据中心编号)
RTCXSDKInit.setCountry(country: RTCXCommonTypes.Country, regionId: number)

2.获取国家地区

const country: RTCXCommonTypes.Country | undefined = RTCXSDKInit.getCountry()

获取当前连接的服务器ID

当自有应用可以连接多个业务服务器时,为了App最佳的体验效果,可以根据当前登录服务器来选择业务服务器。此时,您可以通过以下接口获取App当前连接的国家(即登录服务器ID),从而帮助您选择其他业务服务器最快连接的区域。

import {
RTCXSDKInit,
RTCXSDKInitTypes,
RTCXCommonTypes,
} from '@rtcx/websdk'

RTCXSDKInit.getRegionId()