2017-09-13 148 views
1

我正在尝试使用REST Api(php)在Twilio中创建一个点对点聊天室。代码如下:Twilio令牌和使用REST API的对等聊天室

<?php 
require_once 'Twilio/autoload.php'; 
use Twilio\Rest\Client; 
use Twilio\Jwt\AccessToken; 
use Twilio\Jwt\Grants\VideoGrant; 
include_once 'config.inc.php'; 
$identity = "alice"; 
$client = new Client($TWILIO_API_KEY, $TWILIO_API_SECRET); 
$roomName = $client->video->rooms->create([ 
    'uniqueName' => 'TestRoom2', 
    'type' => 'peer-to-peer', 
    'enableTurn' => false, 
    'Duration' => 300, 
    'MaxParticipants' => 2, 
    'statusCallback' => 'http://example.org' 
]); 
//echo $roomName->status; 
//token 
$token= new AccessToken($TWILIO_ACCOUNT_SID, $TWILIO_API_KEY, $TWILIO_API_SECRET, 300, $identity); 
// Create Video grant 
$videoGrant = new VideoGrant(); 
$videoGrant->setRoom($roomName); 
// Add grant to token 
$token->addGrant($videoGrant); 
// return serialized token 
echo $token->toJWT(); 
?> 

我只使用该代码在其例如通过Twilio为提供: https://www.twilio.com/docs/api/video/rooms-resource

对等网络间建立。

虽然测试在生成的web令牌的数据有效载荷: https://jwt.io/

它显示房间空白。

{ 
    "jti": "SK1ddcfb6782fa358cb5e2306f8875ac1d-1505266888", 
    "iss": "SK1ddcfb6782fa358cb5e2306f8875ac1d", 
    "sub": "AC6c23ea48bd7d6bd681d21301f35c22b6", 
    "exp": 1505267188, 
    "grants": { 
    "identity": "alice", 
    "video": { 
     "room": {} 
    } 
    } 
} 

如果我创建一个房间使用以下,它工作正常。

$roomName = "TestRoom"; 

的问题是与代码:

$client = new Client($TWILIO_API_KEY, $TWILIO_API_SECRET); 
$roomName = $client->video->rooms->create([ 
    'uniqueName' => 'TestRoom2', 
    'type' => 'peer-to-peer', 
    'enableTurn' => false, 
    'Duration' => 300, 
    'MaxParticipants' => 2, 
    'statusCallback' => 'http://example.org' 
]); 

什么是错误的,我Twilio对等网络室代码? Twilio需要太多的时间来回应,支持不太好。他们还没有提供简单的例子,只是一个令人困惑的节点js例子。

请求帮助。

回答

1

看起来好像你正在将一个房间对象传递给setRoom,但setRoom只需要一个字符串(房间的名称)。

你可能想是这样的(注意,使用$roomName对比$room的):

$roomName = 'TestRoom2'; 
$room = $client->video->rooms->create([ 
    'uniqueName' => $roomName, 
    'type' => 'peer-to-peer', 
    'enableTurn' => false, 
    'Duration' => 300, 
    'MaxParticipants' => 2, 
    'statusCallback' => 'http://example.org' 
]); 
$token = new AccessToken($TWILIO_ACCOUNT_SID, $TWILIO_API_KEY, $TWILIO_API_SECRET, 300, $identity); 
$videoGrant = new VideoGrant(); 
$videoGrant->setRoom($roomName); 
$token->addGrant($videoGrant); 
echo $token->toJWT(); 
+0

太感谢你了! – Pamela