Skip to content

Instantly share code, notes, and snippets.

@jchang419
Created May 15, 2018 20:00
Show Gist options
  • Save jchang419/32d25577b581443388a7add62db6f568 to your computer and use it in GitHub Desktop.
Save jchang419/32d25577b581443388a7add62db6f568 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div style="height:500px;width:500px" id="subscriber" />
<div id="subscriberCopy" />
<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>
<script>
// Replace these values with those generated in your TokBox Account
const apiKey = "";
const sessionId = "";
const token = "";
initializeSession();
// Handling all of our errors here by alerting them
function handleError(error) {
if (error) {
alert(error.message);
}
}
function initializeSession() {
let session = OT.initSession(apiKey, sessionId);
let subscriberOptions = {
insertMode: 'append',
width: '100%',
height: '100%'
};
// Subscribe to a newly created stream
session.on('streamCreated', event => {
let createsub = new Promise(function(resolve, reject) {
let subscriber = session.subscribe(event.stream, 'subscriber', subscriberOptions, function initComplete(err) {
if (err) {
reject(err);
} else {
resolve(subscriber);
}
});
});
createsub.then(function(result) {
let subDiv = document.getElementById("subscriber");
let subVid = subDiv.getElementsByTagName("video")[0];
let stream = subVid.captureStream();
let videoEl = document.createElement('video');
videoEl.srcObject = stream;
videoEl.setAttribute('playsinline', '');
videoEl.muted = true;
setTimeout(function timeout() {
videoEl.play();
});
let subCopyDiv = document.getElementById("subscriberCopy");
subCopyDiv.appendChild(videoEl);
}, function(err) {
console.log(err);
});
});
// Connect to the session
session.connect(token, error => {
// If the connection is successful, initialize a publisher and publish to the session
if (error) {
handleError(error);
}
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment