两者如何比较?

最佳答案

TL; DR

D节点

  • 提供RMI;
  • 远程函数可以接受回调作为参数;
  • 很好,因为它是完全异步的;
  • 独立运行或通过现有的http服务器运行;
  • 可以具有浏览器和Node客户端;
  • 支持中间件,就像connect一样;
  • 已经比NowJS长了。

  • NowJS
  • 不仅限于RMI,还实现了“共享范围” API。 It's likeDropbox,仅包含变量和函数,而不包含文件;
  • 远程函数还接受回调(thanks to Sridatta and Eric from NowJSfor the clarification);
  • 依赖于监听的http服务器工作;
  • 只能具有浏览器客户端;
  • 是最近才公开的;
  • 目前有点 buggy 。

  • 结论

    NowJS现在更像是一种玩具-但随着 watch 的成熟,请保留 watch 。为了
    严重的问题,也许与DNode一起使用。有关这些的更详细的评论
    库,请继续阅读。

    D节点

    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
    })
    

    回调必须传递,以便接收来自其他方的响应
    端点。复杂的对话很快就会变得不可读。 Thisquestion讨论了此问题的可能解决方案。
    // 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一样成熟。
    库可以。在测试中,我没有发现明显的问题。

    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-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月)。因此,期望它
    buggy 。在写此答案时,我自己找到了issues。还期望它
    API改变了很多。

    从积极的方面来说,开发人员非常容易访问-埃里克(Eric)甚至指导了我
    使回调起作用。源代码没有记录,但是幸运的是
    简单简短,用户指南和示例足以使您入门。

    关于javascript - dnode和nowjs有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5317282/

    10-10 00:37