Problem while emitting JSON object
Hi, I'm writting a small web application, and using Socket.IO for the communication between my server (in Node.js) and the web app.
When I send JSON data from the server to the client, it works well, and I receive the data correctly in the client and I can work with it. The problem cames when I try to do the reverse way, send an emit with some JSON data. The server appears to don't receive anything. I tried to replace the serviceList[i].name by a string and it works well...
Here is the server's code. I send the JSON result with an emit, and it works well.
io.on('connection', function(socket){
console.log("User connected to Server");
socket.on('check-service', function(obj){
console.log(obj);
var result = {
nombre: obj.nombre,
estado: 0
};
request.get(url+obj.url, function(error, response, body){
if(error){
console.log(error);
}
else{
if(body === '{"result":[0]}'){
result.estado = 0;
}
if(body === '{"result":[1]}'){
result.estado = 1;
}
}
console.log("Result "+result["nombre"]+": "+result["estado"]);
socket.emit('check-result', result);
});
});
Now, in the Web client, I first receive the data (serviceList), but when I try to emit another event with some of that data, the log doesn't show anything (event check-service):
socket.on('DB-Info', function(serviceList){
//Other stuff..... serviceList corresponds to the result emitted by the server...
var service = {
name: serviceList[i].nombre,
url: serviceList[i].url
};
socket.emit('check-service', service);
I don't know if I'm using in the wrong way the JSON object, but I tried multiple ways and I can't solve it. Anyone can help? Thanks!
What is the type of serviceList[i].url? My first guess is that it's not something that should be in JSON.
It's an string... the problem doesn't seem to be with the data type, but anyway thank you