This commit is contained in:
Christopher Cookman 2025-04-15 18:15:02 -06:00
parent ff0a1215c2
commit fe5ac6073a
2 changed files with 62 additions and 13 deletions

View file

@ -8,6 +8,7 @@ const html = require('html-entities');
const blacklist = require("./blacklist.json") const blacklist = require("./blacklist.json")
const wfos = require("./wfos.json") const wfos = require("./wfos.json")
const events = require("./events.json") const events = require("./events.json")
const os = require('os');
const app = express(); const app = express();
expressWs(app); expressWs(app);
@ -19,6 +20,14 @@ global.wsConnections = [];
// 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}`);
const hostname = os.hostname();
ws.send(JSON.stringify({
"type": "connection-info",
"state": true,
"uuid": curUUID,
"host": hostname
}));
wsConnections.push(ws); wsConnections.push(ws);
ws.on('close', () => { ws.on('close', () => {
console.log(`disconnected from ${req.ip}`); console.log(`disconnected from ${req.ip}`);
@ -221,9 +230,11 @@ xmpp.on("stanza", (stanza) => {
if (stanza.is("message") && stanza.attrs.type === "groupchat") { if (stanza.is("message") && stanza.attrs.type === "groupchat") {
clearTimeout(restartTimer) clearTimeout(restartTimer)
restartTimer = setTimeout(() => { restartTimer = setTimeout(() => {
console.log(`${colors.red("[FATAL]")} No messages from weather.im in 10 minutes, restarting!!!!!!!!!!!`) console.log(`${colors.yellow("[WARN]")} Restarting XMPP connection...`);
process.exit(1) xmpp.disconnect().then(() => {
}, 600000) xmpp.start();
});
}, 10000)
// Stops spam from getting old messages // Stops spam from getting old messages
if (startup) return; if (startup) return;
// Get channel name // Get channel name
@ -433,16 +444,19 @@ xmpp.on("stanza", (stanza) => {
if (wsConnections.length > 0) { if (wsConnections.length > 0) {
wsConnections.forEach((ws) => { wsConnections.forEach((ws) => {
ws.send(JSON.stringify({ ws.send(JSON.stringify({
"channel": getWFOByRoom(fromChannel), "type": "iem-message",
"event": evt, "data": {
"body": bodyData.string, "channel": getWFOByRoom(fromChannel),
"timestamp": product_id.timestamp, "event": evt,
"wmo": product_id.wmo, "body": bodyData.string,
"pil": product_id.pil, "timestamp": product_id.timestamp,
"station": product_id.station, "wmo": product_id.wmo,
"raw": product_id_raw, "pil": product_id.pil,
"rawBody": body, "station": product_id.station,
"image": stanza.getChild("x").attrs.twitter_media || null "raw": product_id_raw,
"rawBody": body,
"image": stanza.getChild("x").attrs.twitter_media || null
}
})); }));
}); });
} }

35
public/index.html Normal file
View file

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="stuff"></div>
<script>
const socket = new WebSocket(`ws://${window.location.host}/iem`);
socket.onopen = () => {
const newDiv = document.createElement('div');
newDiv.innerText = "WebSocket connection opened";
document.getElementById('stuff').prepend(newDiv);
};
socket.onclose = () => {
const newDiv = document.createElement('div');
newDiv.innerText = "WebSocket connection closed";
document.getElementById('stuff').prepend(newDiv);
};
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
const explanation = `Event: ${JSON.stringify(data.event)}, Channel: ${JSON.stringify(data.channel)}, Station: ${data.station}`;
console.log(explanation);
const newDiv = document.createElement('div');
newDiv.innerText = explanation;
document.getElementById('stuff').prepend(newDiv);
};
</script>
</body>
</html>