两者相比如何?
最佳答案
TL; 博士
Node
connect
; 现在JS
结论
NowJS 现在更像是一个玩具——但随着它的成熟,请密切关注。为了
严肃的事情,也许和 DNode 一起去。要更详细地审查这些
图书馆,一起阅读。
Node
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
})
回调必须被传递,以便从另一个接收响应
端点。复杂的对话很快就会变得难以理解。 This question讨论此问题的可能解决方案。
// 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 进行了测试。
DNode 成立不到一年,2010 年 6 月。它和 Node 一样成熟
图书馆可以。在我的测试中,我没有发现明显的问题。
现在JS
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.')
}
NowJS 中的函数可以有返回值。要获得它们,回调必须是
通过:
// On the client
now.twice(10, function (r) { console.log(r) }
// On the server
everyone.now.twice = function(n) {
return 2 * n
}
如果您想将回调作为诚实的参数(不是
收集返回值)——必须始终通过返回值收集器,或
NowJS 可能会感到困惑。根据开发人员的说法,这种检索方式
带有隐式回调的返回值将来可能会改变:
// 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 浏览器中运行的脚本。
包含脚本的页面必须由正在运行的同一个 Node 提供服务
服务器。
在服务器端,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 月)。因此,期待它
是 buggy 。我找到了 issues我自己在写这个答案时。也期待它的
API变化很大。
从积极的方面来说,开发人员非常容易接近——Eric 甚至指导了我
使回调工作。源代码没有记录,但幸运的是
简单而简短,用户指南和示例足以让您入门。
关于javascript - dnode 和 nowjs 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5317282/