I want to send a javascript code to the socket.io server so that server broadcast to the clients and that code get executed .
what i tried i make a json variable like this .and send that via socket.io
var sent={
'code': function(){
console.log('javascript code');
}
};
socket.send(sent);
when i check at server the message comes is {} and same as it is to the other client .
what is wrong in this code , how should i send javascript code ?
Do not do this. You will open all listening clients to being hacked.
that said...
...you could follow pimbdb and pass the function in as a string, then use eval on the receiving end 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.
});
Again, don't. Instead, pass some non-executable data that can then be interpreted in a limited number of non-malicious ways.
EDIT: Apologies, interpreted that as if you were executing code on the server. But client<-> client is XSS-prone, still.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With