我正在尝试使用可编程语音API,以便通过我的页面进行通话(响应) 遵循官方指南。 并且有一个奇怪的错误。
因此在组件安装中,我正在检查令牌是否存在,然后将其用于连接设备设置
import { Device } from 'twilio-client';
.....
在组件中:
let device = new Device();
const [callState, setCallState] = useState({
muted: false,
log: 'Connecting...',
onPhone: false,
currentNumber: '',
isValidNumber: false,
active: false
})
....
useEffect(() => {
console.log('Doing some stuff on Component mount')
if (token) {
device.setup(token, { closeProtection: true });
setCallState(prevState => ({
...prevState,
active: true
}));
} else {
//GET TOKEN
}
// Configure event handlers for Twilio Device
//TODO: Outdated bit, for change
device.disconnect(() => {
setCallState(prevState => ({
onPhone: false,
log: 'Call ended.'
}));
});
device.ready(() => {
setCallState(prevState => ({
log: 'Connected'
}));
});
}, [token])
然后在“呼叫”处理程序中,我这样呼叫device.connect
:
const handleToggleCall = () => {
if (callState.onPhone) {
device.disconnectAll();
setCallState(prevState => ({
...prevState,
onPhone: false
}));
} else {
device.connect({ number: "SOME NUMBER" });
setCallState(prevState => ({
...prevState,
muted: false,
onPhone: true
}));
}
}
致电handleToggleCall
时出现此错误:
Error: Call Device.setup() before connect
指向Device.__throwUnlessSetup.
private _throwUnlessSetup(methodName: string) {
if (!this.isInitialized) { throw new InvalidStateError(`Call Device.setup() before ${methodName}`); }
}
据我了解,它甚至没有想到,根据网络选项卡,它请求所有dhtm文件都没有成功初始化,作为初始化的附带证据。