58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import WebRtcModel, { WebRtcState } from './model/WebRtcModel';
|
|
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
|
|
import common from '@ohos.app.ability.common';
|
|
import prompt from '@ohos.prompt'
|
|
import ethernet from '@ohos.net.ethernet';
|
|
|
|
const TAG: string = '[webRTCVoice]';
|
|
|
|
export default class WebRTCVoice{
|
|
|
|
private context:common.UIAbilityContext;
|
|
private atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
|
|
private webRtcModel: WebRtcModel = new WebRtcModel()
|
|
|
|
//TODO ourId待替换
|
|
private ourId: string = 'surenjun'
|
|
private peerId: string = ''
|
|
private state: string = '暂未连接'
|
|
|
|
constructor(context) {
|
|
this.context = context;
|
|
this.init()
|
|
}
|
|
|
|
handleStateChange = (state, message) => {
|
|
console.log(TAG, "on state change, message:" + message);
|
|
this.state = message;
|
|
}
|
|
|
|
init = () => {
|
|
const {webRtcModel,ourId,context,handleStateChange} = this;
|
|
webRtcModel.setContext(context);
|
|
webRtcModel.initWebSocket(ourId, handleStateChange);
|
|
webRtcModel.initRtc();
|
|
|
|
try {
|
|
this.atManager.requestPermissionsFromUser(context, ['ohos.permission.MICROPHONE'], (err, data) => {
|
|
console.info('data:' + JSON.stringify(data));
|
|
console.info('data permissions:' + data.permissions);
|
|
console.info('data authResults:' + data.authResults);
|
|
});
|
|
// prompt.showToast({
|
|
// message: '语音对讲初始化完成',
|
|
// duration: 2000
|
|
// });
|
|
} catch (err) {
|
|
console.log(`catch err->${JSON.stringify(err)}`);
|
|
}
|
|
}
|
|
|
|
aboutToDisappear = () => {
|
|
const {webRtcModel} = this;
|
|
webRtcModel.disappearWebSocket()
|
|
}
|
|
|
|
}
|
|
|