online users
This commit is contained in:
18
index.js
18
index.js
@ -37,27 +37,21 @@ import WebSocket, { WebSocketServer } from "ws";
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
let openSockets = 0;
|
||||
wss.on("connection", function connection(ws, req, res) {
|
||||
openSockets++;
|
||||
ws.send(`online=${wss.clients.size}`);
|
||||
setInterval(() => {
|
||||
ws.send("ping");
|
||||
}, 30000);
|
||||
ws.send(`online=${wss.clients.size}`);
|
||||
}, 5000);
|
||||
|
||||
ws.on("error", console.error);
|
||||
|
||||
ws.on("message", async function message(data, isBinary) {
|
||||
let message = Buffer.from(data).toString();
|
||||
if (message.startsWith(process.env.ANNOUNCEMENT_KEY)) {
|
||||
wss.clients.forEach(function each(client) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(message.replace(process.env.ANNOUNCEMENT_KEY, "announce."));
|
||||
}
|
||||
});
|
||||
} else if (message.startsWith("token") && (await verifyCookie(message.substring(6)))) {
|
||||
if (message.startsWith("token") && (await verifyCookie(message.substring(6)))) {
|
||||
ws.id = await getUserFromCookie(message.substring(6));
|
||||
ws.send(ws.id);
|
||||
const updateAccount = accs.query(`UPDATE accounts SET last_login = $login WHERE username = $user`)
|
||||
updateAccount.get({ $login: new Date().toUTCString(), $user: ws.id });
|
||||
} else if (message.startsWith("pong")) {
|
||||
} else if (message.startsWith("1")) {
|
||||
if (ws.id) {
|
||||
const updateAccount = accs.query(`UPDATE accounts SET last_login = $login WHERE username = $user`)
|
||||
updateAccount.get({ $login: new Date().toUTCString(), $user: ws.id });
|
||||
@ -65,7 +59,7 @@ wss.on("connection", function connection(ws, req, res) {
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("close", () => {openSockets--;});
|
||||
ws.on("close", () => {});
|
||||
});
|
||||
app.post(
|
||||
"/api/event",
|
||||
|
||||
Reference in New Issue
Block a user