javascript - dnode 和 nowjs 有什么区别?

标签 javascript node.js nowjs-sockets dnode

两者相比如何?

最佳答案

TL; 博士

Node

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

  • 现在JS
  • 超越了 RMI 并实现了“共享范围”API。 It's like Dropbox , 仅使用变量和函数而不是文件;
  • 远程功能也接受回调 ( thanks to Sridatta and Eric from NowJS for the clarification );
  • 依赖于监听 http 服务器的工作;
  • 只能有浏览器客户端;
  • 最近才公开;
  • 现在有点问题。

  • 结论

    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/

    相关文章:

    node.js - 由于 Windows 上的长路径错误,无法将 Node js 模块添加到 git

    javascript - 为什么我的 Nginx-Socket.io-Express-Setup 无法通过 HTTPS 运行?

    node.js - 无法加载/nowjs/now.js

    javascript - 尝试使用添加和删除类功能合并简单的 slider 菜单,但没有成功

    javascript - 正则表达式 - 包含句点后的特定数字

    javascript - 在 Gulp AngularJS 应用程序中实现 Google 翻译

    javascript - 现在js : [RangeError: Maximum call stack size exceeded]

    javascript - 是否可以使用Office 2013 Office api插入base64 Word文件

    javascript - 如何在 Javascript/Node JS 中使用 Promise 等待函数执行以等待其回调

    node.js - now.js : "Object has no method" error message when trying to start server. js