From ffab3e8b298e0677c6e7bc4ff282e43ba71d3107 Mon Sep 17 00:00:00 2001
From: Adhidarma Hadiwinoto <adhisimon@gmail.com>
Date: Mon, 7 Feb 2022 15:49:52 +0700
Subject: [PATCH] Fix xid pairs

---
 server.js | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/server.js b/server.js
index 53787a2..9ca1059 100644
--- a/server.js
+++ b/server.js
@@ -41,12 +41,15 @@ if (!wsListenPort) {
     const wss = new WebSocketServer({ noServer: true, perMessageDeflate: true });
 
     wss.on('connection', (ws, req, client) => {
-        const connectionXid = uniqid();
-
         // eslint-disable-next-line no-param-reassign
         ws.isAlive = true;
 
-        const { remoteAddress, apikey, apikeyName } = client;
+        const {
+            xid: connectionXid,
+            remoteAddress,
+            apikey,
+            apikeyName,
+        } = client;
 
         logger.info(`${MODULE_NAME} F7755A03: Client connected`, {
             xid: connectionXid,
@@ -98,8 +101,8 @@ if (!wsListenPort) {
 
             logger.verbose(`${MODULE_NAME} 72D2A702: Got a message`, {
                 xid,
-                remoteAddress,
-                apikey,
+                apikeyName,
+                // apikey,
                 msgType,
                 msg,
                 msgSize: data.toString().length,
@@ -163,6 +166,8 @@ if (!wsListenPort) {
     });
 
     server.on('upgrade', (req, socket, head) => {
+        const xid = uniqid();
+
         const apikey = req.headers && (req.headers.apikey || req.headers.token);
 
         const matchedApikey = isValidApikey(
@@ -177,12 +182,14 @@ if (!wsListenPort) {
 
         wss.handleUpgrade(req, socket, head, (ws) => {
             const client = {
+                xid,
                 remoteAddress: req.socket.remoteAddress,
                 apikeyName: matchedApikey.name,
                 apikey,
             };
 
             logger.verbose(`${MODULE_NAME} AB8BC2F4: Incoming HTTP connection`, {
+                xid,
                 headers: req.headers,
                 client,
             });
-- 
1.9.0