-
Notifications
You must be signed in to change notification settings - Fork 23
/
browser_demo.html
168 lines (145 loc) · 6.28 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
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>OpenTok Hello World</title>
<script src="http://staging.tokbox.com/v0.91/js/TB.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var apiKey = 1127; // OpenTok sample API key. Replace with your own API key.
var sessionId = '1sdemo00855f8290f8efa648d9347d718f7e06fd';
// Replace with your session ID. See http://staging.tokbox.com:4000/hl/session/create
var token = 'devtoken'; // Should not be hard-coded in production versions of apps.
// Add to the page using the OpenTok server-side libraries.
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.ALL);
// 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);
//--------------------------------------
// LINK CLICK HANDLERS
//--------------------------------------
/*
If testing the app from the desktop, be sure to check the Flash Player Global Security setting
to allow the page from communicating with SWF content loaded from the web. For more information,
see http://www.tokbox.com/opentok/build/tutorials/helloworld.html#localTest
*/
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";
publisherProperties.data = "Joe mama"
publisherDiv.setAttribute('id', 'opentok_publisher');
parentDiv.appendChild(publisherDiv);
publisher = session.publish(publisherDiv.id, publisherProperties); // Pass the replacement div id to the publish method
show('unpublishLink');
hide('publishLink');
}
}
function stopPublishing() {
if (publisher) {
session.unpublish(publisher);
}
publisher = null;
show('publishLink');
hide('unpublishLink');
}
//--------------------------------------
// 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');
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');
}
function connectionDestroyedHandler(event) {
// This signals that connections were destroyed
}
function connectionCreatedHandler(event) {
// This signals new connections have been created.
}
/*
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" />
</div>
<div id="myCamera" class="publisherContainer"></div>
<div id="subscribers"></div>
</body>
</html>