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

Javascript 无法将两个客户端连接到Tokbox中的同一会话,javascript,php,opentok,tokbox,Javascript,Php,Opentok,Tokbox,我正在尝试使用tokbox添加视频流服务 但是用户之间无法相互连接。我遵循了他们提供的教程。这是我的密码。出于测试目的,我正在当前将会话id保存在一个文件中 客户一: $opentok = new OpenTok($apiKey, $apiSecret); $session = $opentok->createSession(); $sessionId = $session->getSessionId(); $token = $opentok-

我正在尝试使用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>
$opentok=新的opentok($apiKey,$apiSecret);
$session=$opentok->createSession();
$sessionId=$session->getSessionId();
$token=$opentok->generateToken($sessionId);
$file=fopen(“session.txt”、“w+”);
fwrite($file$sessionId);
fclose($文件);
?>
OpenTok
你好,世界!
var-apiKey='';
var sessionId='';
var标记=“”;
//初始化OpenTok会话对象。
var session=OT.initSession(sessionId);
//初始化发布服务器,并将其放入“Publisher”DOM元素中。
var publisher=OT.initPublisher(apiKey,“publisher”);
会话.on('streamCreated',函数(事件){
//当另一个客户端发布流时调用。
//订阅导致此事件的流。
订阅(event.stream,'subscribers',{insertMode:'append'});
});
//使用OpenTok API密钥和会话的客户端令牌连接到会话
连接(apiKey、令牌、函数)(错误){
如果(错误){
控制台错误(error);
}否则{
//使用前面初始化的发布服务器发布流。
//这会在其他客户端上触发streamCreated事件。
出版(出版商);
}
});
客户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>
$opentok=新的opentok($apiKey,$apiSecret);
$myfile=fopen(“session.txt”、“r”)或die(“无法打开文件!”);
$sessionId=fgets($myfile);
fclose($myfile);
$token=$opentok->generateToken($sessionId);
?>
OpenTok试验
你好,世界!
var-apiKey='';
var sessionId='';
var标记=“”;
//初始化OpenTok会话对象。
var session=OT.initSession(sessionId);
//初始化发布服务器,并将其放入“Publisher”DOM元素中。
var publisher=OT.initPublisher(apiKey,“publisher”);
会话.on('streamCreated',函数(事件){
//当另一个客户端发布流时调用。
//订阅导致此事件的流。
订阅(event.stream,'subscribers',{insertMode:'append'});
});
//使用OpenTok API密钥和会话的客户端令牌连接到会话
连接(apiKey、令牌、函数)(错误){
如果(错误){
控制台错误(error);
}否则{
//使用前面初始化的发布服务器发布流。
//这会在其他客户端上触发streamCreated事件。
出版(出版商);
}
});
我使用chorme开发工具进行了检查,发现两个用户的会话id都是相同的。但每个用户上只显示publisher屏幕


我做错了什么?我该怎么纠正呢?

看起来你是从。不幸的是,这是使用旧的语法,所以我会尽快更新它,对此感到抱歉

旧语法可能仍然有效,但只是为了防止您将initSession函数更改为:

var session = OT.initSession(apiKey, sessionId);
您的initPublisher函数应该是:

OT.initPublisher('publisher');
session.connect(token, function(error) {
您的session.connect功能应为:

OT.initPublisher('publisher');
session.connect(token, function(error) {
您可以在中找到对最新语法的引用

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

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

session.subscribe(event.stream, 'subscribers', {
  insertMode: 'append'
}, function(error) {
  if (error) {
    console.error('Failed to subscribe', error);
  }
});
session.publish(publisher, function(error) {
  if (error) {
    console.error('Failed to publish', error);
  }
});
以及您的session.publish功能:

session.subscribe(event.stream, 'subscribers', {
  insertMode: 'append'
}, function(error) {
  if (error) {
    console.error('Failed to subscribe', error);
  }
});
session.publish(publisher, function(error) {
  if (error) {
    console.error('Failed to publish', error);
  }
});
您还可以使用该函数显示额外的日志,以帮助调试问题。将此置于您的初始会话呼叫之前:

OT.setLogLevel(OT.DEBUG);

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

是否包含用于定位发布者和订阅者视图的CSS?开发者中心有一个教程供您遵循,它可能是一个很好的参考:另一件要检查的事情是:查看是否调用了streamCreated.Hie。谢谢你的帮助。我将应用这些更改,看看是否有效。