【问题标题】:What is the difference between dnode and nowjs?dnode 和 nowjs 有什么区别?
【发布时间】:2011-03-15 19:58:39
【问题描述】:

两者比较如何?

【问题讨论】:

    标签: javascript node.js nowjs-sockets dnode


    【解决方案1】:

    TL;DR

    DNode

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

    NowJS

    结论

    NowJS 现在更像是一个玩具——但请注意它的成熟。为了 严肃的事情,也许与 DNode 一起去。更详细地回顾这些 图书馆,请继续阅读。

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

    必须传递回调才能接收对方的响应 端点。复杂的对话很快就会变得难以阅读。 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 一样成熟 图书馆可以。在我的测试中,我没有发现明显的问题。

    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 月)。因此,期望它 是越野车。我在写这个答案时发现了issues自己。也期待它的 API 变化很大。

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

    【讨论】:

    • nowjs 项目似乎在几个月前就被放弃了 - 是否开发了任何替代品?
    【解决方案2】:

    NowJS 团队成员在这里。更正andref的回答:

    NowJS 完全支持“远程方法调用”。您可以在远程调用中将函数作为参数传递,也可以将函数作为返回值。

    这些函数由 NowJS 包装,就像它们在 DNode 中一样,以便它们在定义函数的机器上执行。这使得向远程端公开新功能变得容易,就像在 DNode 中一样。

    附:此外,我不知道 andref 是否意味着远程调用仅在 DNode 上是异步的。远程调用在 NowJS 上也是异步的。它们不会阻止您的代码。

    【讨论】:

    • 我马上更新答案!对不准确之处深表歉意。
    • 是否已经开发了nowjs的替代品(因为该项目现已解散)?
    • 我想我已经找到了上一个问题的答案:stackoverflow.com/questions/14097623/…
    【解决方案3】:

    没有尝试过 Dnode,所以我的回答不是比较。但是我想提出一些使用nowjs的经验。

    Nowjs 是基于 socket.io 的,这非常有问题。我经常在短时间内多次遇到会话超时、断开连接和now.ready 事件触发。在 nowjs github 页面上查看 this issue

    我还发现在某些平台上使用 websocket 是不可行的,但是可以通过显式禁用 websocket 来避免这种情况。

    我曾计划使用 nowjs 创建一个生产应用程序,但它似乎还不够成熟,无法依赖。如果它符合我的目的,我会尝试 dnode,否则我将切换到普通的 express

    更新:

    Nowjs seems 将被废弃。自 8 个月以来没有提交。

    【讨论】:

    • 我仍在尝试为 now.js 找到一个有用的替代品。我真希望它没有这么快就被抛弃了。 :(
    猜你喜欢
    • 1970-01-01
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    • 2012-03-14
    • 2012-02-06
    • 2011-02-25
    • 2011-11-22
    • 2015-03-26
    相关资源
    最近更新 更多