javascript - Tokbox 一对一视频通话

标签 javascript php api opentok tokbox

我正在研究 Tokbox 视频通话流程。现在我正在使用 Tokbox 的示例套件,它工作正常,但它向我显示了所有事件用户视频。

但我需要一个用户可以与另一用户进行视频通话。

我的意思是我需要一对一的视频通话流程。在 Tokbox 中可以吗?所以请大家帮忙解决一下。

这是我的代码

use Slim\Slim;
use Gregwar\Cache\Cache;
use OpenTok\OpenTok;

if(!empty($userid))
{
$autoloader = __DIR__.'/../../../component/tokbox/vendor/autoload.php'; 
if (!file_exists($autoloader)) {
  die('You must run `composer install` in the sample app directory');
}

require($autoloader);



// PHP CLI webserver compatibility, serving static files
$filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
if (php_sapi_name() === 'cli-server' && is_file($filename)) {
    return false;
}


// Initialize Slim application
$app = new Slim(array(
    'templates.path' => __DIR__
));

// Intialize a cache, store it in the app container
$app->container->singleton('cache', function() {
    return new Cache;
});

// Initialize OpenTok instance, store it in the app contianer
$app->container->singleton('opentok', function () {
    return new OpenTok('***', '****');
});
// Store the API Key in the app container
$app->apiKey = '45833942';
$id=$this->uri->segment('3');
$urlname=$this->uri->segment('4');
// Configure routes
$app->get('/home/livechat/'.$id.'/'.$urlname.'/', function () use ($app) {

    // If a sessionId has already been created, retrieve it from the cache
    $sessionId = $app->cache->getOrCreate('sessionId', array(), function() use ($app) {
        // If the sessionId hasn't been created, create it now and store it
        $session = $app->opentok->createSession();
        return $session->getSessionId();
    });

    // Generate a fresh token for this client
    $token = $app->opentok->generateToken($sessionId);

/*$this->db->select('activemember');
$this->db->from('pshy_videocat');
$psychics=$this->db->get();
$totaluseractive= $psychics->row();
$totalactivevideouser=$totaluseractive->activemember;*/


   ?>

 <input type="hidden" id="connectionCountField" value="0"></input>
 <!--button onclick="myFunction()">Toggle Video</button-->
 <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
 <script src="https://static.opentok.com/v2/js/opentok.js" charset="utf-8"></script>

 <script charset="utf-8">
 var publisher;
 var connectionCount = 0;

 var apiKey = '<?php echo '45833942'; ?>';
 var sessionId = '<?php echo $sessionId; ?>';
 var token = '<?php echo $token; ?>';

 var subscribeoptions = {width: 664, height: 421, insertMode: 'append'}

 var session = OT.initSession(apiKey, sessionId)
 .on('streamCreated', function(event) {
 session.subscribe(event.stream,'myPublisherDiv', subscribeoptions);
 })
 .connect(token, function(error) { 

 var publisherOptions = {
  insertMode: 'append',
  width: 150,
  height: 150,
  publishAudio:true, 
  publishVideo:true,
  name: "You"
};


 publisher = OT.initPublisher('mycam', publisherOptions);
 session.publish(publisher);
 });

 session.on("connectionCreated", function(event) {
   connectionCount++;
   displayConnectionCount();
});

session.on("connectionDestroyed", function(event) {
   connectionCount--;
   displayConnectionCount();
});

function displayConnectionCount() {
    document.getElementById("connectionCountField").value = connectionCount.toString();
    /*var newdata=connectionCount.toString();
      $.ajax({
            url:$('#baseUrl').val()+"home/updateactiveuser",
            type:'post',
            data: {newdata:newdata}
        })*/
}


 var enableVideo=true;

 function myFunction() {
 if(enableVideo)
 {
 publisher.publishVideo(false);
 enableVideo=false;
 } else
 {
 publisher.publishVideo(true);
 enableVideo=true;
 }
 }
 </script>


<?php 
});

$app->run();
}
?>

