Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic video-feed with data-packet testing
- Loading branch information
1 parent
00727d9
commit 8f4e67e
Showing
5 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* @author Ezra Velazquez | ||
*/ | ||
|
||
console.log("hello world"); | ||
|
||
var OpenTok = function() { | ||
var apiKey = 13249262; // OpenTok sample API key. Replace with your own API key. | ||
var sessionId = '153975e9d3ecce1d11baddd2c9d8d3c9d147df18'; // Replace with your session ID. | ||
var token = 'devtoken'; // Should not be hard-coded. | ||
// Add to the page using the OpenTok server-side libraries. | ||
var session; | ||
var publisher; | ||
var subscribers = {}; | ||
|
||
//-------------------------------------- | ||
// LINK CLICK HANDLERS | ||
//-------------------------------------- | ||
function connect() { | ||
console.log(token); | ||
session.connect(apiKey, token); | ||
} | ||
|
||
function disconnect() { | ||
session.disconnect(); | ||
} | ||
|
||
// Called when user wants to start publishing to the session | ||
function startPublishing() { | ||
if (!publisher) { | ||
var parentDiv = document.getElementById("video_feed_instructor"); | ||
var publisherDiv = document.createElement('div'); // Create a div for the publisher to replace | ||
publisherDiv.setAttribute('id', 'opentok_publisher'); | ||
parentDiv.appendChild(publisherDiv); | ||
publisher = session.publish(publisherDiv.id); // Pass the replacement div id to the publish method | ||
} | ||
} | ||
|
||
function stopPublishing() { | ||
if (publisher) { | ||
session.unpublish(publisher); | ||
} | ||
publisher = null; | ||
} | ||
|
||
//-------------------------------------- | ||
// OPENTOK EVENT HANDLERS | ||
//-------------------------------------- | ||
function sessionConnectedHandler(event) { | ||
startPublishing(); | ||
|
||
// Subscribe to all streams currently in the Session | ||
for (var i = 0; i < event.streams.length; i++) { | ||
addStream(event.streams[i]); | ||
} | ||
} | ||
|
||
function streamCreatedHandler(event) { | ||
// Subscribe to the newly created streams | ||
for (var i = 0; i < event.streams.length; i++) { | ||
addStream(event.streams[i]); | ||
} | ||
} | ||
|
||
function streamDestroyedHandler(event) { | ||
// This signals that a stream was destroyed. Any Subscribers will automatically be removed. | ||
// This default behaviour can be prevented using event.preventDefault() | ||
} | ||
|
||
function sessionDisconnectedHandler(event) { | ||
// This signals that the user was disconnected from the Session. Any subscribers and publishers | ||
// will automatically be removed. This default behaviour can be prevented using event.preventDefault() | ||
publisher = null; | ||
} | ||
|
||
function connectionDestroyedHandler(event) { | ||
// This signals that connections were destroyed | ||
} | ||
|
||
function connectionCreatedHandler(event) { | ||
// This signals new connections have been created. | ||
} | ||
|
||
/* | ||
If you un-comment the call to TB.addEventListener("exception", exceptionHandler) above, OpenTok calls the | ||
exceptionHandler() method when exception events occur. You can modify this method to further process exception events. | ||
If you un-comment the call to TB.setLogLevel(), above, OpenTok automatically displays exception event messages. | ||
*/ | ||
function exceptionHandler(event) { | ||
alert("Exception: " + event.code + "::" + event.message); | ||
} | ||
|
||
//-------------------------------------- | ||
// HELPER METHODS | ||
//-------------------------------------- | ||
|
||
function addStream(stream) { | ||
|
||
// For testing purposes, it all goes to instructor currently! | ||
//console.log(stream); | ||
connection_data = getConnectionData(stream['connection']); | ||
console.log(connection_data); | ||
$(".instructor_name.classroom_labels").html(connection_data['name']); | ||
|
||
// Check if this is the stream that I am publishing, and if so do not publish. | ||
if (stream.connection.connectionId == session.connection.connectionId) { | ||
return; | ||
} | ||
var subscriberDiv = document.createElement('div'); // Create a div for the subscriber to replace | ||
subscriberDiv.setAttribute('id', stream.streamId); // Give the replacement div the id of the stream as its id. | ||
document.getElementById("subscribers").appendChild(subscriberDiv); | ||
subscribers[stream.streamId] = session.subscribe(stream, subscriberDiv.id); | ||
} | ||
|
||
function show(id) { | ||
document.getElementById(id).style.display = 'block'; | ||
} | ||
|
||
function hide(id) { | ||
document.getElementById(id).style.display = 'none'; | ||
} | ||
|
||
function getConnectionData(connection) { | ||
try { | ||
connectionData = JSON.parse(connection.data); | ||
} catch(error) { | ||
connectionData = eval("(" + connection.data + ")" ); | ||
} | ||
return connectionData; | ||
} | ||
|
||
return { | ||
init : function() { | ||
$.get('../php/server_ajax.php', { | ||
"o" : "classroom" | ||
}, function(data) { | ||
console.log(data); | ||
sessionId = data.session[0]; | ||
token = data.token; | ||
|
||
if (TB.checkSystemRequirements() != TB.HAS_REQUIREMENTS) { | ||
alert("You don't have the minimum requirements to run this application." | ||
+ "Please upgrade to the latest version of Flash."); | ||
} else { | ||
session = TB.initSession(sessionId); // Initialize session | ||
|
||
// Add event listeners to the session | ||
session.addEventListener('sessionConnected', sessionConnectedHandler); | ||
session.addEventListener('sessionDisconnected', sessionDisconnectedHandler); | ||
session.addEventListener('connectionCreated', connectionCreatedHandler); | ||
session.addEventListener('connectionDestroyed', connectionDestroyedHandler); | ||
session.addEventListener('streamCreated', streamCreatedHandler); | ||
session.addEventListener('streamDestroyed', streamDestroyedHandler); | ||
|
||
connect(); | ||
} | ||
}); | ||
} | ||
} | ||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
require_once 'lib/API_Config.php'; | ||
require_once 'lib/OpenTokSDK.php'; | ||
|
||
class OpenTok { | ||
|
||
function OpenTok() { | ||
|
||
} | ||
|
||
function generate($name) { | ||
$apiObj = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET); | ||
|
||
// Pull this from the server when wanting the workshop | ||
$session = $apiObj->create_session($_SERVER["REMOTE_ADDR"]); | ||
|
||
$role = RoleConstants::PUBLISHER; // Or set to the correct role for the user. | ||
// Get username from the FB server? | ||
$metadata = '{"name": "'.$name.'"}'; | ||
$token_user = $apiObj->generate_token($session->getSessionId(), $role, NULL, $metadata); // Replace with the correct session ID | ||
$session_id = $session->getSessionId(); | ||
|
||
$arr = array('session'=>$session_id, 'token'=>$token_user); | ||
return $arr; | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters