>  기사  >  백엔드 개발  >  利用DNode实现php和nodejs之间的通信

利用DNode实现php和nodejs之间的通信

WBOY
WBOY원래의
2016-06-23 13:56:481158검색

一,安装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 33require 'lib/vendor/autoload.php';// This is the class we're exposing to DNodeclass 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



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.