作者:晖之月 | 来源:互联网 | 2023-02-02 20:53
我正在创建使用JsSIP库来应答通过VoIP SIP提供商进行的呼叫的React应用程序.
我已经创建了一个有两个按钮的页面(Accept和Reject).它成功在SIP服务器上注册SIP客户端.它也成功接听电话,我可以回答.但接听电话时我什么都没听到.
注册JsSIP客户端(willReceiveProps
因为我有道具更改后的连接信息):
const socketHost = 'wss://' + contactCenter.host + ':' + contactCenter.port
const socket = new JsSIP.WebSocketInterface(socketHost)
const cOnfiguration= {
sockets: [socket],
uri: 'sip:' + contactCenter.login + '@' + contactCenter.host,
password: contactCenter.password,
socketHost: socketHost,
}
const coolPhOne= new JsSIP.UA(configuration)
coolPhone.on('connected', (e: any) => {
const messages = ServiceContainer.get(ServiceTypes.Messages)
messages.addSuccess('SIP connected')
})
coolPhone.on('newRTCSession', (e: any) => {
const messages = ServiceContainer.get(ServiceTypes.Messages)
messages.addAlert('New call')
const session = e.session
session.on('failed', this.resetLocalState)
session.on('ended', this.resetLocalState)
const numberRegexp = /\"(\d+)\"/
const fromNumber = (numberRegexp.exec(e.request.headers.From[0].raw))[1]
const tOnumber= (numberRegexp.exec(e.request.headers.Contact[0].raw))[1].slice(1)
this.setState({
callReceived: true,
callSession: session,
fromNumber: fromNumber,
toNumber: toNumber,
})
})
coolPhone.start()
处理答案按钮的方法点击:
private answerCall = () => {
const messages = ServiceContainer.get(ServiceTypes.Messages)
messages.addSuccess('Call answered')
const callOptiOns= {
mediaConstraints: {
audio: true, // only audio calls
video: false
},
pcConfig: {
iceServers: [
{ urls: ["stun:stun.l.google.com:19302"] }
],
iceTransportPolicy: "all",
rtcpMuxPolicy: "negotiate"
}
}
this.state.callSession.answer(callOptions)
this.state.callSession.connection.addEventListener('addstream', (event: any) => {
console.log(event)
this.audioElement.srcObject = event.stream
})
this.audioElement.play()
this.setState({
callAnswered: true,
callReceived: false,
})
}
我做错了什么?
1> Michael Sivo..:
我解决了这个问题.
问题在于this.audioElement.play()
线的位置.
我将它移动到addstream
事件的回调:
this.state.callSession.connection.addEventListener('addstream', (event: any) => {
console.log(event)
this.audioElement.srcObject = event.stream
this.audioElement.play()
})
现在它工作正常.希望你也觉得它很有用.