aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sockets.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sockets.js')
-rw-r--r--lib/sockets.js47
1 files changed, 20 insertions, 27 deletions
diff --git a/lib/sockets.js b/lib/sockets.js
index 6ea369c0..de6986b1 100644
--- a/lib/sockets.js
+++ b/lib/sockets.js
@@ -1,7 +1,7 @@
-import { sockets } from "./store.js";
+import store from "./store.js";
import app from "../app.js";
import jwt from "jsonwebtoken";
-import { v4 as uuidv4 } from "uuid";
+import { v4 } from "uuid";
import WebSocket from "ws";
import { differenceInSeconds, subSeconds } from "date-fns";
import fastifyWs from "@fastify/websocket";
@@ -35,18 +35,18 @@ export const emit = (username, type, data) => {
}
}
- if (!sockets[username]) return;
- for (id in sockets[username]) {
- const ws = sockets[username][id];
+ if (!store.sockets[username]) return;
+ for (id in store.sockets[username]) {
+ const ws = store.sockets[username][id];
ws.send(JSON.stringify({ type, data }));
}
};
export const broadcast = (type, data) => {
const sent = [];
- for (const username in sockets) {
- for (const id in sockets[username]) {
- const ws = sockets[username][id];
+ for (const username in store.sockets) {
+ for (const id in store.sockets[username]) {
+ const ws = store.sockets[username][id];
ws.send(JSON.stringify({ type, data }));
sent.push(ws.id);
}
@@ -60,7 +60,7 @@ export const broadcast = (type, data) => {
app.register(fastifyWs);
app.register(async function(app) {
app.get("/ws", { websocket: true }, ({ socket: ws }, req) => {
- const id = uuidv4();
+ const id = v4();
ws.id = id;
ws.beats = 0;
ws.send(JSON.stringify({ type: "connected", data: id }));
@@ -92,17 +92,17 @@ app.register(async function(app) {
const username = jwt.decode(token).username;
if (!username) throw new Error("Invalid JWT token");
- if (!sockets[username]) sockets[username] = {};
- const existing = Object.keys(sockets[username]);
+ if (!store.sockets[username]) store.sockets[username] = {};
+ const existing = Object.keys(store.sockets[username]);
if (existing.length > 4) {
const p = existing.find(sid => sid !== id);
- sockets[username][p].send(JSON.stringify({ type: "logout" }));
+ store.sockets[username][p].send(JSON.stringify({ type: "logout" }));
setTimeout(() => {
- if (sockets[username][p])
- sockets[username][p].close(code, "too many sockets");
+ if (store.sockets[username][p])
+ store.sockets[username][p].close(code, "too many sockets");
}, 5000);
}
- sockets[username][id] = ws;
+ store.sockets[username][id] = ws;
names[id] = username;
let user = await getUser(username);
@@ -187,8 +187,8 @@ app.register(async function(app) {
// if (reason) console.log("closing socket", reason);
try {
const username = names[id];
- if (sockets[username] && sockets[username][id]) {
- delete sockets[username][id];
+ if (store.sockets[username] && store.sockets[username][id]) {
+ delete store.sockets[username][id];
}
if (all[id]) delete all[id];
@@ -199,19 +199,12 @@ app.register(async function(app) {
});
});
-app.listen({ port: 3000 }, err => {
- if (err) {
- fastify.log.error(err);
- process.exit(1);
- }
-});
-
const names = {};
setInterval(() => {
- for (const username in sockets) {
- for (const id in sockets[username]) {
- let ws = sockets[username][id];
+ for (const username in store.sockets) {
+ for (const id in store.sockets[username]) {
+ let ws = store.sockets[username][id];
ws.beats++;
if (ws.beats > 10) ws.close(code, `${username} ${id} lost heartbeat`);
}