-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbreakout.js
101 lines (93 loc) · 2.75 KB
/
breakout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* global OT, room */
window.addEventListener('load', function studentController () {
var session
var publisher
var streamCount = 0
var subscribersDiv = $('#subscribers')
var timerContainer = $('#elapsed-time')
$('#controls').hide()
function startTimer () {
timerContainer.parent().prepend('Elapsed Time: ')
var starttime = new Date()
setInterval(function updateTimer () {
var t = Date.parse(new Date()) - Date.parse(starttime)
timerContainer.text([
Math.floor((t / (1000 * 60 * 60)) % 24),
Math.floor((t / 1000 / 60) % 60),
Math.floor((t / 1000) % 60)
].join(':'))
}, 1000)
}
function launchSession (data) {
session = OT.initSession(data.apiKey, data.breakoutSessionId)
session.on('streamCreated', function (evt) {
subscribersDiv.removeClass('streams-' + streamCount)
streamCount++
subscribersDiv.addClass('streams-' + streamCount)
var c = document.createElement('div')
c.className = 'stream'
c.id = 'stream-' + evt.stream.id
subscribersDiv.append(c)
session.subscribe(evt.stream, 'stream-' + evt.stream.id, {
insertMode: 'append',
width: '100%',
height: '100%'
})
})
session.on('streamDestroyed', function (evt) {
subscribersDiv.removeClass('streams-' + streamCount)
$('#stream-' + evt.stream.id).remove()
streamCount--
subscribersDiv.addClass('streams-' + streamCount)
})
session.connect(data.token, function (err) {
if (err) {
console.log(err)
return
}
publisher = OT.initPublisher('self-view', {
insertMode: 'append',
width: '100%',
height: '100%',
resolution: '640x480',
name: $('#user-name').val()
}, function (err) {
if (err) {
console.log('Error creating publisher', err)
return
}
$('#controls').show()
session.publish(publisher, function (err) {
if (err) {
console.log('Error publishing to session', err)
}
startTimer()
})
})
})
}
OT.getDevices(function (err, devices) {
if (err) {
console.log(err)
return
}
console.log('MediaDevices', devices)
if (devices.length < 1) {
console.error('No media devices available')
return
}
$('#join-room').removeAttr('disabled')
$('#join-room').on('click', function (evt) {
evt.preventDefault()
$.get('/token/' + room.roomId + '/breakout?name=' + $('#user-name').val(), function (data) {
console.log('Token data', data)
$('#join-form').hide()
launchSession(data)
}, 'json')
.fail(function (err) {
console.log(err)
})
return false
})
})
})