我正在尝试使用socket.io从节点服务器在客户端执行函数。数据到达客户端没问题,但我无法在连接上执行功能。
我的服务器代码(循环发出):
var io = require('socket.io')(server);
io.on('connection', function(client){
setInterval(function(){
client.emit('for_client', datatosend);
}, 100);
});
我的客户端代码:
socket.on('for_client', function (data) {
console.log(data);
switch(true) {
case (data == "reload"):
console.log('DO reload'); //<<< not working
break;
case (data == "switch"):
console.log('DO switch'); //<<< not working
break;
default:
console.log(data); //<<< this DOES work
}
});
这是证明数据正在通过的证据;)
非常感谢任何帮助。
答案 0 :(得分:0)
你的转换声明正在捣乱它。你应该
switch(data) {
case "reload":
console.log('DO reload');
break;
case "switch":
console.log('DO switch');
break;
default:
console.log(data);
}
答案 1 :(得分:0)
//comparison operator in the switch " "==> it get the data as string below method worked for me just normal comparison
switch(true) {
case data == reload:
console.log('DO reload'); //will work just test
break;
case data == switch:
console.log('DO switch'); ///working
break;
default:
console.log(data); //<<< this DOES work
}