-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatexample.html
More file actions
432 lines (405 loc) · 16.4 KB
/
Copy pathchatexample.html
File metadata and controls
432 lines (405 loc) · 16.4 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<!DOCTYPE html>
<html>
<head>
<title>Spaces Webchat</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 Webchat</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>
<div class="row">
<div class="col-12">
<br>
<input type="file" id="fileName" multiple>
<button id="Upload" class="btn btn-primary" onclick="uploadFile()">Upload</button>
<br>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="form-group">
<label for="exampleFormControlTextarea1">Message:</label>
<textarea class="form-control" id="message" rows="3"></textarea>
</div>
<button id="sendMsg" class="btn btn-success" onclick="sendMsg()">Send</button>
</div>
</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() {
$('#message').prop("readonly", true);
$('#sendMsg').prop("disabled", true);
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 () {
$('#chatMsg').prop("disabled", false);
$('#sendMsg').prop("disabled", false);
$('#message').prop("readonly", false);
populateChats();
populateTasks();
populateIdeas();
});
socketio.on('CHANNEL_UNSUBSCRIBED', function () {
console.log("CHANNEL_UNSUBSCRIBED");
});
socketio.on('SUBSCRIBE_CHANNEL_FAILED', function () {
console.log("SUBSCRIBE_CHANNEL_FAILED");
});
socketio.on('SEND_MESSAGE_FAILED', function (msg) {
console.log("SEND_MESSAGE_FAILED");
});
socketio.on('MESSAGE_SENT', function (msg) {
var category = msg.category;
var message;
var description;
var strLength;
if (category == "chat") {
message = msg.content.bodyText;
if (msg.content.data !== undefined) {
if (msg.content.data.length > 0) {
message = msg.sender.displayname + ": " + msg.content.data[0].name + " " + msg.content.data[0].path;
}
}
// Chat messages from Spaces come in the form <p>message goes here<p>
if (message.includes("<p>")) {
strLength = msg.content.bodyText.length;
// Decode ' and " characters
message = msg.sender.displayname + ": " + message.substring(3, strLength - 4).replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
} else {
message = msg.sender.displayname + ": " + message.replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
}
trace(message);
} else if (category == "task") {
message = msg.content.bodyText;
description = msg.content.description;
if (description.includes("<p>")) {
strLength = msg.content.description.length;
// Decode ' and " characters
message = "Task: " + message + " / " + description.substring(3, strLength - 4).replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
} else {
message = "Task: " + message + " / " + description.replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
}
trace(message);
} else if (category == "idea") {
message = msg.content.bodyText;
if (msg.content.data.length !== 0) {
path = msg.content.data[0].path;
message = "Post: " + message + " / " + path;
trace(message);
}
}
});
}
function googleUpload(storageURL, fileKey) {
var mySelectedFile = document.getElementById('fileName').files[0];
$.ajax({
url: storageURL,
type: "put",
processData: false,
contentType: mySelectedFile.type,
headers: {
'Accept': 'application/json',
'Content-type': mySelectedFile.type
},
data: mySelectedFile,
success: function (result) {
var post = {
chatMessages: {},
category: 'idea',
content: {
assignees: [],
bodyText: "sample file",
description: "Description",
status: "pending",
data: [{
"fileId": fileKey,
"fileSize": mySelectedFile.size,
"fileType": "document",
"icon": "",
"name": mySelectedFile.name,
"provider": "native",
"providerFileType": mySelectedFile.type
}]
},
topicId: spacesID,
version: "1.1"
};
socketio.emit('SEND_MESSAGE', post)
},
error: function (log) {
// handle error
console.log("Google Upload Error");
}
});
}
function uploadFile() {
var selectedFile = document.getElementById('fileName').files[0];
console.log("1");
console.dir(selectedFile);
var uploadData = {
files: [{
"Content-Type": selectedFile.type,
"Content-Length": selectedFile.size
}],
topicId: spacesID
};
$.ajax({
headers: {
'Authorization': 'jwt ' + spacesToken,
'Accept': 'application/json',
'Content-type': 'application/json'
},
url: 'https://spacesapis.avayacloud.com/api/files/getuploadurl',
type: "post",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify(uploadData),
success: function (data) {
var googleURL = data.data[0].url;
var fileKey = data.data[0].fileKey;
googleUpload(googleURL, fileKey);
},
error: function (error) {
console.log(`Error ${error}`);
}
});
}
function populateChats() {
$.ajax({
headers: {
'Authorization': 'jwt ' + spacesToken,
'Accept': 'application/json',
'spaces-x-space-password': password
},
url: 'https://spacesapis.avayacloud.com/api/spaces/' + spacesID + '/messages/query?category=chat&size=50',
type: "GET",
dataType: "json",
success: function (data) {
console.dir(data);
var entries = data.total;
var msg;
var message;
var strLength;
for (i = entries - 1; i >= 0; i--) {
msg = data.data[i];
message = msg.content.bodyText;
if (msg.content.data.length > 0) {
message = msg.sender.displayname + ": " + msg.content.data[0].name + " " + msg.content.data[0].path;
} else if (message.includes("<p>")) {
// Chat messages from Spaces come in the form <p>message goes here<p>
strLength = msg.content.bodyText.length;
// Decode ' and " characters
message = msg.sender.displayname + ": " + msg.content.bodyText.substring(3, strLength - 4).replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
} else {
message = msg.sender.displayname + ": " + message.replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
}
console.log(message);
trace("msg: " + message);
}
},
error: function (error) {
console.log(`Error ${error}`);
console.log("error");
}
});
}
function populateTasks() {
$.ajax({
headers: {
'Authorization': 'jwt ' + spacesToken,
'Accept': 'application/json',
'spaces-x-space-password': password
},
url: 'https://spacesapis.avayacloud.com/api/spaces/' + spacesID + '/messages/query?category=task&size=50',
type: "GET",
dataType: "json",
success: function (data) {
var entries = data.total;
for (i = entries - 1; i >= 0; i--) {
msg = data.data[i];
var message = msg.content.bodyText;
var description = msg.content.description;
if (description.includes("<p>")) {
strLength = msg.content.description.length;
// Decode ' and " characters
message = "Task: " + message + " / " + description.substring(3, strLength - 4).replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
} else {
message = "Task: " + message + " / " + description.replace(new RegExp("&" + "#" + "x27;", "g"), "'").replace(/"/g, '"');
}
trace(message);
}
},
error: function (error) {
console.log(`Error ${error}`);
}
});
}
function populateIdeas() {
$.ajax({
headers: {
'Authorization': 'jwt ' + spacesToken,
'Accept': 'application/json',
'spaces-x-space-password': password
},
url: 'https://spacesapis.avayacloud.com/api/spaces/' + spacesID + '/messages/query?category=idea&size=50',
type: "GET",
dataType: "json",
success: function (data) {
var entries = data.total;
for (i = entries - 1; i >= 0; i--) {
msg = data.data[i];
var message = msg.content.bodyText;
var path = msg.content.data[0].path;
message = "Post: " + message + " / " + path;
trace(message);
}
},
error: function (error) {
console.log(`Error ${error}`);
}
});
}
function sendMsg() {
var message = {
content: {
bodyText: $('#message').val()
},
sender: {
type: 'user'
},
category: 'chat',
topicId: spacesID
};
socketio.emit('SEND_MESSAGE', message);
document.getElementById('message').value = "";
}
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;
//spacesToken = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkX3NpZyI6ImRHeF9sVjk3UzlhUkVKQ3ZJYm1QTzVVZGN5Zk1JdWFhN0JiQlFXZDVBdlEiLCJwcm9kdWN0X3R5cGUiOiJhY2NvdW50cyIsImxhc3R1cGRhdGV0aW1lIjoiMjAyMS0xMS0xMVQxNjoyNToyNC4wOTkiLCJpc3MiOiJhdmF5YWNsb3VkLmNvbSIsInB1YmxpY2tleWlkIjoiYWd4emZtOXVaWE51WVRJd01UUnlHZ3NTRFVkS2QzUlFkV0pzYVdOTFpYa1lnSUR3dS1QQjBnZ00iLCJleHAiOjE2NDg3NDcxNzksInVzZXJfaWQiOiJhZ3h6Zm05dVpYTnVZVEl3TVRSeUVRc1NCRlZ6WlhJWWdJRFFoYlMwblFvTSIsInZlciI6IjIuMCJ9.otMelsJPImdamR80AOCQhLVWIi-cj_W263f1pDCft05TLOXrSJOsn6_azTJkwSMrsx0bVWmfrqc5STxrz1_ClsPaAVLsZnIYaOptfLr4D7S-6oq6gqmn82H4isq42JClfBRLE4of53niH8wacOTl6ZzuhiQHlKBB6B15dAhdpOL-XBUCMYjGUL_NdSjIi4tt4yxv8vOXMJhE8NJOlN4UWANuWRNqk4JdifHEfjnafIIml8sBTDkRPU3Hx9QrJHx9Zp8bOqAkwYrAUQCfl9q6nXNOXB_4WPVlXD3y3k3A6GBHKkJY2woGYPvYU1uYGC4SHhXCn5KRl7CWYqDEGRpmew"; //Some functionality (e.g. uploading files) requires a JWT from a registered Spaces user. To exercise that functionality, obtain a Spaces JWT from Chrome and overide the JWT for the anonymous user.
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>