简体   繁体   English

使用node和socket.io运行自定义函数

[英]running custom functions with node and socket.io

So I would like to be able to run custom function through socket.io using node My basic idea is fairly simple, I just can't get it to work. 因此,我希望能够使用node通过socket.io运行自定义功能。我的基本思想很简单,就是无法正常工作。 Generally speaking you would write something similar to this to run a function on incoming message... 一般来说,您会编写与此类似的内容以对传入消息运行功能...

//server
socket.on('incomingMessage', function(data) {
    console.log(data);
    io.sockets.emit('newMessage', {id: data.id, from: data.name, message: data.message});
});
//client
socket.on('newMessage', function(data) {
    $('#messages').html($('#messages').html() + data.message);
    $('#from').html($('#from').html() + data.from);
});

but what I would like to do is allow that to be much more dynamic and actually pass the function I want to run in the message itself, so something like this... 但是我想做的是让它更具动态性,并实际上传递我想在消息本身中运行的功能,所以这样的事情……

//server
socket.on('customFunction', function(data) {
    console.log(data);
    io.sockets.emit('runFunction', data);
})

//client
socket.on('runFunction', function(data) {
    for(var index in data) {
        args.push(data[index]);
    }
    data.fn.apply(this, args);
});

but when I call this from a client using something like... 但是当我使用类似的方式从客户那里打电话给我时...

socket.emit('customFunction', {id: sessionId, name: user.name, callBack: function() { alert('it worked');
    }
});

on the server the only thing it logs is the session id and the name, the callBack(regardless of the name, I have tried func, run, cb, callBack, customFunc) just disappears. 在服务器上,它记录的唯一内容是会话ID和名称,callBack(不管名称如何,我尝试过func,run,cb,callBack,customFunc)都消失了。 so I am kind of at a loss. 所以我有点茫然。 Anyone have any ideas? 有人有想法么? thank you in advance for the help. 预先感谢您的帮助。 I am not sure why, but when calling this it only logs the session id and the name elements, the callback, or fn, or whatever else, just isn't there. 我不确定为什么,但是在调用此方法时,它仅记录会话ID和名称元素,回调或fn或其他任何内容,只是不存在。 Does node or socket.io remove functions from arguments when sending things off? 在发送东西时,node或socket.io是否从参数中删除函数?

You are converting data to an array and then try to call a property it doesn't have anymore then applying the data to the socket.on function ( this ) 您正在将数据转换为数组,然后尝试调用不再具有的属性,然后将数据应用于socket.on函数( this

so try something like: 所以尝试类似的东西:

socket.on('runFunction', function(data) {
    var arg = [];
    args.push(data.id);
    args.push(data.name);
    // etc...
    data.callback.apply(this, args);
});

I don't know if ii got you right but as far as i can see you have two options. 我不知道ii是否使您正确,但据我所知,您有两种选择。

  1. convert your function to string, you can do that with toString() or toSource() eval it on the other side (node or client). 将函数转换为字符串,可以使用toString()或toSource()在另一端(节点或客户端)进行评估。 But maybe and hopefully you don't want that :) - Important is that a function in not serialized if you use JSON.stringify or any framework solution to do that. 但也许并希望您不希望这样做:)-重要的是,如果您使用JSON.stringify或任何框架解决方案来执行此功能,则该函数不应序列化。

  2. Create an index of your callable functions and use the index to identify the function on the other side. 创建可调用函数的索引,并使用该索引在另一侧标识该函数。

Example: 例:

//create a index on one side
var runtime = {
"addMessage":function(param1,param2){  alert(arguments)   },
"whatEverFunction":function(){}
 }

 socket.on("runtime",function(data){
 if(!runtime[data.fn])return alert("runtime error");
 runtime[data.fn].apply(null,data.args);
 }


//create a call on the other
socket.io.emit({fn:"addMessage",args:["paramAValue","paramBValue"]});

If this does not help you, you may want to check if the library "dnode" does what you want. 如果这对您没有帮助,则可能需要检查库“ dnode”是否满足您的要求。

Good Luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM