dnode和nowjs有什么区别?

两者如何比较?

TL; DR

DNode

  • 提供RMI;
  • 远程函数可以接受callback作为参数;
  • 这是很好的,因为它是完全asynchronous的;
  • 独立运行或通过现有的http服务器运行;
  • 可以有浏览器和Node客户端;
  • 支持中间件,就像connect一样;
  • 已经比NowJS长了很多。

NowJS

  • 超越了RMI,实现了“共享范围”API。 这就像Dropbox ,只有variables和函数,而不是文件;
  • 远程函数也接受callback ( 感谢来自NowJS的Sridatta和Eric的澄清 )。
  • 依靠一个监听http服务器来工作;
  • 只能有浏览器客户端;
  • 最近才上市;
  • 现在有些错误。

结论

NowJS现在更多的是一个玩具,但是随着成熟,还要随时关注。 对于严重的东西,也许去与DNode。 有关这些库的更详细的评论,请阅读。

DNode

DNode提供了一个远程方法调用框架。 客户端和服务器都可以向对方公开function。

 // On the server var server = DNode(function () { this.echo = function (message) { console.log(message) } }).listen(9999) // On the client dnode.connect(9999, function (server) { server.echo('Hello, world!') }) 

传递给DNode()的函数与传递给http.createServer的处理函数不同。 它有两个参数: client可以用来访问客户端导出的函数, connection可以用来处理连接相关的事件:

 // On the server var server = DNode(function (client, connection) { this.echo = function (message) { console.log(message) connection.on('end', function () { console.log('The connection %s ended.', conn.id) }) } }).listen(9999) 

导出的方法可以传递任何东西,包括函数。 它们被DNode正确包装为代理,并且可以在另一个端点被callback。 这是基本的:DNode是完全asynchronous的; 它在等待远程方法返回时不会阻塞:

 // A contrived example, of course. // On the server var server = DNode(function (client) { this.echo = function (message) { console.log(message) return 'Hello you too.' } }).listen(9999) // On the client dnode.connect(9999, function (server) { var ret = server.echo('Hello, world!') console.log(ret) // This won't work }) 

必须传递callback才能接收来自另一个端点的响应。 复杂的对话可能会很快变得不可读。 这个问题讨论了这个问题的可能解决scheme

 // On the server var server = DNode(function (client, callback) { this.echo = function (message, callback) { console.log(message) callback('Hello you too.') } this.hello = function (callback) { callback('Hello, world!') } }).listen(9999) // On the client dnode.connect(9999, function (server) { server.echo("I can't have enough nesting with DNode!", function (response) { console.log(response) server.hello(function (greeting) { console.log(greeting) }) }) }) 

DNode客户端可以是在Node实例内运行的脚本,也可以embedded到网页中。 在这种情况下,它只会连接到服务于该网页的服务器。 Connect在这种情况下是非常有帮助的。 这种情况已经过所有现代浏览器和Internet Explorer 5.5和7的testing。

DNode是在不到一年前的2010年6月开始的。它和Node库一样成熟。 在我的testing中,我发现没有明显的问题。

NowJS

NowJS提供了一种可爱的魔术API。 服务器有一个everyone.now范围。 所有的东西都放在everyone.now内部。 now ,每个客户everyone.now可以看到他们now范围。

服务器上的这个代码将与每个将消息写入服务器控制台的客户端共享一个echo函数:

 // Server-side: everyone.now.echo = function (message) { console.log(message) } // So, on the client, one can write: now.echo('This will be printed on the server console.') 

当一个服务器端的“共享”函数运行时, this将有一个now属性,特定于进行该调用的客户端。

 // Client-side now.receiveResponse = function (response) { console.log('The server said: %s') } // We just touched "now" above and it must be synchronized // with the server. Will things happen as we expect? Since // the code is not multithreaded and NowJS talks through TCP, // the synchronizing message will get to the server first. // I still feel nervous about it, though. now.echo('This will be printed on the server console.') // Server-side: everyone.now.echo = function (message) { console.log(message) this.now.receiveResponse('Thank you for using the "echo" service.') } 

NowJS中的函数可以有返回值。 为了得到它们,必须传递一个callback:

 // On the client now.twice(10, function (r) { console.log(r) } // On the server everyone.now.twice = function(n) { return 2 * n } 

这意味着如果你想传递callback作为一个诚实的参数(而不是收集返回值) – 必须总是通过返回值收集器,否则NowJS可能会感到困惑。 根据开发人员的说法,这种以隐式callback方式检索返回值的方式在未来可能会发生变化:

 // On the client now.crunchSomeNumbers('compute-primes', /* This will be called when our prime numbers are ready to be used. */ function (data) { /* process the data */ }, /* This will be called when the server function returns. Even if we didn't care about our place in the queue, we'd have to add at least an empty function. */ function (queueLength) { alert('You are number ' + queueLength + ' on the queue.') } ) // On the server everyone.now.crunchSomeNumbers = function(task, dataCallback) { superComputer.enqueueTask(task, dataCallback) return superComputer.queueLength } 

这是NowJS API的。 那么实际上还有3个function可以用来检测客户端的连接和断开。 不过,我不知道为什么他们没有使用EventEmitter公开这些function。

与DNode不同,NowJS要求客户端是一个在Web浏览器内运行的脚本。 包含该脚本的页面必须由运行服务器的相同节点提供服务。

在服务器端,NowJS也需要HTTP服务器监听。 在初始化NowJS时必须通过:

 var server = http.createServer(function (req, response) { fs.readFile(__dirname + '/now-client.html', function (err, data) { response.writeHead(200, {'Content-Type':'text/html'}) response.write(data) response.end() }) }) server.listen(8080) var everyone = now.initialize(server) 

NowJS第一次提交是从几个星期前(2011年3月)。 因此,期望它是越野车。 我在写这个答案时自己发现了一些问题 。 也期望它的API改变很多。

从积极的方面来说,开发人员是非常方便的 – Eric甚至指导我做callback工作。 源代码没有logging,但幸运的是简单和短,用户指南和例子足以让一个开始。

NowJS团队成员在这里。 更正andref的答案:

NowJS 完全支持“远程方法调用” 。 您可以在远程调用中将函数作为parameter passing,您也可以将函数作为返回值。

这些函数被包装在NowJS中,就像它们在DNode中一样,以便它们在定义函数的机器上执行。 这样就可以很容易地将新function展示给远端,就像在DNode中一样。

PS此外,我不知道是否意味暗示远程调用只在DNodeasynchronous。 远程调用在NowJS上也是asynchronous的。 他们不会阻止你的代码。

没有尝试过Dnode,所以我的回答不是一个比较。 但是我想提出一些使用nowjs的经验。

Nowjs基于socket.io ,这是相当多的bug。 我经常遇到会话超时,断开和now.ready事件在很短的时间内多次发射。 看看nowjs github页面上的这个问题 。

另外我发现在某些平台上使用websockets是不可行的,但是这可以通过明确地禁用websocket来绕开。

我曾经计划使用nowjs创build一个生产应用程序,但是它似乎还不够成熟。 我会尝试dnode,如果它符合我的目的,否则我会切换到简单的express

更新:

Nowjs 似乎被废弃了。 自8个月以来没有提交。

Interesting Posts