I am trying to add a video streaming service using tokbox.
But the users are not able to connect to each other. I followed the tutorial provided by them. Here are my codes. For testing purpose I am saving the session id on a file currently.
Client One:
$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>
client 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>
I checked using chorme-dev-tools and found that the session id is same for both the user. But only the publisher screen is displayed on each user.
What am I doing Wrong? And how Should I correct it?