From c4e63687c05284f9f7105e1925ccfa9c668cfb81 Mon Sep 17 00:00:00 2001 From: Jon Eugster Date: Thu, 4 May 2023 16:03:16 +0200 Subject: [PATCH 1/3] add log messages --- server/index.mjs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/server/index.mjs b/server/index.mjs index a996681..6877bec 100644 --- a/server/index.mjs +++ b/server/index.mjs @@ -34,6 +34,8 @@ const server = app const wss = new WebSocketServer({ server }) +var socketCounter = 0 + const environment = process.env.NODE_ENV const isDevelopment = environment === 'development' @@ -90,6 +92,10 @@ wss.addListener("connection", function(ws, req) { fillQueue(gameId) } + this.socketCounter += 1; + const ip = anonymize(req.headers['x-forwarded-for'] || req.socket.remoteAddress) + console.log(`[${new Date()}] Socket opened - ${ip}`) + const socket = { onMessage: (cb) => { ws.on("message", cb) }, onError: (cb) => { ws.on("error", cb) }, @@ -101,13 +107,22 @@ wss.addListener("connection", function(ws, req) { const socketConnection = jsonrpcserver.createConnection(reader, writer, () => ws.close()) const serverConnection = jsonrpcserver.createProcessStreamConnection(ps); socketConnection.forward(serverConnection, message => { - console.log(`CLIENT: ${JSON.stringify(message)}`) + if (isDevelopment) {console.log(`CLIENT: ${JSON.stringify(message)}`)} return message; }); serverConnection.forward(socketConnection, message => { - console.log(`SERVER: ${JSON.stringify(message)}`) + if (isDevelopment) {console.log(`SERVER: ${JSON.stringify(message)}`)} return message; }); + + console.log(`[${new Date()}] Number of open sockets - ${this.socketCounter}`) + console.log(`[${new Date()}] Free RAM - ${Math.round(os.freemem() / 1024 / 1024)} / ${Math.round(os.totalmem() / 1024 / 1024)} GB`) + + ws.on('close', () => { + console.log(`[${new Date()}] Socket closed - ${ip}`) + this.socketCounter -= 1; + }) + socketConnection.onClose(() => serverConnection.dispose()); serverConnection.onClose(() => socketConnection.dispose()); }) From 93b02883b3a6cf6ced2a527d4305778607bfef2e Mon Sep 17 00:00:00 2001 From: Jon Eugster Date: Thu, 4 May 2023 16:05:14 +0200 Subject: [PATCH 2/3] typo --- server/index.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/index.mjs b/server/index.mjs index 6877bec..325951e 100644 --- a/server/index.mjs +++ b/server/index.mjs @@ -92,7 +92,7 @@ wss.addListener("connection", function(ws, req) { fillQueue(gameId) } - this.socketCounter += 1; + socketCounter += 1; const ip = anonymize(req.headers['x-forwarded-for'] || req.socket.remoteAddress) console.log(`[${new Date()}] Socket opened - ${ip}`) @@ -115,12 +115,12 @@ wss.addListener("connection", function(ws, req) { return message; }); - console.log(`[${new Date()}] Number of open sockets - ${this.socketCounter}`) + console.log(`[${new Date()}] Number of open sockets - ${socketCounter}`) console.log(`[${new Date()}] Free RAM - ${Math.round(os.freemem() / 1024 / 1024)} / ${Math.round(os.totalmem() / 1024 / 1024)} GB`) ws.on('close', () => { console.log(`[${new Date()}] Socket closed - ${ip}`) - this.socketCounter -= 1; + socketCounter -= 1; }) socketConnection.onClose(() => serverConnection.dispose()); From 7c650255984ddfef742f054a8395de3ea3e581a1 Mon Sep 17 00:00:00 2001 From: Jon Eugster Date: Thu, 4 May 2023 16:09:44 +0200 Subject: [PATCH 3/3] add-imports --- server/index.mjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/index.mjs b/server/index.mjs index 325951e..d26bf22 100644 --- a/server/index.mjs +++ b/server/index.mjs @@ -5,6 +5,8 @@ import * as cp from 'child_process'; import * as url from 'url'; import * as rpc from 'vscode-ws-jsonrpc'; import * as jsonrpcserver from 'vscode-ws-jsonrpc/server'; +import os from 'os'; +import anonymize from 'ip-anonymize'; const games = { adam: {