I want to send javascript code to socket.io server

I want to send javascript code to socket.io server so that the server sends to clients and this code is executed.

that I tried to make a json variable like this. and send this via socket.io

 var sent={
          'code': function(){
           console.log('javascript code');
          }

        };

socket.send(sent); 

when I check the server, the message arrives {}and is the same as for the other client.

what is wrong in this code, how do I send javascript code?

+3
source share
1 answer

Do not do that. You will open all listening clients for hacking.

who said ...

... you can follow pimbdb and pass the function as a string, and then use evalon the receiving side to execute it:

// on the sending client
var sent = {
    "code": "function() { /* do something not evil */ }"
}
socket.send(sent);

// on the receiving client
socket.on('message', function(data) {
    if (data.code) eval(data.code); // and pray.
});

, . , .

EDIT: , , . ↔ XSS-, .

+2

All Articles