您好,欢迎访问一九零五行业门户网

使用DNode实现php和nodejs之间通信的简单实例_node.js

一、安装dnode
1, for nodejs, 执行
复制代码 代码如下:
$ sudo npm install dnode
2, for php, 利用composer来安装dnode php执行下列语句下载composer
复制代码 代码如下:
$ wget http://getcomposer.org/composer.phar
创建一个文件composer.json,然后填入如下语句,
复制代码 代码如下:
{
    require: {
        dnode/dnode: 0.2.0
    }
}
执行如下语句安装,
复制代码 代码如下:
$ sudo php composer.phar install
二、利用nodejs创建简单server程序, server.js
复制代码 代码如下:
var dnode = require('dnode');
var server = dnode({
    zing: function (n, cb) { cb(n * 100) }
});
server.listen(7070);
三、利用php创建客户端程序client.php, 其中需要引用刚才安装的dnode文件夹里面的文件autoload.php
复制代码 代码如下:
// connect to dnode server running in port 7070 and call
// zing with argument 33
require 'lib/vendor/autoload.php';
// this is the class we're exposing to dnode
class temp
{
    // compute the client's temperature and stuff that value into the callback
    public function temperature($cb)
    {
    }
}
$loop = new react\eventloop\streamselectloop();
$dnode = new dnode\dnode($loop, new temp());
$dnode->connect(7070, function($remote, $connection) {
    // remote is a proxy object that provides us all methods
    // from the server
    $remote->zing(33, function($n) use ($connection) {
        echo n = {$n}\n;
        // once we have the result we can close the connection
        $connection->end();
    });
});
$loop->run();
?>
四、执行服务器端
复制代码 代码如下:
$ node server.js
五、执行客户端调用服务端程序
复制代码 代码如下:
$ php client.php
这会调用服务器端的加法程序,然后输出结果
复制代码 代码如下:
n = 3300
其它类似信息

推荐信息