Skip to content

Commit

Permalink
Update JS samples to use latest API and add error handling (#144)
Browse files Browse the repository at this point in the history
* Update JS samples to use latest API and add error handling

* Update JS style: single quotes

* Update URL for JS SDK

* Return the initPublisher callback in helloworld sample
  • Loading branch information
aiham authored Jun 9, 2017
1 parent 764ffc5 commit be76801
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
25 changes: 18 additions & 7 deletions sample/Archiving/public/js/host.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
var session = OT.initSession(sessionId),
var session = OT.initSession(apiKey, sessionId),
publisher = OT.initPublisher('publisher'),
archiveID = null;

session.connect(apiKey, token, function(err, info) {
if(err) {
alert(err.message || err);
session.connect(token, function(error, info) {
if (error) {
console.error('Failed to connect', error);
} else {
session.publish(publisher, function(error) {
if (error) {
console.error('Failed to publish', error);
}
});
}
session.publish(publisher);
});

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

session.on('archiveStarted', function(event) {
Expand All @@ -30,7 +41,7 @@ session.on('archiveStopped', function(event) {
});

$(document).ready(function() {
$('.start').click(function (event) {
$('.start').click(function(event) {
var options = $('.archive-options').serialize();
disableForm();
$.post('/start', options).fail(enableForm);
Expand Down
25 changes: 18 additions & 7 deletions sample/Archiving/public/js/participant.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
var session = OT.initSession(sessionId),
publisher = OT.initPublisher("publisher");
var session = OT.initSession(apiKey, sessionId),
publisher = OT.initPublisher('publisher');

session.connect(apiKey, token, function(err, info) {
if(err) {
alert(err.message || err);
session.connect(token, function(error, info) {
if (error) {
console.error('Failed to connect', error);
} else {
session.publish(publisher, function(error) {
if (error) {
console.error('Failed to publish', error);
}
});
}
session.publish(publisher);
});

session.on('streamCreated', function(event) {
session.subscribe(event.stream, "subscribers", { insertMode : "append" });
session.subscribe(event.stream, 'subscribers', {
insertMode : 'append'
}, function(error) {
if (error) {
console.error('Failed to subscribe', error);
}
});
});
2 changes: 1 addition & 1 deletion sample/Archiving/views/participant.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script src="//static.opentok.com/webrtc/v2.2/js/opentok.min.js"></script>
<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>

<div class="container bump-me">

Expand Down
20 changes: 16 additions & 4 deletions sample/HelloWorld/public/js/helloworld.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ var publisher = OT.initPublisher('publisher', {
width: '100%',
height: '100%'
}, function(error) {
alert(error);
if (error) {
console.error('Failed to initialise publisher', error);
}
});

// Attach event handlers
Expand All @@ -18,7 +20,9 @@ session.on({
// Publish the publisher we initialzed earlier (this will trigger 'streamCreated' on other
// clients)
session.publish(publisher, function(error) {
alert(error);
if (error) {
console.error('Failed to publish', error);
}
});
},

Expand All @@ -31,10 +35,18 @@ session.on({
document.getElementById('subscribers').appendChild(subContainer);

// Subscribe to the stream that caused this event, put it inside the container we just made
session.subscribe(event.stream, subContainer);
session.subscribe(event.stream, subContainer, function(error) {
if (error) {
console.error('Failed to subscribe', error);
}
});
}

});

// Connect to the Session using a 'token'
session.connect(token);
session.connect(token, function(error) {
if (error) {
console.error('Failed to connect', error);
}
});

0 comments on commit be76801

Please sign in to comment.