TL; DR
D节点
- 提供RMI;
- 远程函数可以接受回调作为参数;
- 很好,因为它是完全异步的;
- 独立运行或通过现有的http服务器运行;
- 可以具有浏览器和Node客户端;
- 支持中间件,就像
connect
; - 已经比NowJS长了。
NowJS
- 不仅限于RMI,还实现了“共享范围” API。就像Dropbox一样,只包含变量和函数而不是文件;
- 远程函数 也接受回调
- 依靠监听的http服务器工作;
- 只能有浏览器客户端;
- 最近公开;
- 现在有点越野车。
结论
NowJS现在更像是一种玩具-但随着手表的成熟,请留意。对于严重的问题,也许与DNode一起使用。要详细了解这些库,请继续阅读。
D节点
DNode提供了远程方法调用框架。客户端和服务器都可以彼此公开功能。
// On the servervar server = DNode(function () { this.echo = function (message) { console.log(message) }}).listen(9999)// On the clientdnode.connect(9999, function (server) { server.echo('Hello, world!')})传递给
DNode()的函数是一个处理函数,与传递给的函数不同
http.createServer。它具有两个参数:
client可用于访问客户端导出的功能,
connection并可用于处理与连接有关的事件:
// On the servervar 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适当地包装为代理,并且可以在另一个端点被回调。这是基础:DNode是完全异步的;在等待远程方法返回时不会阻塞:
// A contrived example, of course.// On the servervar server = DNode(function (client) { this.echo = function (message) { console.log(message) return 'Hello you too.' }}).listen(9999)// On the clientdnode.connect(9999, function (server) { var ret = server.echo('Hello, world!') console.log(ret) // This won't work})必须传递回叫以便从另一个端点接收响应。复杂的对话很快就会变得不可读。
// On the servervar 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 clientdnode.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实例内运行的脚本,也可以嵌入在网页内。在这种情况下,它将仅连接到为网页提供服务的服务器。在这种情况下,Connect提供了很大的帮助。此方案已在所有现代浏览器以及InternetExplorer 5.5和7中进行了测试。
DNode于不到一年前(2010年6月)启动。它与Node库一样成熟。在测试中,我没有发现明显的问题。
NowJS
NowJS提供了一种神奇的API,与可爱的东西接壤。服务器具有
everyone.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-sidenow.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 pre 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中的函数可以具有返回值。要获取它们,必须传递一个回调:
// On the clientnow.twice(10, function (r) { console.log(r) }// On the servereveryone.now.twice = function(n) { return 2 * n}如果您想将回调作为诚实的参数传递(而不是收集返回值),则这意味着-
必须始终传递返回值收集器,否则NowJS可能会感到困惑。根据开发人员的说法,这种使用隐式回调检索返回值的方法将来可能会改变:
// On the clientnow.crunchSomeNumbers('compute-primes', function (data) { }, function (queueLength) { alert('You are number ' + queueLength + ' on the queue.') })// On the servereveryone.now.crunchSomeNumbers = function(task, dataCallback) { superComputer.enqueueTask(task, dataCallback) return superComputer.queueLength}这就是NowJS API。好吧,实际上还有3个功能可用于检测客户端连接和断开连接。我不知道为什么他们没有使用来公开这些功能
EventEmitter。
与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)甚至指导我使回调工作。源代码未记录,但幸运的是简短而简短,并且用户指南和示例足以使您入门。



