and*_*ref 61

TL; DR

DNode

  • 提供RMI;
  • 远程函数可以接受回调作为参数;
  • 这很好,因为它是完全异步的;
  • 独立运行或通过现有的http服务器运行;
  • 可以有浏览器和节点客户端;
  • 支持中间件,就像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!')
})
Run Code Online (Sandbox Code Playgroud)

传递给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)
Run Code Online (Sandbox Code Playgroud)

导出的方法可以传递任何东西,包括函数.它们被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
})
Run Code Online (Sandbox Code Playgroud)

必须传递回调才能从其他端点接收响应.复杂的会话可能会很快变得难以理解.此问题讨论了此问题的可能解决方案.

// 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)
        })
    })
})
Run Code Online (Sandbox Code Playgroud)

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.')
Run Code Online (Sandbox Code Playgroud)

当服务器端"共享"功能运行时,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.')
}
Run Code Online (Sandbox Code Playgroud)

NowJS中的函数可以具有返回值.要获得它们,必须传递回调:

// On the client

now.twice(10, function (r) { console.log(r) }

// On the server

everyone.now.twice = function(n) {
    return 2 * n
}
Run Code Online (Sandbox Code Playgroud)

如果你想将一个回调作为一个诚实的参数传递(而不是收集一个返回值),这就意味着 - 一个人必须总是传递返回值收集器,否则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
}
Run Code Online (Sandbox Code Playgroud)

这就是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)
Run Code Online (Sandbox Code Playgroud)

NowJS第一次提交是几周前(2011年3月).因此,期待它是错误的.在写这个答案时我自己发现了问题.也期望它的API会发生很大变化.

从积极的方面来说,开发人员非常容易接受--Eric甚至引导我进行回调工作.源代码没有记录,但幸运的是简单和简短,用户指南和示例足以启动它.


Sri*_*ala 14

NowJS团队成员在这里.更正了andref的答案:

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

这些函数由NowJS包装,就像它们在DNode中一样,以便它们在定义函数的机器上执行.这样可以很容易地将新功能公开到远程端,就像在DNode中一样.

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