dnode和nowjs有什么区别? [英] What is the difference between dnode and nowjs?
问题描述
两者如何相互比较?
推荐答案
TL; DR
DNode
- 提供RMI;
- 远程函数可以接受回调作为参数;
- 这很好,因为它是完全异步的;
- 独立运行或通过现有的http服务器运行;
- 可以拥有浏览器和节点客户端;
- 支持中间件,就像
connect
; - 比NowJS更长。
- provides RMI;
- remote functions can accept callbacks as arguments;
- which is nice, since it is fully asynchronous;
- runs stand-alone or through an existing http server;
- can have browser and Node clients;
- supports middleware, just like
connect
; - has been around longer than NowJS.
- 不仅仅是RMI,还实现了共享范围API。 就像
Dropbox ,只有变量和函数而不是文件; - 远程函数也接受回调(感谢来自NowJS
的Sridatta和Eric澄清); - 依赖于监听http服务器工作;
- 只能拥有浏览器客户端;
- 最近才上市;
- 现在有些错误。
- goes beyond just RMI and implements a "shared scope" API. It's like Dropbox, only with variables and functions instead of files;
- remote functions also accept callbacks (thanks to Sridatta and Eric from NowJS for the clarification);
- depends on a listening http server to work;
- can only have browser clients;
- became public very recently;
- is somewhat buggy right now.
TL;DR
DNode
NowJS现在更像是一种玩具 - 但随着它的成熟,请保持观察。对于
严重的东西,也许与DNode一起去。有关这些
库的更详细的评论,请继续阅读。
NowJS is more of a toy right now -- but keep a watch as it matures. For serious stuff, maybe go with DNode. For a more detailed review of these libraries, read along.
DNode提供了远程方法调用框架。客户端和服务器
都可以相互公开函数。
DNode provides a Remote Method Invocation framework. Both the client and server can expose functions to each other.
// 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()
的函数是一个与传递给$ b的处理程序不同的处理程序$ b http.createServer
。它有两个参数: client
可用于访问客户端导出的
函数和连接
可以用于处理
连接相关事件:
The function that is passed to DNode()
is a handler not unlike the one passed to
http.createServer
. It has two parameters: client
can be used to access the
functions exported by the client and connection
can be used to handle
connection-related events:
// 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)
导出的方法可以传递任何东西,包括函数。它们是由dbode包装为代理的
,可以在另一个端点回调。这是
基础:DNode是完全异步的;在等待
远程方法返回时它不会阻塞:
The exported methods can be passed anything, including functions. They are properly wrapped as proxies by DNode and can be called back at the other endpoint. This is fundamental: DNode is fully asynchronous; it does not block while waiting for a remote method to return:
// 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
})
为了接收来自其他
端点的响应,必须传递回调。复杂的会话可能会很快变得难以理解。 这个
问题讨论此问题的可能解决方案。
Callbacks must be passed around in order to receive responses from the other endpoint. Complicated conversations can become unreadable quite fast. This question discusses possible solutions for this problem.
// 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实例内运行的脚本,也可以是
嵌入在网页中。在这种情况下,它只会连接到
服务于该网页的服务器。在这种情况下, Connect 可以提供很大帮助。此方案已在所有现代浏览器和Internet Explorer 5.5和7中进行了测试。
The DNode client can be a script running inside a Node instance or can be embedded inside a webpage. In this case, it will only connect to the server that served the webpage. Connect is of great assistance in this case. This scenario was tested with all modern browsers and with Internet Explorer 5.5 and 7.
DNode于2010年6月开始不到一年。它像Node一样成熟
库可以。在我的测试中,我发现没有明显的问题。
DNode was started less than a year ago, on June 2010. It's as mature as a Node library can be. In my tests, I found no obvious issues.
NowJS提供了一种边界的神奇API在可爱。服务器有
everyone.now
范围。放在 everyone.now
内的所有东西都变成
,通过他们的现在
范围对每个客户都可见。
NowJS provides a kind of magic API that borders on being cute. The server has an
everyone.now
scope. Everything that is put inside everyone.now
becomes
visible to every client through their now
scope.
服务器上的这段代码将与
写入消息的每个客户共享一个 echo
函数服务器控制台:
This code, on the server, will share an echo
function with every client that
writes a message to the server console:
// 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.')
当服务器端共享功能时运行,此
将具有 now
属性
,该属性特定于进行该调用的客户端。 / p>
When a server-side "shared" function runs, this
will have a now
attribute
that is specific to the client that made that call.
// 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中的函数可以有返回值。要获得它们,回调必须是
传递:
Functions in NowJS can have return values. To get them, a callback must be passed:
// On the client
now.twice(10, function (r) { console.log(r) }
// On the server
everyone.now.twice = function(n) {
return 2 * n
}
如果你想要,这有一个含义传递一个回调作为一个诚实的参数(而不是
来收集一个返回值) - 一个必须总是传递返回值收集器,或
NowJS可能会感到困惑。根据开发人员的说法,这种检索方式带有隐式回调的
返回值将来可能会改变:
This has an implication if you want to pass a callback as an honest argument (not to collect a return value) -- one must always pass the return value collector, or NowJS may get confused. According to the developers, this way of retrieving the return value with an implicit callback will probably change in the future:
// 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个函数可以用
来检测客户端连接和断开连接。我不知道为什么
没有使用 EventEmitter
公开这些功能。
And this is it for the NowJS API. Well, actually there are 3 more functions that
can be used to detect client connection and disconnection. I don't know why they
didn't expose these features using EventEmitter
, though.
与DNode不同,NowJS要求客户端是在Web浏览器中运行的脚本。
包含脚本的页面必须由运行
服务器的同一节点提供服务。
Unlike DNode, NowJS requires that the client be a script running inside a web browser. The page containing the script must be served by the same Node that is running the server.
在服务器端,NowJS还需要一个http服务器听。初始化NowJS时必须传递
:
On the server side, NowJS also needs an http server listening. It must be passed when initializing 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月)。因此,预计它将是b
。在写这个答案时,我自己找到了问题。还期望它的
API会发生很大变化。
NowJS first commit is from a couple weeks ago (Mar 2011). As such, expect it to be buggy. I found issues myself while writing this answer. Also expect its API to change a lot.
从积极的方面来说,开发人员非常容易接受--Eric甚至引导我
来回调制作。源代码没有记录,但幸运的是
简单和简短,用户指南和示例足以让一个人启动。
On the positive side, the developers are very accessible -- Eric even guided me to making callbacks work. The source code is not documented, but is fortunately simple and short and the user guide and examples are enough to get one started.
这篇关于dnode和nowjs有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!