-
Notifications
You must be signed in to change notification settings - Fork 0
/
appv3.2.js
290 lines (261 loc) · 9.81 KB
/
appv3.2.js
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
app = {
connection: function () {
// Chat app MQTT settings
var USER = document.getElementById('login').value;
var mqttServer = "ws://broker.hivemq.com:8000/mqtt";
var mqttTopic = "codettes2022";
var userName = USER;//document.getElementById("logon-as").value; // || "anonymous user"
//var userName = "anonymous usr";
const clientId = 'cb22_' + Math.random().toString(16).substr(2, 8)
var userList = [];
const opts = {
keepalive: 30,
clientId: clientId,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
will: {
topic: 'WillMsg',
payload: 'Connection Closed abnormally..!',
qos: 0,
retain: false
},
rejectUnauthorized: false
}
console.log('connecting mqtt client')
const client = mqtt.connect(mqttServer, opts);
client.on('error', function (err) {
console.log(err)
client.end()
})
client.on('connect', function () {
// Once a connection has been made, make a subscription and send a message.
console.log('client connected:' + clientId)
client.subscribe(mqttTopic, { qos: 0 })
message = (USER + " is finally back!!");
message2 = (USER + " just joined! its been a while say hi!!");
message3 = (USER + "is in the chat!!");
message4 = ("welcome " + USER + ", we hope you brought boogie's!!");
message.destinationName = mqttTopic;
message2.destinationName = mqttTopic;
message3.destinationName = mqttTopic;
message4.destinationName = mqttTopic;
var values = [message, message2, message3, message4]
ToUse = values[Math.floor(Math.random() * values.length)];
client.publish(mqttTopic, ToUse, { qos: 0, retain: false })
sendPong();
sendPing();
})
client.on('message', function (topic, message, packet) {
if (toggle == 0) {
msg = message.toString(); // library delivers buffer so convert to strig first
console.log("onMessageArrived: " + msg);
// if it has JSON payload do NOT add to chat
try {
msgObj = JSON.parse(message.toString()); // t is JSON so handle it how u want
// if message has Pin of Pong in it send it to the PingPongHandler
if (Object.keys(msgObj)[0] == "ping") { sendPong(); };
if (Object.keys(msgObj)[0] == "pong") { handlePong(msgObj.pong); }; // pong value is an object!!
// other handlers for control messages below
} catch {
document.getElementById("chatlog").innerHTML += "<br>" + msg;
//sendPing();
}
}
})
// --- To manage userlist implement a PING and PONG system ---
// Ping will request an "alive sign" from all or any user
// Pong function will respond to ping
// handlePong pongs in the UI
// Ping will be scheduled to run regularly as a KeepAlive signal
function sendPing(usr = '*') {
if (toggle == 0) {
// ping sends out a message to all (*) or any specific user to respond if ur there
var pingObj = { ping: usr }; // JS Object {ping : "usr"} -> JSON {/"ping/":/"usr/"}
client.publish(mqttTopic, JSON.stringify(pingObj));
}
}
function sendPong() {
if (toggle == 0) {
// sends clientID and UserName in a JSON object (and whatever u need more)
var pongObj = { pong: { userName: userName, clientId: clientId } };
client.publish(mqttTopic, JSON.stringify(pongObj));
console.log(JSON.stringify(pongObj));
}
}
function redrawUserList() {
if (toggle == 0) {
// Generate the userlist HTML
var ulist = "";
userList.forEach(function (item) {
//var x = arrayItem.prop1 + 2;
var user1 = "<img src='original.jpg'></img>";
var user2 = "<img src='im1.jpg'></img>";
var user3 = "<img src='im2.jpg'></img>";
var user4 = "<img src='im3.jpg'></img>";
var values = [user1, user2, user3, user4]
ToUse = values[Math.floor(Math.random() * values.length)];
var CHATBUBBLE = '<div>';//</h4></div>
ulist += "<a><li>" + ToUse + item.userName + "<i class='fa fa-fw fa-phone'></i></a></li>" + "<br>"
//ulist+= CHATBUBBLE + item.userName + "<a href='#" + mqttTopic + "/" + item.clientId +"'><i class='fa fa-fw fa-phone'></i></a></li>"+ "</h4></div>" +"<br>"
});
//ulist+="</ul>";
console.log(ulist);
document.getElementById("ulist").innerHTML = ulist;
}
}
// function that manages the UserList and other UI stuff related to PingPong
function handlePong(pongObj) {
if (toggle == 0) {
const index = userList.findIndex(object => {
return object.userName === pongObj.userName;
// Update Userlist with Pongs
});
//console.log("index:" + index);
if (index >= 0) {
console.log("User exists");
userList[index] = pongObj;
} else {
console.log("New User " + pongObj.userName);
userList.push(pongObj);
}
//console.log(userList);
redrawUserList();
}
}
// to keep connection alive
//setInterval(sendPong, 10000); // keeps ponging every 10 secs
}
}
var USER = document.getElementById('login').value;
var ID = document.getElementById('admin').value;
function getInput() {
var USER = document.getElementById('login').value;
var ID = document.getElementById('admin').value;
document.getElementById("login-box").className = "nothing";
document.getElementById("chatapp").className = "chatapp";
console.log("alert{>>'new user signed on as [" + USER + "] with:id:of[" + ID + "]'<<}");
console.log("dialog hidden");
app.connection();
}
function sendPing(usr = '*') {
if (toggle == 0) {
// ping sends out a message to all (*) or any specific user to respond if ur there
var pingObj = { ping: usr }; // JS Object {ping : "usr"} -> JSON {/"ping/":/"usr/"}
client.publish(mqttTopic, JSON.stringify(pingObj));
}
}
// Chat app MQTT settings
var toggle = 0;
var USER = document.getElementById('login').value;
var mqttServer = "ws://broker.hivemq.com:8000/mqtt";
var mqttTopic = "codettes2022";
var userName = USER;//document.getElementById("logon-as").value; // || "anonymous user"
//var userName = "anonymous usr";
const clientId = 'cb22_' + Math.random().toString(16).substr(2, 8)
var userList = [];
const opts = {
keepalive: 20,
clientId: clientId,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 20 * 1000,
will: {
topic: 'WillMsg',
payload: 'Connection Closed abnormally..!',
qos: 0,
retain: false
},
rejectUnauthorized: false
}
console.log('connecting mqtt client')
const client = mqtt.connect(mqttServer, opts);
client.on('close', function () {
console.log(clientId + ' disconnected')
})
function sendMessageButton(msgtext) {
if (msgtext != '' && toggle == 0) {
//sendPing();
var USER = document.getElementById('login').value;
var userName = USER;//document.getElementById("loginName").value; // || "anonymous user"
client.publish(mqttTopic, userName + " says: " + msgtext);
if (msgtext == "whoami") {
console.log(userName);
client.publish(mqttTopic, "Your name is " + userName + ", silly");
}
else if (msgtext == "disconnect") {
var t = document.getElementById("toggle");
//client.publish(mqttTopic, "disconnected user " + USER + " from topic " + mqttTopic);
console.log("disconnected user " + USER + " from topic " + mqttTopic)
t.value = "disconnected";
toggle = 1;
console.log("off")
console.log("disconnected")
client.subscribe("", { qos: 0 })
}
}
}
// OR listen to the Enter event on n input box
function sendMsg(ele) {
if (event.key === 'Enter' && toggle == 0) {
var USER = document.getElementById('login').value;
var userName = USER;//document.getElementById("loginName").value; // || "anonymous user"
if (ele.value == "whoami") {
console.log(userName);
client.publish(mqttTopic, "Your name is " + userName + ", silly");
}
else if (ele.value == "disconnect") {
var t = document.getElementById("toggle");
console.log("disconnected user " + USER + " from topic " + mqttTopic)
t.value = "disconnected";
toggle = 1;
console.log("off")
console.log("disconnected")
client.subscribe("", { qos: 0 })
}
client.publish(mqttTopic, userName + " says: " + ele.value);
//alert(ele.value);
ele.value = ""; // reset the input after entering
// sendPing();
}
}
function ondisconnect() {
var t = document.getElementById("toggle");
if (t.value == "disconnected") {
t.value = "connected";
toggle = 0
console.log("on")
app.connection();
//client.subscribe(mqttTopic, { qos: 0 })
}
else if (t.value == "connected") {
t.value = "disconnected";
toggle = 1;
console.log("off")
console.log("disconnected")
client.subscribe("", { qos: 0 })
}/*
*/
}
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}