当前位置:首页 / 编程技术 / 前端技术 / 使用DNode实现php和nodejs之间通信的简单实例
使用DNode实现php和nodejs之间通信的简单实例
发布时间:2022/07/10来源:编程网

一、安装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
<?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 ReactEventLoopStreamSelectLoop(); $dnode = new DNodeDNode($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
分享到:
免责声明:本文仅代表文章作者的个人观点,与本站无关,请读者仅作参考,并自行核实相关内容。文章内容来源于网络,版权归原作者所有,如有侵权请与我们联系,我们将及时删除。
资讯推荐
热门最新
精品工具
全部评论(0)
剩余输入数量90/90
暂无任何评论,欢迎留下你的想法
你可能感兴趣的资讯
换一批