/** * @description WebSDK 实例化对象 */ let rtc; /** * @method initSDK * @description 初始化SDK方法 * @param { object } data 初始化SDK所需参数 * @returns { object } */ export function initSDK(data) { // eslint-disable-next-line no-undef rtc = new Rtc(data); return rtc; } /** * @method initListener * @description 初始化监听事件 */ export function initListener() { rtc.on('login_success', data => { console.log('登录成功', data); alert('登录成功:' + JSON.stringify(data)); }); rtc.on('login_failed', data => { console.log('登录失败', data); alert('登录失败:' + JSON.stringify(data)); }); rtc.on('conference_join', function () { // 有监听就是加入房间成功 console.log('加入房间成功'); }); rtc.on('conference_join_failed', function (err) { // 加入房间失败 err为错误原因 console.log('加入房间失败', err); }); rtc.on('switch_user_settings', function (settingData) { // 单个用户配置监听 console.log(settingData); }); rtc.on('allow_sub', stream => { alert('监听到有流'); rtc.trySubscribeStream({ tryStream: stream, success: function success(stream) { stream.show('other'); }, fail: function fail(err) { console.error(err); } }); }); } /** * @method startLive * @description 开启直播 */ export function startLive() { rtc.startLive({ success(data) { alert('开启直播成功:' + JSON.stringify(data)); }, fail(data) { alert('开启直播失败:' + JSON.stringify(data)); } }); } /** * @method endLive * @description 结束直播 */ export function endLive() { rtc.endLive({ success(data) { alert('结束直播成功:' + JSON.stringify(data)); }, fail(data) { alert('结束直播失败:' + JSON.stringify(data)); } }); } /** * @method createLocalStram * @description 创建本地流 */ export function createLocalStream() { const createData = { video: true, audio: true }; rtc.createLocalStream({ streamName: 'main', createData, success: function (stream) { // 创建本地流成功,将流展示到id为 live 的dom元素盒子中 stream.show('live'); // publish(stream); // 如果需要立即推流,执行publish方法 alert('创建本地流成功'); }, fail: function (data) { // 创建本地流失败,应用层处理 console.log(data); alert('创建本地流失败'); } }); } /** * @method publishStream * @description 推流 */ export function publishStream() { rtc.publish({ streamName: 'main', // 推流成功,更新上麦结果 success: function (stream) { rtc.updateMcResult({ pid: 1, stid: stream.id(), success: function (data) { alert('更新上麦结果请求成功,此处可处理应用层逻辑' + JSON.stringify(data)); }, fail: function (data) { alert('更新上麦结果请求失败,此处可处理应用层逻辑' + JSON.stringify(data)); } }); }, fail: function (stream) { // 推流失败,更新上麦结果 rtc.updateMcResult({ pid: 0, stid: stream.id(), success: function (data) { console.log('更新上麦结果请求成功,此处可处理应用层逻辑', data); }, fail: function (data) { console.log('更新上麦结果请求失败,此处可处理应用层逻辑', data); } }); alert('推流失败,此处可处理应用层逻辑'); } }); } /** * @method handsUp * @description 申请连麦 */ export function handsUp(data) { rtc.handsUp(data); }