|
1 | 1 | // Initialize an OpenTok Session object
|
2 |
| -var session = TB.initSession(sessionId); |
| 2 | +var session = OT.initSession(sessionId); |
3 | 3 |
|
4 | 4 | // Initialize a Publisher, and place it into the element with id="publisher"
|
5 |
| -var publisher = TB.initPublisher(apiKey, 'publisher'); |
| 5 | +var publisher = OT.initPublisher(apiKey, 'publisher'); |
6 | 6 |
|
7 |
| -// Attach event handlers |
8 |
| -session.on({ |
| 7 | +// Attach an event handler for when the session dispatches the 'streamCreated' event. |
| 8 | +session.on('streamCreated', function(event) { |
| 9 | + // This function runs when another client publishes a stream (eg. session.publish()) |
9 | 10 |
|
10 |
| - // This function runs when session.connect() asynchronously completes |
11 |
| - sessionConnected: function(event) { |
12 |
| - // Publish the publisher we initialzed earlier (this will trigger 'streamCreated' on other |
13 |
| - // clients) |
14 |
| - session.publish(publisher); |
15 |
| - }, |
| 11 | + // Subscribe to the stream that caused this event, put it inside the DOM element with id='subscribers' |
| 12 | + session.subscribe(event.stream, 'subscribers', { |
| 13 | + insertMode: 'append' |
| 14 | + }); |
| 15 | +}); |
16 | 16 |
|
17 |
| - // This function runs when another client publishes a stream (eg. session.publish()) |
18 |
| - streamCreated: function(event) { |
19 |
| - // Create a container for a new Subscriber, assign it an id using the streamId, put it inside |
20 |
| - // the element with id="subscribers" |
21 |
| - var subContainer = document.createElement('div'); |
22 |
| - subContainer.id = 'stream-' + event.stream.streamId; |
23 |
| - document.getElementById('subscribers').appendChild(subContainer); |
| 17 | +// Connect to the Session using the 'apiKey' of the application and a 'token' for permission |
| 18 | +session.connect(apiKey, token, function(error) { |
| 19 | + // This function runs when session.connect() asynchronously completes |
24 | 20 |
|
25 |
| - // Subscribe to the stream that caused this event, put it inside the container we just made |
26 |
| - session.subscribe(event.stream, subContainer); |
27 |
| - } |
| 21 | + // Handle connection errors |
| 22 | + if (error) { |
| 23 | + return console.error(error); |
| 24 | + } |
28 | 25 |
|
| 26 | + // Publish the publisher we initialzed earlier (this will trigger 'streamCreated' on other |
| 27 | + // clients) |
| 28 | + session.publish(publisher); |
29 | 29 | });
|
30 |
| - |
31 |
| -// Connect to the Session using the 'apiKey' of the application and a 'token' for permission |
32 |
| -session.connect(apiKey, token); |
0 commit comments