diff options
| author | Adam Soltys <[email protected]> | 2020-06-13 21:39:22 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2020-06-13 21:39:22 -0700 |
| commit | 74b7d2a0d614f170ba5aea3a1a172a7a68356d80 (patch) | |
| tree | be2a73a9917616c0b49c24dc99642378e5a47549 /lib/sockets.js | |
| parent | dcb21adb40a44c8108f1d652cc6ddd59ae7c4024 (diff) | |
| download | coinos-server-74b7d2a0d614f170ba5aea3a1a172a7a68356d80.tar.gz coinos-server-74b7d2a0d614f170ba5aea3a1a172a7a68356d80.zip | |
push notifications
Diffstat (limited to 'lib/sockets.js')
| -rw-r--r-- | lib/sockets.js | 93 |
1 files changed, 56 insertions, 37 deletions
diff --git a/lib/sockets.js b/lib/sockets.js index 89beffb3..07af88d8 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -37,47 +37,66 @@ const names = {}; wss.on("connection", function(ws, req) { const id = uuidv4(); - setTimeout(() => ws.ok || ws.close(), 2000); + setTimeout(() => ws.user || ws.close(), 2000); - ws.on("message", async token => { - if (!token || token === "null") return; + ws.on("message", async (message) => { + let type, data; try { - const username = jwt.decode(token).username; - if (!sockets[username]) sockets[username] = {}; - sockets[username][id] = ws; - names[id] = username; + ({ type, data } = JSON.parse(message)); + } catch (e) { + l.error("coudn't parse socket message"); + } - let user = await getUser(username); - let payments = await user.getPayments({ - where: { - account_id: user.account_id, - }, - order: [["id", "DESC"]], - limit: 12, - include: { - model: db.Account, - as: "account" - } - }); - let accounts = await user.getAccounts(); - user = user.get({ plain: true }); - user.accounts = accounts; - user.payments = payments; + let token; + switch (type) { + case "login": + token = data; + if (!token || token === "null") return; + try { + const username = jwt.decode(token).username; + if (!sockets[username]) sockets[username] = {}; + sockets[username][id] = ws; + names[id] = username; - ws.ok = true; - ws.send(JSON.stringify({ type: "nodes", data: networks })); - ws.send(JSON.stringify({ type: "rates", data: app.get("rates") })); - ws.send(JSON.stringify({ type: "login", data: user })); - l.info("socket open", username, id); - } catch (e) { - ws.close(); - l.error( - "problem opening socket", - token, - e.message, - id, - req.connection.remoteAddress - ); + let user = await getUser(username); + let payments = await user.getPayments({ + where: { + account_id: user.account_id + }, + order: [["id", "DESC"]], + limit: 12, + include: { + model: db.Account, + as: "account" + } + }); + let accounts = await user.getAccounts(); + user = user.get({ plain: true }); + user.accounts = accounts; + user.payments = payments; + + ws.user = user; + ws.send(JSON.stringify({ type: "login", data: user })); + ws.send(JSON.stringify({ type: "version", data: clientVersion })); + l.info("socket open", username, id); + } catch (e) { + ws.close(); + l.error( + "problem opening socket", + token, + e.message, + id, + req.connection.remoteAddress + ); + } + break; + case "token": + token = data; + if (!ws.user) return; + ws.token = token; + break; + default: + l.warn("received socket message of unknown type", type, data); } }); |
