无法将两个客户端连接到Tokbox中的同一会话

时间:2017-06-04 14:39:15

标签: javascript php opentok tokbox

我正在尝试使用tokbox添加视频流服务。

但是用户无法相互连接。我按照他们提供的教程。这是我的代码。出于测试目的,我将会话ID保存在当前文件中。

客户一:

$opentok    =   new OpenTok($apiKey, $apiSecret);
$session    =   $opentok->createSession();
$sessionId  =   $session->getSessionId();
$token      =   $opentok->generateToken($sessionId);

$file = fopen("session.txt","w+");
fwrite($file, $sessionId);
fclose($file);
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>OpenTok</title>
</head>
<body>

    <h2>Hello, World!</h2>
    <div id="publisher"></div>
    <div id="subscribers"></div>

</body>
</html>

<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>
<script>

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

    // Initialize an OpenTok Session object.
    var session = OT.initSession(sessionId);

    // Initialize a Publisher, and place it into the 'publisher' DOM element.
    var publisher = OT.initPublisher(apiKey, 'publisher');

    session.on('streamCreated', function(event) {
      // Called when another client publishes a stream.
      // Subscribe to the stream that caused this event.
      session.subscribe(event.stream, 'subscribers', { insertMode: 'append' });
    });

    // Connect to the session using your OpenTok API key and the client's token for the session
    session.connect(apiKey, token, function(error) {
      if (error) {
        console.error(error);
      } else {
        // Publish a stream, using the Publisher we initialzed earlier.
        // This triggers a streamCreated event on other clients.
        session.publish(publisher);
      }
    });

</script>

客户2:

$opentok    =   new OpenTok($apiKey, $apiSecret);
    $myfile     =   fopen("session.txt", "r") or die("Unable to open file!");

    $sessionId  =   fgets($myfile); 
    fclose($myfile);

    $token      =   $opentok->generateToken($sessionId);


?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>OpenTok Test</title>
</head>
<body>

    <h2>Hello, World!</h2>
    <div id="publisher"></div>
    <div id="subscribers"></div>

</body>
</html>

<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>
<script>

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

    // Initialize an OpenTok Session object.
    var session = OT.initSession(sessionId);

    // Initialize a Publisher, and place it into the 'publisher' DOM element.
    var publisher = OT.initPublisher(apiKey, 'publisher');

    session.on('streamCreated', function(event) {
      // Called when another client publishes a stream.
      // Subscribe to the stream that caused this event.
      session.subscribe(event.stream, 'subscribers', { insertMode: 'append' });
    });

    // Connect to the session using your OpenTok API key and the client's token for the session
    session.connect(apiKey, token, function(error) {
      if (error) {
        console.error(error);
      } else {
        // Publish a stream, using the Publisher we initialzed earlier.
        // This triggers a streamCreated event on other clients.
        session.publish(publisher);
      }
    });

</script>

我使用chorme-dev-tools进行了检查,发现用户的会话ID相同。但是每个用户只显示发布者屏幕。

我做错了什么?我该如何纠正呢?

1 个答案:

答案 0 :(得分:0)

看起来你从OpenTok PHP SDK sample page得到了这个。不幸的是,这是使用旧的语法,所以我会尽快更新它,抱歉。

旧的语法应该仍然有用,但万一你应该将initSession函数更改为:

var session = OT.initSession(apiKey, sessionId);

您的initPublisher功能应该是:

OT.initPublisher('publisher');

你的session.connect函数应该是:

session.connect(token, function(error) {

您可以在OpenTok JS SDK Reference

中找到对最新语法的引用

此外,您应该在订阅和发布时真正倾听错误。

为此,您需要向session.subscribe函数添加回调:

session.subscribe(event.stream, 'subscribers', {
  insertMode: 'append'
}, function(error) {
  if (error) {
    console.error('Failed to subscribe', error);
  }
});

还有你的session.publish功能:

session.publish(publisher, function(error) {
  if (error) {
    console.error('Failed to publish', error);
  }
});

您还可以使用OT.setLogLevel功能显示额外的日志以帮助调试问题。将此放在的initSession调用之前:

OT.setLogLevel(OT.DEBUG);

那里有很多日志,但它可以让你更好地了解正在发生的事情。请记住,不应将日志级别设置为生产中的DEBUG。