-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence.html
186 lines (168 loc) · 6.53 KB
/
presence.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
<!DOCTYPE html>
<html>
<head>
<title>Spaces Presence Tracker</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.3/socket.io.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1>Spaces Presence Tracker</h1>
<div class="row">
<div class="col-4">
<label for="room">Room Number:</label>
<textarea class="form-control" id="room" rows="1"></textarea>
</div>
<div class="col-4">
<label for="password">Password:</label>
<input class="form-control" type="password" id="password" rows="1"></input>
</div>
<div class="col-4">
<br>
<button id="connect" class="btn btn-primary" onclick="startConnect()">Connect</button>
</div>
<br>
</div>
<hr/>
<div class="row">
<div class="col-12">
<textarea readonly="readonly" class="form-control" id="console-log" rows="30"></textarea>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(init);
function init() {
document.getElementById("connect").innerHTML = "Connect";
}
var spacesToken;
var socketURL = "https://spacesapis-socket.avayacloud.com/chat";
var spacesID;
var socketio = "";
var password;
function connectSocket() {
socketio = io.connect(
socketURL, {
query: 'tokenType=jwt&token=' + spacesToken,
transports: ['websocket']
}
);
socketio.on('connect', function () {
var spaceToSubscribe = {
channel: {
_id: spacesID,
type: 'topic',
password: password
}
};
socketio.emit('SUBSCRIBE_CHANNEL', spaceToSubscribe);
document.getElementById("connect").innerHTML = "Disconnect";
});
socketio.on('disconnect', function () {
console.log("Socket disconnect");
});
socketio.on('connect_error', function () {
console.log("Socket connect_error");
});
socketio.on('error', function () {
console.log("Socket error");
});
socketio.on('CHANNEL_SUBSCRIBED', function () {
});
socketio.on('CHANNEL_UNSUBSCRIBED', function () {
console.log("CHANNEL_UNSUBSCRIBED");
});
socketio.on('SUBSCRIBE_CHANNEL_FAILED', function () {
console.log("SUBSCRIBE_CHANNEL_FAILED");
});
socketio.on('PRESENCE_EVENT_RESPONSE', function (msg) {
if (msg.category == "app.event.presence.party.online") {
trace("Party online: " + msg.sender.displayname + " / " + msg.sender.username);
} else if (msg.category == "app.event.presence.party.leaves") {
trace("Party leaves: " + msg.sender.displayname + " / " + msg.sender.username);
}
});
socketio.on('MEDIA_SESSION_RESPONSE', function (msg) {
if (msg.category == "app.event.attendee.added") {
trace("Media attendee added: " + msg.content.attendee.displayname + " / " + msg.content.attendee.username);
} else if (msg.category == "app.event.attendee.left") {
trace("Media attendee leaves: " + msg.content.attendee.displayname + " / " + msg.content.attendee.username);
} else if (msg.category == "tracksstatus") {
trace("Track Status: " + msg.sender.username + " " + JSON.stringify(msg.content.mediaSession));
}
});
}
function clearTrace() {
var consoleTxt = $('#console-log').val();
$('#console-log').val("");
}
function disconnect() {
var spaceToUnsubscribe = {
channel: {
_id: spacesID,
type: 'topic',
password: password
}
};
socketio.emit('UNSUBSCRIBE_CHANNEL', spaceToUnsubscribe);
init();
clearTrace();
socketio = null;
}
function startConnect() {
if (document.getElementById("connect").innerHTML == "Disconnect") {
disconnect();
return;
}
input = document.getElementById("password").value;
if (input.trim() != '') {
password = document.getElementById("password").value;
} else {
password = null;
}
spacesID = document.getElementById('room').value;
// Create an anonymous user in order to obtain a jwt token
$.ajax({
data: JSON.stringify({
"displayname": "Chatty McChat",
"username": "Anonymous"
}),
url: "https://spacesapis.avayacloud.com/api/anonymous/auth",
contentType: 'application/json',
type: 'POST',
success: function (data) {
spacesToken = data.token;
joinRoom();
connectSocket();
},
error: function (error) {}
});
}
function joinRoom() {
// Join the space
$.ajax({
headers: {
'Authorization': 'jwt ' + spacesToken,
'Accept': 'application/json',
'spaces-x-space-password': password
},
url: 'https://spacesapis.avayacloud.com/api/spaces/' + spacesID + '/join',
type: "GET",
success: function (data) {
console.log("Room joined");
console.dir(data);
}
});
}
function trace(text) {
text = text.trim();
const now = (window.performance.now() / 1000).toFixed(3);
var consoleTxt = $('#console-log').val();
var log = text;
$('#console-log').val(consoleTxt + "\n\n" + log);
}
</script>
</body>
</html>