两者如何比较?
connect
;NowJS现在更像是一种玩具-但随着手表的成熟,请留意。对于严重的问题,也许与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客户端可以是在Node实例内运行的脚本,也可以嵌入在网页内。在这种情况下,它将仅连接到为网页提供服务的服务器。在这种情况下,Connect提供了很大的帮助。此方案已在所有现代浏览器以及InternetExplorer 5.5和7中进行了测试。
DNode于不到一年前(2010年6月)启动。它与Node库一样成熟。在测试中,我没有发现明显的问题。
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浏览器中运行的脚本。包含脚本的页面必须由运行服务器的同一节点提供。
在服务器端,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)甚至指导我使回调工作。源代码未记录,但幸运的是简短而简短,并且用户指南和示例足以使您入门。
问题内容: 两者都意味着空间,但是有什么区别吗? 问题答案: 一个是不间断空间,另一个是常规空间。不间断的空格表示该行不应在该点处换行,就像它不会在一个单词的中间换行一样。 此外,正如斯文德(Svend)在其评论中指出的那样,不间断的空间不会崩溃。
本文向大家介绍<%# %> 和 <% %> 有什么区别?相关面试题,主要包含被问及<%# %> 和 <% %> 有什么区别?时的应答技巧和注意事项,需要的朋友参考一下 答:<%# %>表示绑定的数据源 <%%>是服务器端代码块
问题内容: 以下代码之间有什么区别: 和 Python建议采用一种做事方式,但有时似乎不止一种。 问题答案: 一个是函数调用,一个是文字: 使用第二种形式。它更具Python风格,并且可能更快(因为它不涉及加载和调用单独的函数)。
DNode:一个基于 NodeJs 的 JSON 协议的 RPC 远程调用解决方案,并且还可以实现浏览器端直接调用远程服务(通过基于 socket.io 的 websocket 开发库)。 安装方法:npm install dnode 示例代码: var dnode = require('dnode');dnode.connect(5050, function (remote) { remo
发展至今(2020 年 6 月份),GCC 编译器已经更新至 10.1.0 版本,其功能也由最初仅能编译 C 语言,扩增至可以编译多种编程语言,其中就包括 C++ 。 除此之外,当下的 GCC 编译器还支持编译 Go、Objective-C,Objective-C ++,Fortran,Ada,D 和 BRIG(HSAIL)等程序,甚至于 GCC 6 以及之前的版本还支持编译 Java 程序。但本
问题内容: 比较PMD和CheckStyle有一个问题。但是,我找不到关于PMD和FindBugs之间差异/相似性的详尽分类。我相信一个关键的区别是PMD适用于源代码,而FindBugs适用于编译的字节码文件。但是就功能而言,这应该是一个选择,还是两者相辅相成? 问题答案: 我同时使用。我认为他们是相辅相成的。 正如您所说,PMD在源代码上工作,因此会发现诸如以下问题:违反命名约定,缺少花括号,放