谢谢

最佳答案

这是一个示例代码。我没有为此使用过 SLIM。但这也可以用 Slim 来完成。我已执行该脚本 5 次,每次我都会获得一个唯一的 session ID。

收到 session ID -

Session Id Got : 1_MX40NTgzMzk0Mn5-MTQ5NDMyMzQ0NzU0NH5KNk9Gcy9FSktPSlUwdldUbURwazJ0Umd-QX4
Session Id Got : 2_MX40NTgzMzk0Mn5-MTQ5NDMyMzQ3ODMzM35rWWU5NDV1ZjZPMGhLc3pCS3pRSERJY0h-QX4
Session Id Got : 1_MX40NTgzMzk0Mn5-MTQ5NDMyMzQ5NTcwOX5kc0Q3NDBjQSthOWJaMEk1eUllU3dCY0t-QX4
Session Id Got : 2_MX40NTgzMzk0Mn5-MTQ5NDMyMzUwNzExOH5NMEZuZWRyejBZYnZRVk1zSEczNldocmV-QX4
Session Id Got : 1_MX40NTgzMzk0Mn5-MTQ5NDMyMzUxNzE3NH5Yc0hyMUlacmFqK25pVzhXNDI5NTV6eDB-QX4

普通 PHP 脚本 -

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

require 'vendor/autoload.php';
use OpenTok\OpenTok;

$apiKey = '45833942';
$apiSecret = '9727d4ae20e8695a8f787bc58c0b4a9ebf6aae6e';
$opentok = new OpenTok($apiKey, $apiSecret);

use OpenTok\MediaMode;
use OpenTok\ArchiveMode;


// An automatically archived session:
$sessionOptions = array(
    'archiveMode' => ArchiveMode::ALWAYS,
    'mediaMode' => MediaMode::ROUTED
);
$session = $opentok->createSession($sessionOptions);


// Store this sessionId in the database for later use
$sessionId = $session->getSessionId();


echo "Session Id Got : $sessionId";

修身版 -

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use OpenTok\OpenTok;
use OpenTok\MediaMode;
use OpenTok\ArchiveMode;

require 'vendor/autoload.php';

$app = new \Slim\App;

$container = $app->getContainer();

$container['opentok'] = function ($c) {
    $apiKey = '45833942';
    $apiSecret = '9727d4ae20e8695a8f787bc58c0b4a9ebf6aae6e';

    $opentok = new OpenTok($apiKey, $apiSecret);
    return $opentok;
};


$app->get('/', function (Request $request, Response $response) {
    // An automatically archived session:
    $sessionOptions = array(
        'archiveMode' => ArchiveMode::ALWAYS,
        'mediaMode' => MediaMode::ROUTED
    );
    $session = $this->opentok->createSession($sessionOptions);

    // Store this sessionId in the database for later use
    $sessionId = $session->getSessionId();

    $response->getBody()->write("Session Id Got : $sessionId");
    return $response;
});

$app->run();

希望对你有帮助。

引用号:https://tokbox.com/developer/sdks/php/

关于javascript - Tokbox 一对一视频通话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43797405/

相关文章:

javascript - 为什么这个简单的 jQuery 脚本不起作用?

javascript - FIREBASE admin sdk 不返回 promise ? Node JS

javascript - 如果从 Dropdown Knockout js 中选择特定值,则显示 div

javascript - HTML 到 PDF(通过 javascript)如何添加 css 或表格?

php - 如何在 PHPUnit 中的测试之间共享夹具

php - 如何在 URL 中传递电子邮件地址的 md5 哈希值?

java - 如何使用不记名 token 身份验证配置 Glide 以下载图像文件?

php - 在 Gmail 帐户上使用 PHPMailer 添加自签名证书

javascript - 在 Highcharts 中添加超过 3 个 y 轴。不显示第 4 个 y 轴值

php - 如何连接到营销事件监控 API?