2
Hello, I am studying sockets with Node js for games and I would like to know how to recover the id of a client socket connection with the server, I have tried several ways but did not succeed!
My client code is as follows::
var net = require('net');
var client = new net.Socket(); //Cria o socket do cliente
client.connect(3000, '127.0.0.1', function() { //Inicia o socket do cliente
console.log('Conectado ao servidor');
client.write('Olá servidor! De, Cliente.');
});
client.on('data', function(data) {
console.log('Recebido: ' + data);
});
My server code is as follows:
var net = require("net"); //Importa a biblioteca
var porta = 3000; //Define qual porta o servidor vai escutar
var host = "127.0.0.1";
var servidor = net.createServer(); //Cria o socket do servidor
servidor.listen(porta, function (socket){ //Roda o servidor
console.log(`Servidor iniciado..`);
});
servidor.on('connection', function(socket){
console.log(`Nova coneção`);
socket.on('data', function(data){
console.log(`Recebido: ` + data);
})
});
How can I get the client connection/socket id to have a control on my server?
My code is very simple, I know very little and, I’m just studying how the sockets work in Ode, if you have suggestions for improvements or criticism I’m totally open to receiving them!!