Nodejs as the backend, using websocket to write chat rooms, how to get the ip that connects users?

1.nodejs uses the ws library
2. The code for the user to disconnect is written as follows

var WebSocketServer = require("D:/0.ProgramFiles/2.Devel/4.NodeJs/node_modules/ws").Server;
var wss = new WebSocketServer({port: 8888});
var userList = [];

wss.on("connection", function (ws,req) {
    // console.log(req.connection.remoteAddress)
    console.log(getUUID());

    ws.on("message", function (message) {
        console.log(":" + message);
        wss.clients.forEach(function each(client) {
            client.send(message);
        });
    });

    ws.on("close", function (message) {
        console.log(id);
    });

});

the current situation is:
after the user closes the foreground web page directly, the backend can only receive the number 1001,

how can I get exactly which user has gone offline?


make good use of documents

https://www.npmjs.com/package.

wss.on('connection', ws => {
  const id = getUUID();
  
  ws.on('close', (code, message) => {
    console.log(`user ${id} has left`);
  };
});

of course, the chat room must have a login interface. onmessage take out the account and deposit it to id . In short, bookkeeping has to do it himself, and the ws package will not do it for you.

Please use markdown, for your convenience.

Menu