Add stuff :)

This commit is contained in:
Christopher Cookman 2025-04-17 23:39:03 -06:00
parent 94d5930cf4
commit 21ddfe4160

145
index.js
View file

@ -16,7 +16,7 @@ expressWs(app);
// Serve static files from the "public" directory // Serve static files from the "public" directory
app.use(express.static('public')); app.use(express.static('public'));
global.wsConnections = []; global.wsConnections = [];
var roomList = [];
// IEM WebSocket // IEM WebSocket
app.ws('/iem', (ws, req) => { app.ws('/iem', (ws, req) => {
console.log(`connection from ${req.ip}`); console.log(`connection from ${req.ip}`);
@ -33,57 +33,116 @@ app.ws('/iem', (ws, req) => {
console.log(`disconnected from ${req.ip}`); console.log(`disconnected from ${req.ip}`);
wsConnections = wsConnections.filter((conn) => conn.ws !== ws); wsConnections = wsConnections.filter((conn) => conn.ws !== ws);
}); });
ws.on('message', (msg) => { try {
const data = JSON.parse(msg); ws.on('message', (msg) => {
if (!data.type) return; const data = JSON.parse(msg);
switch (data.type) { if (!data.type) return;
case "subscribe": switch (data.type) {
if (data.channel) { case "subscribe":
if (!wsConnections[sock - 1].subs.includes(data.channel)) { const subscriptionTarget = data.channel || getWFOroom(data.wfo) || "*";
wsConnections[sock - 1].subs.push(data.channel); if (subscriptionTarget === "*") {
wsConnections[sock - 1].subs.push(subscriptionTarget);
ws.send(JSON.stringify({ ws.send(JSON.stringify({
"type": "internal-response", "type": "internal-response",
"code": 200, "code": 200,
"data": { "data": {
"message": `Subscribed to ${data.channel}` "message": `Subscribed to all channels.`
} }
})); }));
} else { break;
ws.send(JSON.stringify({ } else if(!roomList.includes(subscriptionTarget)) {
"type": "internal-response",
"code": 409,
"data": {
"error": "Already subscribed to this channel."
}
}));
}
}
break;
case "unsubscribe":
if (data.channel) {
if (wsConnections[sock - 1].subs.includes(data.channel)) {
wsConnections[sock - 1].subs = wsConnections[sock - 1].subs.filter((sub) => sub !== data.channel);
ws.send(JSON.stringify({
"type": "internal-response",
"code": 200,
"data": {
"message": `Unsubscribed from ${data.channel}`
}
}));
} else {
ws.send(JSON.stringify({ ws.send(JSON.stringify({
"type": "internal-response", "type": "internal-response",
"code": 404, "code": 404,
"data": { "data": {
"error": "Not subscribed to this channel." "error": "Invalid channel."
}
}));
break;
} else {
wsConnections[sock - 1].subs.push(subscriptionTarget);
ws.send(JSON.stringify({
"type": "internal-response",
"code": 200,
"data": {
"message": `Subscribed to ${subscriptionTarget}`
}
}));
break;
}
case "unsubscribe":
const unsubscribeTarget = data.channel || getWFOroom(data.wfo) || "*";
if (unsubscribeTarget === "*") {
wsConnections[sock - 1].subs = wsConnections[sock - 1].subs.filter((sub) => sub !== "*");
ws.send(JSON.stringify({
"type": "internal-response",
"code": 200,
"data": {
"message": `Unsubscribed from all channels.`
}
}));
break;
} else if(!getWFO(data.channel)) {
ws.send(JSON.stringify({
"type": "internal-response",
"code": 404,
"data": {
"error": "Invalid channel."
}
}));
break;
} else {
wsConnections[sock - 1].subs = wsConnections[sock - 1].subs.filter((sub) => sub !== unsubscribeTarget);
ws.send(JSON.stringify({
"type": "internal-response",
"code": 200,
"data": {
"message": `Unsubscribed from ${unsubscribeTarget}`
}
}));
break;
}
case "get-subscriptions":
ws.send(JSON.stringify({
"type": "internal-response",
"code": 200,
"data": {
"subscriptions": wsConnections[sock - 1].subs
}
}));
break;
case "room-list":
if (roomList.length > 0) {
ws.send(JSON.stringify({
"type": "room-list",
"code": 200,
"count": roomList.length,
"data": roomList
}));
} else {
ws.send(JSON.stringify({
"type": "room-list",
"code": 503,
"count": roomList.length,
"data": {
"error": "Room list is currently empty. Please try again later."
} }
})); }));
} }
} }
break; });
} } catch (error) {
}); console.error(error);
}); ws.send(JSON.stringify({
"type": "internal-response",
"code": 500,
"data": {
"error": "Internal server error."
}
}));
}
})
// Random funcs // Random funcs
function toTitleCase(str) { function toTitleCase(str) {
@ -150,8 +209,11 @@ const getFirstURL = function (string) {
// Function to get the room name from the WFO code // Function to get the room name from the WFO code
const getWFOroom = function (code) { const getWFOroom = function (code) {
if (typeof code !== 'string') {
return null;
}
code = code.toLowerCase(); code = code.toLowerCase();
if (wfos[code]) { if (wfos[code] && typeof wfos[code].room === 'string') {
return wfos[code].room; return wfos[code].room;
} else { } else {
return code; return code;
@ -268,6 +330,7 @@ xmpp.on("stanza", (stanza) => {
// Join the room // Join the room
//xmpp.send(xml("presence", { to: `${channel.jid}/${channel.name}/${curUUID}` }, xml("item", { role: "visitor" }))); //xmpp.send(xml("presence", { to: `${channel.jid}/${channel.name}/${curUUID}` }, xml("item", { role: "visitor" })));
xmpp.send(xml("presence", { to: `${item.attrs.jid}/${curUUID}` }, xml("item", { role: "visitor" }))); xmpp.send(xml("presence", { to: `${item.attrs.jid}/${curUUID}` }, xml("item", { role: "visitor" })));
roomList.push(item.attrs.jid.split("@")[0]);
}); });
} }
} }
@ -339,7 +402,7 @@ xmpp.on("stanza", (stanza) => {
}); });
xmpp.on("status", (status) => { xmpp.on("status", (status) => {
console.log(`${colors.cyan("[INFO]")} XMPP Status: ${status}`); console.log(`${colors.cyan("[INFO]")} XMPP Status`);
// Broadcast a message to all connected WebSocket clients // Broadcast a message to all connected WebSocket clients
wsConnections.forEach((connection) => { wsConnections.forEach((connection) => {
if (connection.ws.readyState === 1) { // Ensure the socket is open if (connection.ws.readyState === 1) { // Ensure the socket is open