如何发送 Twilio 连接并在 Twilio Client Javascript SDK 的另一个函数中接受它?
How to send Twilio connection and accept it in another function on Twilio Client Javascript SDK?
我正在使用 Twilio JS SDK quickstart 在我的网站上拨打和接听电话:
https://www.twilio.com/docs/voice/client/javascript/quickstart
https://www.twilio.com/docs/voice/client/javascript/connection
当我接到一个 phone 电话时,我想通过点击一个按钮连接到那个来电,所以这是我的代码:
HTML:
<div id="register"></div>
JS:
function capable(tkn){ //tkn is the token.
var devices;
devices = new Twilio.Device(tkn, {
// Set Opus as our preferred codec. Opus generally performs better, requiring less bandwidth and
// providing better audio quality in restrained network conditions. Opus will be default in 2.0.
allowIncomingWhileBusy: true,
codecPreferences: ['opus', 'pcmu'],
// Use fake DTMF tones client-side. Real tones are still sent to the other end of the call,
// but the client-side DTMF tones are fake. This prevents the local mic capturing the DTMF tone
// a second time and sending the tone twice. This will be default in 2.0.
fakeLocalDTMF: true,
sounds: {
incoming: 'https://www.mywebsite.com/audio/ring-ring.mp3'
}
});
devices.on('incoming', function (conn) {
console.log('Incoming connection from ' + conn.parameters.From);
var telf = conn.parameters.From;
var reg = '<div class="reg_item">';
reg += '<p>Caller: </p>' + telf;
reg += '<button onclick="answerB('+conn+');" class="btn btn-md btn-success"><i class="fa fa-phone" aria-hidden="true"></i></button>';
reg += '</div>';
$("#register").prepend(reg);
}
}
function answerB(idx){
console.log(idx);
idx.accept();
}
它似乎无法以这种方式显示此错误:
控制台:
我该如何解决?
我需要你的帮助。
谢谢。
而不是这个:
console.log('Incoming connection from ' + conn.parameters.From);
var telf = conn.parameters.From;
var reg = '<div class="reg_item">';
reg += '<p>Caller: </p>' + telf;
reg += '<button onclick="answerB(' + conn + ');" class="btn btn-md btn-success"><i class="fa fa-phone" aria-hidden="true"></i></button>';
reg += '</div>';
$("#register").prepend(reg);
更改为:
console.log('Incoming connection from ' + conn.parameters.From);
var telf = conn.parameters.From;
var reg = '<div class="reg_item">';
reg += '<p>Caller: </p>' + telf;
reg += '<button id="button-answer" class="btn btn-md btn-success"><i class="fa fa-phone" aria-hidden="true"></i></button>';
reg += '</div>';
$("#register").prepend(reg);
$("#button-answer").on("click", conn, answerB);
你的answerB
函数应该是:
function answerB(event) {
console.log(event.data);
event.data.accept();
}
如果你成功了,请告诉我。
我正在使用 Twilio JS SDK quickstart 在我的网站上拨打和接听电话: https://www.twilio.com/docs/voice/client/javascript/quickstart
https://www.twilio.com/docs/voice/client/javascript/connection
当我接到一个 phone 电话时,我想通过点击一个按钮连接到那个来电,所以这是我的代码:
HTML:
<div id="register"></div>
JS:
function capable(tkn){ //tkn is the token.
var devices;
devices = new Twilio.Device(tkn, {
// Set Opus as our preferred codec. Opus generally performs better, requiring less bandwidth and
// providing better audio quality in restrained network conditions. Opus will be default in 2.0.
allowIncomingWhileBusy: true,
codecPreferences: ['opus', 'pcmu'],
// Use fake DTMF tones client-side. Real tones are still sent to the other end of the call,
// but the client-side DTMF tones are fake. This prevents the local mic capturing the DTMF tone
// a second time and sending the tone twice. This will be default in 2.0.
fakeLocalDTMF: true,
sounds: {
incoming: 'https://www.mywebsite.com/audio/ring-ring.mp3'
}
});
devices.on('incoming', function (conn) {
console.log('Incoming connection from ' + conn.parameters.From);
var telf = conn.parameters.From;
var reg = '<div class="reg_item">';
reg += '<p>Caller: </p>' + telf;
reg += '<button onclick="answerB('+conn+');" class="btn btn-md btn-success"><i class="fa fa-phone" aria-hidden="true"></i></button>';
reg += '</div>';
$("#register").prepend(reg);
}
}
function answerB(idx){
console.log(idx);
idx.accept();
}
它似乎无法以这种方式显示此错误:
控制台:
我该如何解决?
我需要你的帮助。
谢谢。
而不是这个:
console.log('Incoming connection from ' + conn.parameters.From);
var telf = conn.parameters.From;
var reg = '<div class="reg_item">';
reg += '<p>Caller: </p>' + telf;
reg += '<button onclick="answerB(' + conn + ');" class="btn btn-md btn-success"><i class="fa fa-phone" aria-hidden="true"></i></button>';
reg += '</div>';
$("#register").prepend(reg);
更改为:
console.log('Incoming connection from ' + conn.parameters.From);
var telf = conn.parameters.From;
var reg = '<div class="reg_item">';
reg += '<p>Caller: </p>' + telf;
reg += '<button id="button-answer" class="btn btn-md btn-success"><i class="fa fa-phone" aria-hidden="true"></i></button>';
reg += '</div>';
$("#register").prepend(reg);
$("#button-answer").on("click", conn, answerB);
你的answerB
函数应该是:
function answerB(event) {
console.log(event.data);
event.data.accept();
}
如果你成功了,请告诉我。