forked from opentok/opentok-ios-sdk-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-demo.html
139 lines (123 loc) · 4.69 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>OpenTok Test</title>
</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="publisherContainer"></div>
<div id="subscribersContainer"></div>
<div id="signals"></div>
<script src='http://static.opentok.com/webrtc/v2.2/js/TB.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 an OpenTok server-side library)
var apiKey = '472032'; // Replace with your own API key.
var sessionId = '2_MX40NzIwMzJ-fjE0MTI5MDQwMzI1MzF-aVRZekk2Y0gzMFFLVERjRmQ5cjk0M2pDfn4'; // Replace with your generated session ID.
var token = 'T1==cGFydG5lcl9pZD00NzIwMzImc2lnPThiMDUzYWJmMzUyOGNjMjVmMGUxMmFlNWFlMmJhOTdhYjBmZjVkMzA6cm9sZT1tb2RlcmF0b3Imc2Vzc2lvbl9pZD0yX01YNDBOekl3TXpKLWZqRTBNVEk1TURRd016STFNekYtYVZSWmVrazJZMGd6TUZGTFZFUmpSbVE1Y2prME0ycERmbjQmY3JlYXRlX3RpbWU9MTQxMjkwNDA1MiZub25jZT0wLjEyMDY3MjAyMTY4NjU2ODU0JmV4cGlyZV90aW1lPTE0MTU0OTYwMDg='; // Replace with your generated token (use Project Tools or an OpenTok server-side library)
var session;
// Un-comment the following to set automatic logging:
// OT.setLogLevel(OT.DEBUG);
session = OT.initSession(apiKey, sessionId);
// Add event listeners to the session
session.on({
sessionDisconnected: function(event) {
// The user was disconnected from the Session. Any subscribers
// and publishers will automatically be removed from the dom.
show('connectLink');
hide('disconnectLink');
hide('publishLink');
hide('unpublishLink');
hide('signalLink');
},
streamCreated: function(event) {
// Subscribe to a new third-party stream in the session.
console.log('streamCreated');
var subOptions = {insertMode: 'append'};
session.subscribe(event.stream, 'subscribersContainer', subOptions);
},
signal: function(event) {
// A signal was received.
var signalsDiv = document.getElementById('signals');
var messageP = document.createElement('p');
messageP.innerHTML = event.data;
signalsDiv.appendChild(messageP);
}
});
//--------------------------------------
// LINK CLICK HANDLERS
//--------------------------------------
function connect() {
session.connect(token, function(error) {
if (error) {
alert(error.message);
} else {
show('disconnectLink');
show('publishLink');
show('signalLink');
hide('connectLink');
}
});
}
function disconnect() {
session.disconnect();
hide('disconnectLink');
hide('publishLink');
hide('unpublishLink');
}
// Called when user wants to start publishing to the session
function startPublishing() {
var publisherOptions = {
name: 'A web-based OpenTok client',
insertMode: 'append'
};
publisher = OT.initPublisher('publisherContainer', publisherOptions);
session.publish(publisher, function(error) {
if (error) {
alert(error.message);
}
});
show('unpublishLink');
hide('publishLink');
}
function stopPublishing() {
session.unpublish(publisher);
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");
}
}
);
}
//--------------------------------------
// HELPER METHODS
//--------------------------------------
function show(id) {
document.getElementById(id).style.display = 'block';
}
function hide(id) {
document.getElementById(id).style.display = 'none';
}
</script>
</body>
</html>