Thursday 8 June 2017

Socket.io > calling client-side functions from server?

I am trying to execute functions on the client side from a node server using socket.io. The data is getting to the client side no problem but I am not able to execute functions on connect.

My server code (emit in a loop):

var io = require('socket.io')(server);
io.on('connection', function(client){
    setInterval(function(){
        client.emit('for_client', datatosend);
    }, 100);
});

My client side code:

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
    }

});

And here is proof the data is coming through ;)

enter image description here

Any help is super appreciated.



via andehlu

No comments:

Post a Comment