forked from opentok/opentok-android-sdk-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser_demo.html
198 lines (170 loc) · 6.79 KB
/
browser_demo.html
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>OpenTok Hello World</title>
<script src='https://static.opentok.com/v2/js/opentok.min.js'></script>
<script type="text/javascript" charset="utf-8">
// *** Fill the following variables using your own Project info from the Dashboard ***
// *** (https://dashboard.tokbox.com/projects) ***
var apiKey = ''; // Replace with your own API key.
var sessionId = ''; // Replace with your generated Session ID.
var token = ''; // Replace with your generated Token (use Project Tools or from a server-side library)
var session;
var publisher;
var subscribers = {};
// Un-comment either of the following to set automatic logging and exception handling.
// See the exceptionHandler() method below.
// TB.setLogLevel(TB.DEBUG);
// TB.addEventListener('exception', exceptionHandler);
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);
session.addEventListener('signal', signalHandler);
//--------------------------------------
// LINK CLICK HANDLERS
//--------------------------------------
function connect() {
session.connect(apiKey, token);
}
function disconnect() {
session.disconnect();
hide('disconnectLink');
hide('publishLink');
hide('unpublishLink');
}
// Called when user wants to start publishing to the session
function startPublishing() {
if (!publisher) {
var parentDiv = document.getElementById('myCamera');
var publisherDiv = document.createElement('div'); // Create a div for the publisher to replace
var publisherProperties = {};
publisherProperties.name = 'A web-based OpenTok client';
publisherDiv.setAttribute('id', 'opentok_publisher');
parentDiv.appendChild(publisherDiv);
publisher = OT.initPublisher(publisherDiv.id, publisherProperties);
session.publish(publisher, function(error) {
if (error) {
console.log('The publisher failed to connect.');
}
});
show('unpublishLink');
hide('publishLink');
}
}
function stopPublishing() {
if (publisher) {
session.unpublish(publisher);
}
publisher = null;
show('publishLink');
hide('unpublishLink');
}
function sendSignal() {
session.signal({
type: "chat",
data: "ping from browser",
},
function(error) {
if (error) {
console.log("signal error: " + error.reason);
} else {
console.log("signal sent");
}
}
);
}
//--------------------------------------
// OPENTOK EVENT HANDLERS
//--------------------------------------
function sessionConnectedHandler(event) {
// Subscribe to all streams currently in the Session
for (var i = 0; i < event.streams.length; i++) {
addStream(event.streams[i]);
}
show('disconnectLink');
show('publishLink');
show('signalLink');
hide('connectLink');
}
function streamCreatedHandler(event) {
// Subscribe to the newly created streams
for (var i = 0; i < event.streams.length; i++) {
TB.log('streamCreated - connectionId: ' + event.streams[i].connection.connectionId);
TB.log('streamCreated - connectionData: ' + event.streams[i].connection.data);
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;
show('connectLink');
hide('disconnectLink');
hide('publishLink');
hide('unpublishLink');
hide('signalLink');
}
function connectionDestroyedHandler(event) {
// This signals that connections were destroyed
}
function connectionCreatedHandler(event) {
// This signals new connections have been created.
}
function signalHandler(event) {
var signalsDiv = document.getElementById("signals");
var messageP = document.createElement("p");
messageP.innerHTML = event.data;
signalsDiv.appendChild(messageP);
}
/*
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) {
// 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';
}
</script>
</head>
<body>
<div id="links">
<input type="button" value="Connect" id ="connectLink" onClick="javascript:connect()" />
<input type="button" value="Disconnect" id ="disconnectLink" onClick="javascript:disconnect()" style="display:none" />
<input type="button" value="Publish" id ="publishLink" onClick="javascript:startPublishing()" style="display:none" />
<input type="button" value="Unpublish" id ="unpublishLink" onClick="javascript:stopPublishing()" style="display:none" />
<input type="button" value="Signal" id ="signalLink" onClick="javascript:sendSignal()" style="display:none" />
</div>
<div id="myCamera" class="publisherContainer"></div>
<div id="subscribers"></div>
<div id="signals"></div>
</body>
</html>