120 lines
3.3 KiB
TypeScript
120 lines
3.3 KiB
TypeScript
import { role } from "./utils";
|
|
const AgoraMiniappSDK = require("../lib/agora-miniapp-sdk.js");
|
|
const option = {
|
|
appId: '',
|
|
token: '',
|
|
tokenA: '',
|
|
channelId: '',
|
|
screenShareId: '',
|
|
uid: 0
|
|
}
|
|
let client: any = '';
|
|
export const agora = {
|
|
// 初始化
|
|
init: async (callback?) => {
|
|
client = new AgoraMiniappSDK.Client();
|
|
await client.init(option.appId, () => {
|
|
callback?.(true)
|
|
}, () => {
|
|
callback?.(false)
|
|
});
|
|
},
|
|
// 销毁
|
|
destroy: async (callback?) => {
|
|
if (client) {
|
|
await client.leave()
|
|
await client.destroy()
|
|
client = '';
|
|
}
|
|
callback?.()
|
|
},
|
|
// 设置角色
|
|
setRole: async (isRoomManager: boolean, callBack?: Function) => {
|
|
await client.setRole(isRoomManager ? 'broadcaster' : 'audience')
|
|
if (isRoomManager) {
|
|
client.publish((url: string) => {
|
|
callBack && callBack(url)
|
|
})
|
|
}
|
|
},
|
|
// 设置频道参数
|
|
setOption: async (data) => {
|
|
option.token = data.token;
|
|
option.tokenA = data.tokenA;
|
|
option.channelId = data.channelId;
|
|
option.appId = data.appId;
|
|
option.uid = Number(data.uid);
|
|
option.screenShareId = data.screenShareId;
|
|
await agora.init()
|
|
},
|
|
reconnecSetOption: async (token, callback) => {
|
|
option.token = token;
|
|
await agora.init(callback)
|
|
},
|
|
// 加入频道
|
|
joinChannel: async (roleStatus: boolean, callBack: Function) => {
|
|
await client.join(option.token, option.channelId, option.uid, () => {
|
|
agora.setRole(roleStatus, (url) => {
|
|
callBack(url)
|
|
})
|
|
})
|
|
},
|
|
// 监控
|
|
allJoinChannelEx: async (user: any, bool: boolean, callBack: Function) => {
|
|
if (!role.ID.includes(user.roleId) || !user.isRoomManager) {
|
|
await agora.setRole(bool, (url: string) => {
|
|
callBack(url);
|
|
})
|
|
}
|
|
bool ? await agora.unmuteLocal('video') : await agora.muteLocal('video')
|
|
},
|
|
// 刷新token
|
|
renewToken: (token) => {
|
|
client.renewToken(token)
|
|
},
|
|
// 监听
|
|
clientEvent: ({ streamAdded, streamRemoved, tokenRef, updateUrl, error }: any) => {
|
|
client.on("stream-added", async e => {
|
|
await agora.subscribe(e.uid, (url: string, uid: number | string) => {
|
|
streamAdded(url, uid)
|
|
})
|
|
});
|
|
client.on("stream-removed", async e => {
|
|
streamRemoved(e.uid)
|
|
});
|
|
client.on("token-privilege-will-expire", async e => {
|
|
tokenRef()
|
|
});
|
|
client.on("error", async e => {
|
|
error()
|
|
});
|
|
client.on("update-url", async e => {
|
|
updateUrl(e)
|
|
});
|
|
},
|
|
// 订阅远端音视频流
|
|
subscribe: async (uid: number | string, callBack: Function) => {
|
|
const { url, rotation } = await client.subscribe(uid)
|
|
callBack(url, uid)
|
|
},
|
|
// 停止发送本地音视频流
|
|
muteLocal: async (target: string) => {
|
|
await client.muteLocal(target)
|
|
},
|
|
// 恢复发送本地音视频流
|
|
unmuteLocal: async (target: string) => {
|
|
await client.unmuteLocal(target)
|
|
},
|
|
// 设置订阅的视频流类型。 0 大流 1 小流
|
|
setRemoteVideoStreamType: async (uid: number, streamType: 0 | 1) => {
|
|
await client.setRemoteVideoStreamType(uid, streamType)
|
|
},
|
|
// 停止接收远端用户的音视频流。
|
|
mute: async (uid: number, target: string) => {
|
|
await client.mute(uid, target)
|
|
},
|
|
// 恢复接收远端用户的音视频流。
|
|
unmute: async (uid: number, target: string) => {
|
|
await client.unmute(uid, target)
|
|
},
|
|
} |