2

在我的前端 Javascript 代码中,我调用Twilio.Device.connect(),它没有向我的语音请求 URL 发出请求。我不确定这里发生了什么。我确保我事先设置了我的能力令牌,并且没有错误,但它仍然不起作用。这是前端JS代码。

            Twilio.Device.setup(resp.token);
            Twilio.Device.connect({autoDial: true});

            // respond to "connect" event

            Twilio.Device.connect(function (conn) {
                alert("Got here!");
            }

这也是我生成令牌的代码。

public static void getToken()
{
    TwilioCapability t = new TwilioCapability(ACCOUNT_SID, AUTH_TOKEN);

    t.allowClientOutgoing(APP_SID);
    t.allowClientIncoming("test");

    try {
        throw new OKResponse(ImmutableMap.of("token", t.generateToken(3600)));
    } catch (DomainException e) {
        Logger.error(e, "Error generating twilio token: %s", e.getMessage());
    }
}
4

1 回答 1

1

我有同样的问题,

您需要在调用allowClientOutgoing() 和allowClientIncoming( ) 之后调用函数generateToken( ) 以便Services_Twilio_Capability() 创建的对象具有应用链接。

这有效:

$objToken = new Services_Twilio_Capability($accountSid, $authToken);
$objToken->allowClientOutgoing('APXXXXXXXXXX');
$objToken->allowClientIncoming($_REQUEST['name']);
$strToken = $objToken->generateToken();

这不会:

$objToken = new Services_Twilio_Capability($accountSid, $authToken);
$strToken = $objToken->generateToken();
$objToken->allowClientOutgoing('APXXXXXXXXXX');
$objToken->allowClientIncoming($_REQUEST['name']);

此外,它不会抛出错误,但您的 js 将始终显示为“断开连接”

更新

这是我的后端的编辑:

   /**
     * Create an instance of Services_Twilio_Capability();
     *
     * @return object
     */
    private function instantiateCapability(){
        if(is_null($this->objCapability))
            $this->objCapability = new \Services_Twilio_Capability(TWILIO_ID, TWILIO_KEY);
        return $this->objCapability;
    }

    /**
     * Generate a token
     *
     * @link http://twilio-php.readthedocs.org/en/latest/usage/token-generation.html
     * @param bool $boolOutgoing Allow outgoing connections
     * @param bool $boolIncoming Allow incoming connections
     * @return string
     */
    public function generateToken($boolOutgoing = true, $boolIncoming = true){
        $objCapability = $this->instantiateCapability();

        if($boolOutgoing) $objCapability->allowClientOutgoing(TWILIO_SID]);
        if($boolIncoming) $objCapability >allowClientIncoming($_SESSION[$GLOBALS['APP_NAME'] . 'ID']);

        $strToken = $objCapability->generateToken(TOKEN_DURATION);
        return json_encode(array('status' => 1, 'token' => $strToken));
    }

这是前端(AJAX 响应回调):

function(result){
    if(result.status == 1) {
       //Load the twilio object
       Twilio.Device.setup(result.token);
    }
}
于 2014-11-24T21:42:21.953 回答