~ev/news

069196d44ae3c8861c33ab733d617a4b6dc4e2e9 — Ev Bogue 2 years ago f82a023
rewrite websocket handler as a Map (per deno chat server example)
1 files changed, 13 insertions(+), 9 deletions(-)

M server.js
M server.js => server.js +13 -9
@@ 48,18 48,21 @@ setInterval(function () {
  Deno.writeTextFile(appdir + 'log', logfile)
}, 25000)

const clients = []
const clients = new Map()
  
function dispatch (msg) {
  clients.forEach(client => {
    client.send(msg)
  })
  for (const client of clients.values()) {
    client.send(msg);
  }
}

let clientId = 0;

async function wsHandler (ws) {
  clients.push(ws)
  const id = ++clientId;
  clients.set(id, ws);
  for await (const msg of ws) {
    if (typeof msg === "string"){
    if (typeof msg === "string") {
      if (msg.length > 44) {
        if (!log.includes(msg)) {
          log.push(msg)


@@ 81,11 84,12 @@ async function wsHandler (ws) {
        }
      }
      dispatch(msg);
    } /*else if (isWebSocketCloseEvent(msg)) {
    }
    else if (isWebSocketCloseEvent(msg)) {
      clients.delete(id);
      dispatch(`Closed: [${id}]`);
      break
    }*/
      break;
    }
  }
}