aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sockets.js
blob: 89beffb39867d8ee3921899ed2c73a79c5498508 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const jwt = require("jsonwebtoken");
const uuidv4 = require("uuid/v4");
const WebSocket = require("ws");

wss = new WebSocket.Server({ clientTracking: false, noServer: true });

/* eslint-disable-next-line */
const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {});
sockets = {};

emit = (username, type, data) => {
  if (data.username !== undefined) data = pick(data, ...whitelist);

  if (!sockets[username]) return;
  for (id in sockets[username]) {
    const ws = sockets[username][id];
    ws.send(JSON.stringify({ type, data }));
  }
};

broadcast = (type, data) => {
  for (const username in sockets) {
    for (const id in sockets[username]) {
      const ws = sockets[username][id];
      ws.send(JSON.stringify({ type, data }));
    }
  }
};

server.on("upgrade", function(req, socket, head) {
  wss.handleUpgrade(req, socket, head, function(ws) {
    wss.emit("connection", ws, req);
  });
});

const names = {};

wss.on("connection", function(ws, req) {
  const id = uuidv4();
  setTimeout(() => ws.ok || ws.close(), 2000);

  ws.on("message", async token => {
    if (!token || token === "null") return;
    try {
      const username = jwt.decode(token).username;
      if (!sockets[username]) sockets[username] = {};
      sockets[username][id] = ws;
      names[id] = username;

      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.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
      );
    }
  });

  ws.on("close", function() {
    try {
      const username = names[id];
      if (sockets[username] && sockets[username][id])
        delete sockets[username][id];
      l.info("socket closed", username, id);
    } catch (e) {
      l.error("problem closing socket", e.message);
    }
  });
});