接收入站呼叫Twilio时出错

时间:2016-02-24 12:04:48

标签: twilio

当使用twilio接收到我的浏览器的入站呼叫时,第一次初始呼叫有效但如果我再次尝试向我的浏览器进行入站呼叫,则会出现twilio错误"创建应答时出错:未能设置本地答案sdp:调用错误状态:STATE_INPROGRESS

enter image description here

1 个答案:

答案 0 :(得分:1)

来自Twilio的梅根在这里。

以下code snippet extends from the quickstart改变了我们处理事件监听器的方式,这对于收到相同错误并描述了无法接受后续调用的类似行为的人起了作用。

<?php
include '../Twilio/Services/Twilio/Capability.php';

// put your Twilio API credentials here
$accountSid = 'ACxxxxx';
$authToken  = 'aaxxxxx';

// put your Twilio Application Sid here
$appSid     = 'APxxxxx';

$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming('alex');
$token = $capability->generateToken();
// put your Twilio Application Sid here
$appSid     = 'APxxxxx';

?>

<!DOCTYPE html>
<html>
<head>
    <title>Twilio Client</title>
    <script type="text/javascript" src="//static.twilio.com/libs/twiliojs/refs/6359b40/twilio.js"></script>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <script type="text/javascript">
    $( document ).ready(function() {
        $("#answer").hide();
        $("#hangup").hide();
        $("#reject").hide();

        Twilio.Device.setup("<?php echo $token; ?>", { debug: true});


        Twilio.Device.ready(function (device) {
            $("#log").text("Ready");
        });

        Twilio.Device.error(function (error) {
            $("#log").text("Error: " + error.message);
        });

        Twilio.Device.connect(function (conn) {
            $("#log").text("Successfully established call");
            $("#call").hide();
            $("#reject").hide();
            $("#hangup").show();

        });

        Twilio.Device.disconnect(function (conn) {
            $("#log").text("Call ended. Ready for new incoming/ outgoing calls.");
            $("#hangup").hide();
            $("#call").show();
        });


        /**
        * Ideally, we don't want to set a listener every time
        * Twilio.Device.incoming is called. Instead, we should
        * set a listener once and keep track of the current
        * connection.
        */
        var connection = null;

        Twilio.Device.incoming(function(conn) {
            // Set the reference to the current
            // connection when we receive an
            // incoming call
            connection = conn;
            $("#log").text("Incoming call from " + conn.parameters.From);
            $("#answer").show();
            $("#call").hide();
            $("#reject").show();

            // Clear the reference to the
            // connection when disconnected.
            connection.disconnect(function() {
                connection = null;
            })
        });

        Twilio.Device.cancel(function(conn) {
            $("#log").text("Ready");
            $("#answer").hide();
            $("#reject").hide();
            $("#call").show();
        });

        $("#reject").click(function() {
            $("#log").text("Incoming call is rejected. Ready for new incoming/ outgoing calls.");
            connection.reject();
            $("#answer").hide();
            $("#reject").hide();
            $("#call").show();


        });

        // Set the listener once.
        $("#answer").click(function() {
            // If there's no pending connection,
            // there's nothing to do.
            if (!connection) { return; }

            // Update the interface
            $("#answer").hide();
            $("#call").show();
            $("#log").text("Call accepted");

            // Accept the current connection.
            connection.accept();
        });

        $("#call").click(function() {
            // get the phone number to connect the call to
            params = {"PhoneNumber": $("#number").val()};
            Twilio.Device.connect(params);
        });

        $("#hangup").click(function() {
            Twilio.Device.disconnectAll();
            $("#log").text("Ready");
        });


    });

    </script>
</head>
<body>
<div align="center" style="margin: auto; width:500px; padding: 20px;">
    <div class="form-inline">
        <div class="input-group">
            <div class="input-group-addon"><img src="https://www.twilio.com/bundles/favicons/img/Twilio_16.png" /></div>
            <input type="text" class="form-control" id="number" name="number">
        </div>

        <button class="btn btn-success" id="answer">Accept</button>

        <button class="btn btn-success" id="call">Call</button>

        <button class="btn btn-danger" id="hangup">Hangup</button>

        <button class="btn btn-danger" id="reject">Reject</button>

    </div>
    <br>
    <div id="log" class="alert-info" style="width: 347px; font-size: large;"></div>
</div>


</body>
</html>

我希望这证明是有帮助的。