Twilio:Twilio.Device.connect()没有按下语音请求URL

时间:2014-11-24 19:39:24

标签: java javascript twilio twilio-click-to-call

在我的前端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());
    }
}

1 个答案:

答案 0 :(得分: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);
    }
}