-
Notifications
You must be signed in to change notification settings - Fork 17
/
browser-demo-textchat.html
98 lines (85 loc) · 2.92 KB
/
browser-demo-textchat.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
<!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="Signal" id ="signalLink" onClick="javascript:sendSignal()" style="display:none" />
</div>
<div id="signals"></div>
<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 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('signalLink');
},
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('signalLink');
hide('connectLink');
}
});
}
function disconnect() {
session.disconnect();
hide('disconnectLink');
}
function sendSignal() {
session.signal({
type: "text-chat",
data: JSON.stringify({"sentOn":1462396461923.305,"text":"Hi","sender":{"alias":"Tokboxer","id":"16FEB40D-C09B-4491-A983-44677B7EBB3E"}})
},
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>