-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct.html
237 lines (217 loc) · 8.18 KB
/
direct.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
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
<!DOCTYPE html>
<html>
<head>
<title>Spaces Direct Message</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 Direct Message</h1>
<div class="row">
<div class="col-4">
<label for="jwt">JWT:</label>
<textarea class="form-control" id="jwt" rows="1"></textarea>
</div>
<div class="col-3">
<label for="email">Email Address:</label>
<input class="form-control" type="text" id="email" rows="1"></input>
</div>
<div class="col-2">
<br>
<button id="authUser" class="btn btn-primary" onclick="authUser()">Obtain JWT</button>
</div>
<div class="col-2">
<br>
<button id="connect" class="btn btn-primary" onclick="startConnect()">Connect</button>
</div>
<br>
</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 = "";
function connectSocket() {
socketio = io.connect(
socketURL, {
query: 'tokenType=jwt&token=' + spacesToken,
transports: ['websocket']
}
);
socketio.on('connect', function () {
var spaceToSubscribe = {
channel: {
_id: spacesID,
type: 'topic'
}
};
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);
});
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) {
// This is a message with a file
message = msg.sender.displayname + ": " + message + " " + msg.content.data[0].name + " " + msg.content.data[0].path;
trace(message);
return;
}
}
// 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);
}
});
}
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'
}
};
socketio.emit('UNSUBSCRIBE_CHANNEL', spaceToUnsubscribe);
init();
clearTrace();
socketio.disconnect();
socketio = null;
}
function startConnect() {
if (document.getElementById("connect").innerHTML == "Disconnect") {
disconnect();
return;
}
spacesToken = document.getElementById('jwt').value;
email = document.getElementById('email').value;
$.ajax({
headers: {
'Authorization': 'jwt ' + spacesToken,
'Accept': 'application/json'
},
url: "https://spacesapis.avayacloud.com/api/users/meetingroom/" + email,
type: 'Get',
success: function (data) {
var userId = data.data.cid;
getDirectMessageSpace(userId);
},
error: function (error) {}
});
}
function getDirectMessageSpace(userId) {
$.ajax({
headers: {
'Authorization': 'jwt ' + spacesToken,
'Accept': 'application/json'
},
url: "https://spacesapis.avayacloud.com/api/spaces/direct/user/" + userId,
type: "GET",
success: function (data) {
spacesID = data.data[0]._id;
connectSocket();
}
});
}
function authUser() {
$.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;
document.getElementById('jwt').value = spacesToken;
},
error: function (error) {}
});
}
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>