zby-live-sdk
Version:
This is a live SDK for weclassroom.
167 lines (155 loc) • 5.12 kB
JavaScript
import zbyChannelObj from './zby';
import pomeloChannelObj from './pomelo';
import getSendMsgParams from './getSendMsgParams';
import dict from '../util/dict';
import defaultApi from '../default';
import {
userAndDeviceStatusInfo
} from '../util/statsStore';
import {
getCloudData
} from '../network/api';
import zbysdk from '../zby-live-sdk';
import globalConfig from '../config/config';
const channelObjs = {
1: zbyChannelObj,
2: pomeloChannelObj
};
let channelObj = null;
class ChannelClient {
constructor() {
this.initParams = null;
}
// 加入房间
/**
*
* @param {object} params
* @param {string} params.userId 用户id
* @param {string} params.roomId 房间id
* @param {string} params.host 信道链接地址
* @param {string} params.userName 用户名
* @param {string} params.role 角色 teacher/student/assitant
* @param {string} params.institutionId 机构
* @param {function} cb 回调函数
* @param {number} channelType 信道类型 1=直播云信道 2=pomelo
*/
async joinRoom(params, cb, channelType = 1) {
if (channelObj) {
defaultApi.writeLog('channel has init');
return;
}
// 设置初始化参数
this.initParams = params;
// 设置当前信道对象,使用直播云还是pomelo
channelObj = channelObjs[channelType];
defaultApi.writeLog('channel_log : choose channel and start joining ......');
// 加入房间
channelObj.joinRoom(params, cb);
this.sendJoin(params);
}
// 离开房间
async leaveRoom() {
if (!channelObj) {
defaultApi.writeLog('channel has not init');
return;
}
// 离开房间
channelObj.leaveRoom();
channelObj = null;
}
// 发送信道消息
sendChannelMsg(msg) {
if (!globalConfig.usePomelo) {
return;
}
if (!channelObj) {
defaultApi.writeLog('channel has not init');
return;
}
channelObj.sendChannelMsg(msg);
}
// 发送进房消息,是自己加入房间群发的消息
sendJoin(args) {
if (!globalConfig.usePomelo) {
return;
}
this.sendChannelMsg(getSendMsgParams.sendJoin(args));
}
// 发送离开房间消息,是收到别人进入房间然后定向发的消息
sendLeave(args) {
if (!globalConfig.usePomelo) {
return;
}
this.sendChannelMsg(getSendMsgParams.sendLeave(args));
}
// 发送进房ack消息
sendJoinAck(args) {
if (!globalConfig.usePomelo) {
return;
}
this.sendChannelMsg(getSendMsgParams.sendJoinAck(args));
}
// 发送推流消息,addstream自己推流成功会发,如果有个人进来你会单独给他发,没有ack
sendAddStream(args) {
if (!globalConfig.usePomelo) {
return;
}
this.sendChannelMsg(getSendMsgParams.sendAddStream(args));
}
// 发送设备状态更新消息
sendUpdateStreamDeviceStatus(args) {
if (!globalConfig.usePomelo) {
return;
}
this.sendChannelMsg(getSendMsgParams.sendUpdateStreamDeviceStatus(args));
}
// 发送停止推流消息
sendRemoveStream(args) {
if (!globalConfig.usePomelo) {
return;
}
this.sendChannelMsg(getSendMsgParams.sendRemoveStream(args));
}
//断线重连时,加入小组
async reJoin() {
if (!globalConfig.usePomelo) {
return;
}
dataReport.pomeloState({state: 3});
//获取当前小组sdk类型
const cloudResponse = await getCloudData(window.zby_sdk_req_get_cloud_params);
const new_target_sdk_type = cloudResponse.data.sdkName;
if (window.target_sdk_type) {
//正在切换中,等待切换
defaultApi.writeLog('reJoin changing -- wait_changed');
return;
} else if (!window.target_sdk_type && window.current_sdk_type !== new_target_sdk_type) {
//未在切换中,且当前SDK类型和小组不一致:进行切换SDK
defaultApi.writeLog(`reJoin : 断线重连,准备执行切换 SDK to ${new_target_sdk_type}`);
zbysdk.changeSDK(1);
} else {
const {roomId, userId, userName, institutionId, role} = this.initParams;
//一致:重进
defaultApi.writeLog('reJoin : SDK一致, 重进');
const baseInfo = {
roomId,
userId,
userName,
institutionId,
role
};
this.sendJoin(baseInfo);
this.sendAddStream(baseInfo);
this.sendUpdateStreamDeviceStatus(Object.assign({}, baseInfo, {
deviceType: dict.getDeviceTypeKey('video'),
deviceStatus: dict.getDeviceStatusKey(userAndDeviceStatusInfo && userAndDeviceStatusInfo.camera ? 'open' : 'closed'),
}));
this.sendUpdateStreamDeviceStatus(Object.assign({}, baseInfo, {
deviceType: dict.getDeviceTypeKey('audio'),
deviceStatus: dict.getDeviceStatusKey(userAndDeviceStatusInfo && userAndDeviceStatusInfo.microPhone ? 'open' : 'closed'),
}));
}
}
}
const channelClient = new ChannelClient();
export default channelClient;