Javascript 在Nowjs中传递带有回调参数的对象

Javascript 在Nowjs中传递带有回调参数的对象,javascript,node.js,nowjs-sockets,Javascript,Node.js,Nowjs Sockets,我了解到nowjs支持传递对象以及字符串,但由于某些原因,我在使用它时遇到了问题。这是他们网页上的nowjs示例,只需解析一个字符串,对我来说效果很好 客户端 now.test('foo', function(msg){ console.log(msg); }); now.test('default', function(msg){ console.log(Object.keys(msg)); console.log(msg.GetEthern

我了解到nowjs支持传递对象以及字符串,但由于某些原因,我在使用它时遇到了问题。这是他们网页上的nowjs示例,只需解析一个字符串,对我来说效果很好

客户端

  now.test('foo', function(msg){
       console.log(msg);
  });
now.test('default', function(msg){
       console.log(Object.keys(msg));
       console.log(msg.GetEthernet());
  });
服务器端

everyone.now.test = function(val, callback){
  callback(val + ' bar!');
}
everyone.now.test = function(val, callback){
    var profile = honeydConfig.GetProfile(val);
    console.log("Got eth " + profile.GetEthernet() + " for profile " + profileName);
    callback(profile);
}
我尝试传递以下代码,并将其作为val的对象

客户端

  now.test('foo', function(msg){
       console.log(msg);
  });
now.test('default', function(msg){
       console.log(Object.keys(msg));
       console.log(msg.GetEthernet());
  });
服务器端

everyone.now.test = function(val, callback){
  callback(val + ' bar!');
}
everyone.now.test = function(val, callback){
    var profile = honeydConfig.GetProfile(val);
    console.log("Got eth " + profile.GetEthernet() + " for profile " + profileName);
    callback(profile);
}

在服务器端,它打印GetEthernet函数的正确输出。在客户端,它只是说,“uncaughttypeerror:Object”没有“GetEthernet”方法,并返回Object.keys的空数组“

我没有使用nowjs,但是如果它能够在服务器和客户端之间发送完整的JavaScript对象,也就是说,具有原型链和方法的对象,我会非常惊讶。无论您使用的是什么框架,在客户端和服务器之间传递的对象都需要序列化为字符串;对于JavaScript,这通常意味着JSON序列化。如果nowjs文档说您可以传递一个对象,那么它们可能意味着一个普通的JavaScript对象,一个可JSON序列化的对象。否则,您必须序列化对象方法并将它们发送给客户机进行重新解释,这对于除了最简单的方法之外的所有方法都没有任何意义

长话短说,尝试传递
profile.GetEthernet()
的输出,而不是传递
profile
并调用该方法。如果您需要传递有关
概要文件的更多数据,您可以使用各种方法输出创建一个对象并发送它。

就是这样。在服务器端添加了“profile.ethernet=profile.getEthernet()”,效果良好。显然,它可以发送对象的成员变量,但不能发送方法,这很有意义。谢谢