dnode和nowjs有什么区别?
-
24-10-2019 - |
题
两者如何相互比较?
解决方案
tl; dr
dnode
- 提供RMI;
- 远程功能可以接受回调作为参数;
- 这很好,因为它是完全异步的;
- 运行独立或通过现有的HTTP服务器;
- 可以拥有浏览器和节点客户端;
- 支持中间件
connect
; - 已经比现在更长。
现在
- 超越RMI,并实现“共享范围” API。 就像Dropbox, ,仅具有变量和函数而不是文件;
- 远程功能 也接受回调 (感谢Nowjs的Sridatta和Eric的澄清);
- 取决于听力HTTP服务器的工作;
- 只能有浏览器客户端;
- 最近公开了;
- 现在有些越野车。
结论
现在,现在更像是一个玩具 - 但随着它的成熟而保持手表。对于严肃的东西,也许会随身携带。有关这些库的更详细的评论,请继续阅读。
dnode
DNODE提供了一个远程方法调用框架。客户端和服务器都可以互相公开功能。
// 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正确包裹为代理,可以在另一个端点上回电。这是基本的:dnode完全异步;它在等待远程方法返回时不会阻止:
// 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
})
必须通过回调才能从另一个端点接收响应。复杂的对话可能会很快变得不可读。 这个问题 讨论有关此问题的可能解决方案。
// 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客户端可以是在节点实例中运行的脚本,也可以嵌入网页中。在这种情况下,它将仅连接到服务网页的服务器。 连接 在这种情况下有很大的帮助。该方案经过所有现代浏览器和Internet Explorer 5.5和7进行了测试。
Dnode不到一年前就在2010年6月开始。它就像节点库一样成熟。在我的测试中,我没有发现明显的问题。
现在
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-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.')
}
NowJ中的函数可以具有返回值。要获得它们,必须通过回调:
// On the client
now.twice(10, function (r) { console.log(r) }
// On the server
everyone.now.twice = function(n) {
return 2 * n
}
如果您想将回调作为诚实的论点(不收集返回值),这将有含义 - 必须始终通过返回值收集器,或者现在可能会感到困惑。根据开发人员的说法,这种以隐式回调检索回报值的方式可能会在将来发生变化:
// 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
, , 尽管。
与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会发生很多变化。
从积极的一面来看,开发人员非常容易访问 - 埃里克甚至指导我使回调工作。源代码没有记录,但幸运的是简单而简短,用户指南和示例足以开始使用。
其他提示
NowJS团队成员在这里。纠正安德烈的答案:
现在 完全支持“远程方法调用”. 。您可以将函数作为远程调用中的参数传递,并且也可以将函数作为返回值也可以。
这些函数就像在dnode中一样由nowJ包裹,以便它们在定义函数的机器上执行。就像在dnode中一样,这很容易将新功能公开到远程端。
ps此外,我不知道Andref是否意味着远程呼叫仅在dnode上是异步。远程呼叫在nowjs上也是异步的。他们不会阻止您的代码。