I need to change some variables of a script from the view of my project, but I need to do it from the server or have the variable take the value I send it through websockets .
I tried the following:
//EN EL SERVER
io.sockets.on("connection",function(socket){
socket.emit("NombreDelSocket",UnDatoJSON);
});
//EN EL CLIENTE
var Nombre;
var socket = io.connect(RUTA DEL SERVIDOR);
socket.on("NombreDelSocket",function(data){
//data es UnDatoJSON que mando el servidor
Nombre = data[0].nombre;
console.log(Nombre);
});
When I check the browser console, the console.log(Nombre)
instruction does indeed print the name I wanted to send. Very good, but it does not change the name of the variable. If I show that message from the console outside of that method of socket
, it will tell me that the name is undefined
.
I do not know if you understand what I want to do. I just want to set a variable of a client script, but I do not know how to do it from websockets .