diff options
| author | Adam Soltys <[email protected]> | 2023-04-11 19:22:10 +0000 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-04-11 19:22:14 +0000 |
| commit | f8b0e547e6cf055636f765833e430cf06549fcbe (patch) | |
| tree | 3a503e082a788af7fb77b5befb2663515d5d0632 /lib/sockets.js | |
| parent | 4d480b400a3a7d6990aac4a0cacccd86c0fd9cd3 (diff) | |
| download | coinos-server-f8b0e547e6cf055636f765833e430cf06549fcbe.tar.gz coinos-server-f8b0e547e6cf055636f765833e430cf06549fcbe.zip | |
upgrade websocket lib
Diffstat (limited to 'lib/sockets.js')
| -rw-r--r-- | lib/sockets.js | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/sockets.js b/lib/sockets.js index 458c77a9..7be4936b 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -41,7 +41,7 @@ export const broadcast = (type, data) => { } } - Object.values(all).map(ws => { + Object.values(all).map((ws) => { if (!sent.includes(ws.id)) ws.send(JSON.stringify({ type, data })); }); }; @@ -49,14 +49,17 @@ export const broadcast = (type, data) => { export async function socketServer(app) { let seconds = (n, d = new Date()) => d.setSeconds(d.getSeconds() - n) && d; - app.get("/ws", { websocket: true }, ({ socket: ws }, req) => { + app.get("/ws", { websocket: true }, (connection, req) => { + let { socket: ws } = connection; + connection.on("error", console.log); + const id = v4(); ws.id = id; ws.beats = 0; ws.send(JSON.stringify({ type: "connected", data: id })); all[id] = ws; - ws.on("message", async message => { + ws.on("message", async (message) => { let type, data; try { ({ type, data } = JSON.parse(message)); @@ -87,7 +90,7 @@ export async function socketServer(app) { if (!store.sockets[uid]) store.sockets[uid] = {}; const existing = Object.keys(store.sockets[uid]); if (existing.length > 4) { - const p = existing.find(sid => sid !== uid); + const p = existing.find((sid) => sid !== uid); store.sockets[uid][p].send(JSON.stringify({ type: "logout" })); setTimeout(() => { if (store.sockets[uid][p]) @@ -132,7 +135,7 @@ export async function socketServer(app) { ws.on("error", console.log); - ws.on("close", function(code, reason) { + ws.on("close", function (code, reason) { try { const uid = users[id]; if (store.sockets[uid] && store.sockets[uid][id]) { |
