101 lines
2.7 KiB
TypeScript
101 lines
2.7 KiB
TypeScript
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?.()
|
|
});
|
|
},
|
|
// 销毁
|
|
destroy: async (callback?) => {
|
|
if (client) {
|
|
await client.leave()
|
|
await client.destroy(() => {
|
|
callback?.()
|
|
client = '';
|
|
})
|
|
}
|
|
},
|
|
// 设置角色
|
|
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: () => {
|
|
client.join(option.token, option.channelId, option.uid, () => {
|
|
agora.setRole(false)
|
|
})
|
|
},
|
|
// 刷新token
|
|
renewToken: (token) => {
|
|
client.renewToken(token)
|
|
},
|
|
// 监听
|
|
clientEvent: ({ streamAdded, streamRemoved, tokenRef }) => {
|
|
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()
|
|
});
|
|
},
|
|
// 订阅远端音视频流
|
|
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)
|
|
},
|
|
} |