-
Notifications
You must be signed in to change notification settings - Fork 0
/
tb.js
145 lines (119 loc) · 4.17 KB
/
tb.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var apiKey = '11827222';
var currentSessionID = '';
var currentToken = '';
var session = null;
var replaceElementId = 'tokbox';
TB.setLogLevel(TB.DEBUG);
var isDisconnecting = false;
var isWaitingToConnect = false;
var sessionEventListener = null;
function setSessionEventListener(eventListener) {
sessionEventListener = eventListener;
}
function connectWithSessionAndToken(sessionID, token) {
currentSessionID = sessionID
currentToken = token;
if (!isDisconnecting) {
connectWithCurrentSessionID();
} else {
debug("waiting to connect with sessionID: " + currentSessionID);
isWaitingToConnect = true;
}
}
function connectWithCurrentSessionID() {
debug("connecting with sessionID: " + currentSessionID);
isWaitingToConnect = false;
session = TB.initSession(currentSessionID);
session.addEventListener('sessionConnected', sessionConnectedHandler);
session.addEventListener('sessionDisconnected', sessionDisconnectedHandler);
session.addEventListener('streamCreated', streamCreatedHandler);
session.addEventListener('connectionCreated', connectionCreatedHandler);
session.addEventListener('connectionDestroyed', connectionDestroyedHandler);
session.connect(apiKey, currentToken);
}
function disconnectCurrentSession() {
if (session != null) {
debug("disconnecting..");
isDisconnecting = true;
session.disconnect();
}
}
function sessionDisconnectedHandler (event) {
isDisconnecting = false;
removeEverythingInContentDivAfterDisconnect();
var _session = event.target;
console.log("ended session");
console.log(_session);
sessionEventListener.didEndSession(_session);
if (isWaitingToConnect) {
debug("was waiting to connect. Connecting after a disconnect with sessionID: " + currentSessionID);
connectWithSessionAndToken(currentSessionID, currentToken);
}
}
function removeEverythingInContentDivAfterDisconnect() {
var c = document.getElementById('content');
if (c.hasChildNodes()) {
while (c.childNodes.length >= 1) {
c.removeChild(c.firstChild);
}
}
}
var publisher;
function sessionConnectedHandler(event) {
console.log(event);
insertReplaceElementInContent();
publisher = session.publish(replaceElementId);
publisher.publishAudio(false);
// Subscribe to streams that were in the session when we connected
subscribeToStreams(event.streams);
var _session = event.target;
console.log("Started session");
console.log(_session);
if (sessionEventListener === null)
console.log("Warning: sessionEventListener is null. Use setSessionEventListener and listen!");
else
sessionEventListener.didStartSession(_session);
}
function insertReplaceElementInContent() {
var boxDiv = document.createElement('div');
boxDiv.setAttribute('id', 'you');
boxDiv.setAttribute('class', 'box');
var tokboxDiv = document.createElement('div');
tokboxDiv.setAttribute('id',replaceElementId);
var infoDiv = document.createElement('div');
infoDiv.setAttribute('class', 'info');
boxDiv.appendChild(tokboxDiv);
boxDiv.appendChild(infoDiv);
document.getElementById('content').appendChild(boxDiv);
}
function streamCreatedHandler(event) {
// Subscribe to any new streams that are created
subscribeToStreams(event.streams);
}
function subscribeToStreams(streams) {
for (var i = 0; i < streams.length; i++) {
// Make sure we don't subscribe to ourself
if (streams[i].connection.connectionId === session.connection.connectionId) {
return;
}
var id = streams[i].streamId;
// Create the div to put the subscriber element in to
var box = document.createElement('div');
box.setAttribute('class', 'box');
box.setAttribute('id', streams[i].connection.connectionId);
document.getElementById('content').appendChild(box);
var cam = document.createElement('div');
cam.setAttribute('id', 'stream' + id);
document.getElementById(streams[i].connection.connectionId).appendChild(cam);
// Subscribe to the stream
session.subscribe(streams[i], cam.id);
}
}
function connectionCreatedHandler(event) {
}
function connectionDestroyedHandler(event) {
console.log(event)
var c = document.getElementById('content');
var divId = document.getElementById(event.connections[0].connectionId);
c.removeChild(divId);
}